How come there is a varying gap between the fixed header and the main content when the height is set to 10%?

I've noticed a peculiar issue when using percentage values for the height of my fixed header and margin-top of the main div.

Everything works smoothly when I set the header height to 50px and margin-top of the main area to 50px.

However, when I change these values to 10%, the distance between the header and the main becomes inconsistent:

header {
  position: fixed;
  display: block;
  top: 0px;
  left: 0px;
  right: 0px;
  height: 10%;
  background-color: green;
}

main {
  margin-top: 10%;
  background-color: blue;
}
<header>
  My Header
</header>
<main>
  Main area here!
</main>

JSFiddle: https://jsfiddle.net/azizn/L7sroo4m/

Can anyone provide an explanation for this inconsistency?

Answer №1

Ensure that your HTML and body tags have specific height and width properties by adding the following code snippet:

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

Below is the complete CSS code:

html, body{
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}
header {
  position: fixed;
  display: block;
  top: 0px;
  left: 0px;
  right: 0px;
  height: 10%;
  width: 100%;
  background-color: green;
}
main {
  position: absolute;
  display: block;
  top: 10%;
  width: 100%;
  background-color: blue;
}    

I set main's position: absolute and used top instead of margin to prevent body overflow. Using margin would require setting body{height: 90%;}, but it's better practice to keep the header within the body element.

Answer №2

GCyrillus provided the clues that helped me answer this question:

The issue lies in the fact that the percentage value of height pertains to the parent's height, while the percentage value of margin-top relates to the parent's width.

The remedy is to steer clear of percentage values and instead utilize viewport units x vh, which are based on the height of the viewport.

In essence, all you need to do is swap 10% with 10vh in my demonstration and everything will function smoothly.

Here is an illustration of viewport units: http://www.quirksmode.org/css/units-values/viewport_small.html

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

Why won't my navbar shift to the right when using mr-auto?

I attempted to use mr-auto to shift the navbar to the right, but it doesn't seem to be working as expected. I'm a bit confused as to why. Can someone provide some insight? Below is the relevant code snippet: <nav class="navbar navbar-expa ...

Which method is more effective: embedding PHP processing code within HTML files, or having forms redirect to separate PHP scripts that ultimately point back to static pages?

I'm in the process of developing a new website, and I'm still learning about PHP. Would it be more advantageous to use .html pages where buttons with Onclick would trigger JavaScript functions that redirect to a PHP page for database interaction ...

Adjust the width of an HTML input using the Bootstrap 5 form-control class

Is it possible to set the width of an input using Bootstrap's form-control or input-group classes? In the example below, each input should have a width based on its maxlength attribute: <link href="https://cdn.jsdelivr.net/npm/<a href="/cd ...

What are some strategies for creating a website that caters to both web crawlers and users of single page applications?

Let's get down to the specifics. I've got a frontend website at (don't worry, it's not porn). If you have javascript enabled, the URL becomes [this uses jquery/ajax load]. If you're without javascript, the URL remains as . To ...

Designing in Ionic 3.x: Implementing Ion-Icon with a gradient backdrop (ion-chip)

Is there a way to add a gradient background to an icon? I attempted to nest the ion-icon within an ion-chip, like so: <ion-chip class="my-chip"> <ion-icon name="basket></ion-icon> </ion-chip Then, in the .css file: ...

Implementing CSS styles to Iframe content

I have written the following code snippet to display an iframe: <iframe scrolling='no' frameborder='1' id='fblike' src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.XYZ.com%2F&amp;send=false& ...

What is the rationale behind positioning the closing right angle-bracket of an HTML tag next to the opening left angle-bracket of a different HTML tag?

I came across a clever trick using this coding structure where the ending tag 'angle bracket' is placed at the beginning of a new left 'angle-bracket' tag opening. Unfortunately, I can't recall why it was done or where I saw it. I ...

How can I dynamically insert HTML content into a data-attribute using JavaScript?

I have been trying to insert a variable that includes HTML code into a DATA attribute (a href ... data-content= ...), but it isn't functioning properly. The code I input seems to delete certain characters and as a result, it does not display correctly ...

I need to implement a div-based dropdown with a scrollbar in JavaScript and CSS to prevent it from extending beyond the screen. Additionally, the use of struts is essential in this implementation

Dealing with a dynamically populated div-based dropdown can be tricky, especially when it extends beyond the screen's limits and hides entries. This is an inherited application that lacks support, leaving me to handle it without the necessary expertis ...

Concealing a button within a specific interface

I am currently facing an issue with a data table that includes two control buttons: edit and save. My problem lies in hiding the save button on the initial preview. Basically, what I want to achieve is displaying only the Edit button on the first page. Wh ...

Various lists do not appear directly under each other

Hello everyone, I recently created a webpage displaying different lists of football players. My goal is to keep the lists stacked vertically without any floats that would make them appear side by side. The separate lists are essential for properly categori ...

What is the best way to display content next to an image, as well as below the image once it finishes?

Currently, I am working on customizing my blog posts in WordPress. My goal is to have the content of each post displayed next to the post thumbnail, and once the image ends, the content should seamlessly flow underneath it from the left side. I have imple ...

Using importXML with Internet Explorer 11

My project website includes a roster page that retrieves XML data. Previously, this functionality worked across all browsers but now it only works in Chrome. In IE11, it seems that the importXML function is not functioning correctly as the roster data is m ...

Enhance your table layout with Bootstrap 3 by adjusting textarea size to fill

I am currently in the process of developing a forum and bulletin board software. I am facing some challenges while trying to integrate a reply section into the thread page. You can view what I have implemented so far here. Html: <div class="panel pane ...

Unable to initialize styles in a newly created Angular CLI project

Just finished setting up a new project with the angular cli. Encountering an issue when trying to link the styles.css file in index.html <link rel="stylesheet" type="text/css" href="styles.css"> Getting the following error in Chrome's dev too ...

Comprehending the Syntax of the ExtJS Framework

I have recently inherited a project utilizing Sencha's ExtJS framework without any handover or documentation. As I navigate through the code, I find myself trying to decipher certain syntax and exploring resources tailored for newcomers to this specif ...

What could be the reason for the malfunction of the min value in my Material UI date textfield?

I am attempting to set a minimum date for the picker to start from, but it does not seem to be working as expected. <TextField id="date" type="date" defaultValue="2017-05-24" minDate="24/01/2019" ...

Is it possible for a div to extend beyond the previous div's boundaries using CSS?

When viewing a website like GameFAQs, you may notice that there are two divs positioned next to each other - the main content and an aside (such as a poll). As you resize the window, the aside sometimes moves down below the main content without leaving any ...

Struggling with a clash of styles between the float property and the navigation style in HTML

When I apply the float:left style to my <ul>, why does the nav bar appear very large? Consider this example: <ul class="nav" id="ModuleContainer" runat="server" style="float:left"> <li>111111----------------------</li> < ...

Verify if the specified value is present in the dropdown using AngularJS

Utilizing AngularJS, I have implemented an input field with autocomplete functionality. The autocomplete feature pulls data from a JSON file and displays it in a dropdown table format. Users are able to filter the results and select a value from the dropdo ...