Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Body height 100% displaying vertical scrollbar

Tags:

html

css

Out of curiosity, considering the example below, why does having the margin on the #container div cause a vertical scrollbar to appear in the browser? The container is much smaller in height than the body height which is set to 100%.

I have set the padding and margins to 0 for all elements except the #container. Note that I have deliberately omitted absolute positioning on the #container div. In this case how is the browser calculating the height of the body and how is the margin affecting it?

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* { padding:0; margin:0;}
html, body { height:100%; }
#container
{
  padding:10px;
  margin:50px;
  border:1px solid black;
  width: 200px;
  height: 100px;
}
</style>
</head>
<body>
  <div id='container'>
  </div>
</body>
</html>

Example also on JSFiddle

like image 545
neodymium Avatar asked Oct 20 '12 15:10

neodymium


4 Answers

If you paint the backgrounds of html and body (giving each its own color), you'll quickly notice that body is being shifted down along with #container, and #container itself isn't offset from the top of body at all. This is a side effect of margin collapse, which I cover in detail here (although that answer describes a slightly different setup).

It's this behavior that's causing the scrollbar to appear, since you've declared body to have 100% the height of html. Note that the actual height of body is unaffected, as margins are never included in height calculations.

like image 50
BoltClock Avatar answered Nov 15 '22 12:11

BoltClock


Based upon @BoltClock♦'s answer, I fixed it by zeroing the margin...
so

html,body, #st-full-pg {
   height: 100%;
   margin: 0;
}

works where id "st-full-pg" is assigned to a panel div (which further contained panel-heading and panel-body)

like image 34
gawkface Avatar answered Nov 15 '22 12:11

gawkface


A bit late, but maybe it helps someone.

Adding float: left; to #container removes the scrollbar, as W3C says:

•Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).

like image 24
Michel Avatar answered Nov 15 '22 12:11

Michel


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

This worked for me

like image 8
fjkjava Avatar answered Nov 15 '22 11:11

fjkjava