Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100% height goes a few pixels off the page?

Tags:

html

css

I'm trying to make a box 100% height of the page. But in both Chrome and IE, the following extends a few pixels off the bottom of the page so I have to scroll. Why? Why is there a scrollbar here?

  <!doctype html>
  <html >
  <head>
    <style type="text/css">
        html, body 
        {
            margin: 0px;
            padding: 0px;
            height: 100%; 
        }
        div {
            border:5px solid black;
            height: 100%;
        }
    </style>
  </head>
  <body >
    <div >This flows a few pixels off the bottom of the page</div>
  </body>
</html>
like image 816
user2543623 Avatar asked Feb 09 '23 02:02

user2543623


1 Answers

It goes a few pixels off the page because you're including a 5px border. The body of the div is 100% the height of the page, but the border sits outside of that, adding 10px total height to the page alongside the 100% height. So, on a 1000px page the height of your div will be 1010px. Remove the border and it'll be exactly the right height.

div {
    height: 100%;
}

If you still want the border, but not the unwanted extra height you can use the box-sizing: border-box property to place it inside the boundaries of the div

div {
    height: 100%;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
like image 142
asdf Avatar answered Feb 11 '23 16:02

asdf