Reputation: 46539
How come there is a vertical scrollbar visible with this HTML file?
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
html, body {border:0; margin:0; padding:0; height:100%}
h1 {margin:1em}
</style>
</head>
<body>
<h1>Test</h1>
</body>
</html>
(or this jsFiddle)
The body has exactly the same height as the window, obviously. So, what makes the content larger? What am I missing?
(Tested with IE, FF, Chrome.)
Upvotes: 3
Views: 1157
Reputation: 26937
This behaviour is called "collapsing margins". Since your h1
element is the first child element of body
, it tries to collapse margin with it and the margin of h1
ends up at the body
tag. Since body
is already 100% the added margin will make it larger than the viewport.
Read more on collapsing margins on w3c
Upvotes: 3
Reputation: 9037
The margin on your h1 element is expanding the content of your body which is already 100% of the height. Exceeding 100% of the height is causing the scroll.
Upvotes: 2
Reputation: 490143
Your h1
element's top margin
is being calculated from the top of the document.
You could use padding
in this example instead.
Upvotes: 1
Reputation: 7693
remove height: 100%
or add overflow: hidden
. But be careful - 100% means only 100% of your initial screen.
Upvotes: 1
Reputation: 2113
use this:
html {border:0; margin:0; padding:0; height:100%}
body {border:0; margin:0; padding:0}
Upvotes: 1