Positioning elements in CSS using percentages allows for flexible and responsive

I am new to web design and I am trying to create a webpage similar to the one shown in this image. However, I want to set the height and width of the divs using percentage values instead of pixels.

Whenever I attempt this, the boxes end up overlapping or not appearing at all. I believe my logic is as follows:

For instance:

  1. The wrapper has attributes position:absolute;, height:100%;, width:100%; (When defined in pixels there is no issue, but when percentages are used, most times the wrapper does not render properly).
  2. The header has attributes position:relative (relative to the wrapper?) height:10%; and width:100%;
  3. The slider has attributes position:relative;, height:70%;, width;100%;, top:10% (since 10% is allocated to the header, it should start below that??) ...etc ...
  4. The footer has attributes position: relative;, height:10%;, width:100%;, bottom:0; (To fit it to the bottom)

In summary, can someone assist me with absolute and relative positioning based on my example?

 #wrapper{
background-color:red;
height:100%;
width:100%;
position: absolute;
}

#header{
background-color:yellow;
height:20%;
width:100%;
position: relative;


}

#slider{
background-color:blue;
position: relative;
height:70%;
width:100%;
top:20%;

}

#footer{
background-color:yellow;
position:relative;
height:10%;
width:100%;
top:90%;

}
<html>
<head>
<title></title>
</head>

<body>
<div id="wrapper">
<div id="header"></div>
<div id="slider"></div>
<div id="footer"></div>
</div>
</body>
</html>

Answer №1

This is a replica of your code, but with updated CSS to achieve a layout with a 20% header, 70% slider, and 10% footer.

The inner div's have a style of position: relative, eliminating the need to specify top or width, as they automatically stack vertically at full width (100%).

If using position: absolute, such as in the case of the wrapper, you must set values for left, top, width, and height.

html, body {margin: 0; height: 100%; }

#wrapper{
  background-color:red;
  left: 0;
  top: 0;
  height:100%;
  width:100%;
  position: absolute;
  box-sizing: border-box;
}

#header{
  background-color:yellow;
  height:20%;
  position: relative;
  box-sizing: border-box;
}

#slider{
  background-color:blue;
  position: relative;
  height:70%;
  box-sizing: border-box;
}

#footer{
  background-color:yellow;
  position:relative;
  height:10%;
  box-sizing: border-box;
}
<div id="wrapper">
<div id="header"></div>
<div id="slider"></div>
<div id="footer"></div>
</div>

Here's a more contemporary approach utilizing flex:

html, body {
  height: 100%;
  margin: 0;
}
.wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

header {
  flex: 0;
  flex-basis: 20%;
  background-color: yellow;
  text-align: center;
  padding: 10px;
  box-sizing: border-box;
}

main {
  flex: 1;
  flex-basis: 70%;
  background-color: blue;
  text-align: center;
  padding: 10px;
  box-sizing: border-box;
}

footer {
  flex: 0;
  flex-basis: 10%;
  background-color: yellow;
  text-align: center;
  padding: 10px;
  box-sizing: border-box;
}
<div class="wrapper">

  <header>
    Header
  </header>

  <main>
    Slider
  </main>

  <footer>
    Footer
  </footer>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Removing a Dom element using stage.removeChild( )

When the number 6 is typed and entered into the game, the function correct() in the code snippet below determines what action to take. I would like to remove the DOM element gg (the equation 3+3=input) from the stage after typing 6 and pressing enter. How ...

Unable to apply 3rd condition with ngClass into effect

I've been attempting to incorporate a third condition into my ngClass. Initially, I managed to get two classes working in my ngClass to change the row colors alternately. [ngClass]="{ totalrow:i%2 != 0, odd:i%2 == 0}" Now, I'm trying to introdu ...

What factors influence the timing of Chrome's spell-check feature for a particular word?

Currently, I am troubleshooting a bug in our code that is related to text input behavior. The issue at hand is that in one field, when you start typing on Chrome, the word does not get red underlined until you press space. However, in another field, Chrom ...

How to prevent v-menu from overlapping a navbar in Vue.js

Exploring the examples on the main page of Vuetify, we come across the v-menu component showcased in detail. Check it out here: https://vuetifyjs.com/en/components/menus/#accessibility If you activate any of the buttons to open the v-menu and then scroll ...

jQuery replacing spaces in id names

I'm encountering an issue with the following HTML and jQuery code. The problem seems to be related to the space in the ID name. HTML <span id="plus one">Plus One</span> jQuery $("#plus one").hide(); Since I'm unable to change the ...

PHP form does not seem to be vulnerable to cross-site scripting attacks

On one of my web pages, I have a form with an action php page using the following html syntax: <form name="form" action="test.php" method="get"> I decided to make it more secure by modifying it to: <form name="form" action="<?php echo htmlsp ...

Move from right to left on the slide?

Is there a way to toggle a div's visibility between collapsed and expanded, transitioning smoothly from right to left? I've noticed that most examples I come across demonstrate the transition from left to right. ...

Uncover the hidden message in a string encoded for JavaScript

This encoded HTML code is intended for use in a JavaScript function <div class=\"ProductDetail\"><div style=\"width:780px\">\r\n\t<div class=\"baslik\" style=\"margin: 0px; padding: 5px 10px ...

Leveraging React Native to position a view absolutely in the center of the screen without obstructing any other components

How can I center an image inside a view in the middle of the screen using position: "absolute"? The issue is that the view takes up 100% of the width and height of the screen, causing all components underneath it (such as input fields and buttons ...

Exploring the utility of the load_file function in the simple HTML DOM method

After writing this code and it was functioning properly. <?php include ('require/simplehtmldom_1_5/simple_html_dom.php'); echo $html=file_get_html('http://www.site.com'); ?> Now I want to make it work using the object method. He ...

``Is there a way to retrieve the file path from an input field without having to submit the form

Currently, I am looking for a way to allow the user to select a file and then store the path location in a JavaScript string. After validation, I plan to make an AJAX call to the server using PHP to upload the file without submitting the form directly. Thi ...

Firefox fails to render SVG animation

Currently, I'm attempting to incorporate this unique animation into my website: http://codepen.io/dbj/full/epXEyd I've implemented the following JavaScript code in order to achieve the desired effect: var tl = new TimelineLite; tl.staggerFromTo ...

In IE9, users can select background elements by clicking on specifically positioned links

Trying to turn an li element into a clickable link by overlaying it with an a element set to 100% height and width. While this solution works in Chrome and FF, IE9 is causing issues as other elements behind the link remain selectable, rendering the link un ...

Issue with Bootstrap checkbox buttons not rendering properly

I'm attempting to utilize the checkbox button feature outlined on the bootstrap webpage here in the "checkbox" subsection. I copied and pasted the html code (displayed below) from that page into a jsfiddle, and checkboxes are unexpectedly appearing in ...

How can I create a custom AppBar transition using Material-UI?

Is there a way to incorporate transitions into the AppBar element within Material-UI? I have tried adjusting the class properties, but unfortunately, I'm not seeing any animation. Can anyone pinpoint what the issue might be? To see the code in action ...

Error in CSS Header - Position Shifts when Hovered Over

UPDATED HEADER HTML/CSS I am currently working on the header section of my website at . I encountered a bug where, upon hovering over the links in the header from right to left, the positions of the links become disorganized. I am unsure of what might be ...

Switch up the positioning of elements using jQuery

My webpage contains multiple elements that are absolutely positioned with the CSS attribute "top." I am looking to adjust the top position of each element by 20px. This involves retrieving the current top position and adding 20px to it. The quantity of e ...

How can we use jQuery to change the format of a

I'm working on a project where I want the calendar to display the date that the user picks. Right now, my "{{date}}" is in the format 2016-01-11, but it keeps showing one day before the selected date. I would like it to display as mm/dd/yyyy. I belie ...

What is the best way to add padding or margins to the heading that is being shown as a table-cell?

After finding an example for a header with lines on the side (source here, specifically: here), I've managed to implement it successfully. However, I am facing a challenge when trying to add some padding to both sides of the text and having the lines ...

Is there a requirement to download and set up any software in order to utilize Highchart?

I've been working on a project to create a program that displays highcharts, but all I'm getting is a blank page. Here's my code: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charse ...