Make body have 100% of the browser height

2018-12-31 04:53发布

I want to make body have 100% of the browser height. Can I do that using CSS?

I tried setting height: 100%, but it doesn't work.

I want to set a background color for a page to fill the entire browser window, but if the page has little content I get a ugly white bar at the bottom.

标签: html css height
21条回答
墨雨无痕
2楼-- · 2018-12-31 05:33

Try adding:

body {
height: 100vh;
}
查看更多
宁负流年不负卿
3楼-- · 2018-12-31 05:36

Here:

html,body{
    height:100%;
}

body{
  margin:0;
  padding:0
  background:blue;
}
查看更多
听够珍惜
4楼-- · 2018-12-31 05:36

Please check this:

* {margin: 0; padding: 0;}    
html, body { width: 100%; height: 100%;}

Or try new method Viewport height :

html, body { width: 100vw; height: 100vh;}

Viewport: If your using viewport means whatever size screen content will come full height fo the screen.

查看更多
素衣白纱
5楼-- · 2018-12-31 05:37

Here Update

html
  {
    height:100%;
  }

body{
     min-height: 100%;
     position:absolute;
     margin:0;
     padding:0; 
}
查看更多
十年一品温如言
6楼-- · 2018-12-31 05:38
html {
    background: url(images/bg.jpg) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    min-height: 100%;
}
html body {
    min-height: 100%
}

Works for all major browsers: FF, Chrome, Opera, IE9+. Works with Background images and gradients. Scrollbars are available as content needs.

查看更多
临风纵饮
7楼-- · 2018-12-31 05:42

If you have a background image then you will want to set this instead:

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

This ensures that your body tag is allowed to continue growing when the content is taller than the viewport and that the background image continues to repeat/scroll/whatever when you start scrolling down.

Remember if you have to support IE6 you will need to find a way to wedge in height:100% for body, IE6 treats height like min-height anyway.

查看更多
登录 后发表回答