Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making html and body stretch to 100% height and more if the page scrolls

Tags:

html

css

I want to make my <html> and <body> tags be the entire height of the viewport if the page isn't tall enough to warrant scrolling, or the entire height of the web page if it does.

At the moment I'm using this CSS:

html, body {
    height: 100%;
}

Which works a treat if the web page isn't tall enough for the user to scroll, however on a long webpage it only goes to the height of the viewport on the users browser. I also tried:

html, body {
    min-height: 100%;
}

but that just seems to have the same effect as having no height declaration at all.

like image 790
James Dawson Avatar asked Jan 04 '13 05:01

James Dawson


2 Answers

Have you tried:

min-height: 100vh;

That should set the height of the elements to the viewport and if they go beyond, it will have a scrollbar, here is an example:

http://codepen.io/r3plica/pen/jBaPYB

like image 200
r3plica Avatar answered Sep 21 '22 16:09

r3plica


It sounds like your goal is for the body to

  • always cover the screen (with minimal or no content)
  • be able to stretch (if there is a ton of content)

if that's the case the following snippet should help

/* this looks like it should work

       html, body{
         min-height: 100%;
       }

    but this ↓ does the trick */

html, body{
  padding: 0;
  margin: 0;
}
html{
  height: 100%;
}
body{
  min-height: 100%;
}
like image 28
user3276552 Avatar answered Sep 21 '22 16:09

user3276552