Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<html> and <body> Tags 100% Height Fail

I have an application which requires me to use CSS to set the height of the <html> and <body> tags to 100%, like this (JSFiddle version):

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

body {
  background-color: #141414;
  overflow-x: hidden;
  overflow-y: auto;
}

I've included all of the CSS that styles these tags. However, I always have a scrollbar which allows me to scroll down about 20px, regardless of how large or small the content of the page really is. This is problem also persists on screes of any size.

I can see why in these screenshots. Note that the body has an ID of #container, but this is simply used for JS targeting, not CSS styling:

enter image description here

A 3D perspective of the same page, showing the <body> tag is slid down from the top of the <html> tag:

enter image description here

Since I don't have any margin or padding on any of these tags, and since none of the HTML elements near the top of the page have any margin or padding to push the <body> tag downward, I'm really stuck.

How can I fix the CSS such that the <body> tag is flush with the top of the page?

Thank you for your time.

like image 866
Oliver Spryn Avatar asked Aug 18 '12 17:08

Oliver Spryn


People also ask

Why is height 100% not working HTML?

Setting min-height to 100% on both elements does not allow the body element to fill the page like you might expect. If you check the computed style values in dev tools, the body element has a height of zero. Meanwhile, the HTML element has a height equal to the visible part of the page in the browser.

What does height of 100% mean in HTML?

It just means 100% of the div or class or tag it is enclosed within.


2 Answers

Try

header {
    margin: 0px 17% 0px 17%;
}
like image 128
code-jaff Avatar answered Oct 19 '22 02:10

code-jaff


The most likely cause is collapsing margins. You can "fix" this by adding padding: 0.01px to your body.

Hopefully my second attempt at answering this question isn't as stupid as my first XD

like image 42
Niet the Dark Absol Avatar answered Oct 19 '22 04:10

Niet the Dark Absol