Make body have 100% of the browser height

后端 未结 21 1343
离开以前
离开以前 2020-11-22 00:26

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

相关标签:
21条回答
  • 2020-11-22 00:30

    I would use this

    html, body{
          background: #E73;
          min-height: 100%;
          min-height: 100vh;
          overflow: auto; // <- this is needed when you resize the screen
        }
    <html>
        <body>
        </body>
    </html>

    The browser will use min-height: 100vh and if somehow the browser is a little older the min-height: 100% will be the fallback.

    The overflow: auto is necessary if you want the body and html to expand their height when you resize the screen (to a mobile size for example)

    0 讨论(0)
  • 2020-11-22 00:31

    What I use on the start of literally every CSS file I use is the following:

    html, body{
        margin: 0;
    
        padding: 0;
    
        min-width: 100%;
        width: 100%;
        max-width: 100%;
    
        min-height: 100%;
        height: 100%;
        max-height: 100%;
    }
    

    The margin of 0 ensures that the HTML and BODY elements aren't being auto-positioned by the browser to have some space to the left or right of them.

    The padding of 0 ensures that the HTML and BODY elements aren't automatically pushing everything inside them down or right because of browser defaults.

    The width and height variants are set to 100% to ensure that the browser doesn't resize them in anticipation of actually having an auto-set margin or padding, with min and max set just in case some weird, unexplainable stuff happens, though you probably dont need them.

    This solution also means that, like I did when I first started on HTML and CSS several years ago, you won't have to give your first <div> a margin:-8px; to make it fit in the corner of the browser window.


    Before I posted, I looked at my other fullscreen CSS project and found that all I used there was just body{margin:0;} and nothing else, which has worked fine over the 2 years I've been working on it.

    Hope this detailed answer helps, and I feel your pain. In my eyes, it is dumb that browsers should set an invisible boundary on the left and sometimes top side of the body/html elements.

    0 讨论(0)
  • 2020-11-22 00:32

    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.

    0 讨论(0)
  • 2020-11-22 00:33

    After testing various scenarios, I believe this is the best solution:

    html {
        width: 100%;
        height: 100%;
        display: table;
    }
    
    body {
        width: 100%;
        display: table-cell;
    }
    
    html, body {
        margin: 0px;
        padding: 0px;
    }
    

    It is dynamic in that the html and the body elements will expand automatically if their contents overflow. I tested this in the latest version of Firefox, Chrome, and IE 11.

    See the full fiddle here (for you table haters out there, you can always change it to use a div):

    https://jsfiddle.net/71yp4rh1/9/


    With that being said, there are several issues with the answers posted here.

    html, body {
        height: 100%;
    }
    

    Using the above CSS will cause the html and the body element to NOT automatically expand if their contents overflow as shown here:

    https://jsfiddle.net/9vyy620m/4/

    As you scroll, notice the repeating background? This is happening because the body element's height has NOT increased due to its child table overflowing. Why doesn't it expand like any other block element? I'm not sure. I think browsers handle this incorrectly.

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

    Setting a min-height of 100% on the body as shown above causes other problems. If you do this, you cannot specify that a child div or table take up a percentage height as shown here:

    https://jsfiddle.net/aq74v2v7/4/

    Hope this helps someone. I think browsers are handling this incorrectly. I would expect the body's height to automatically adjust growing larger if its children overflow. However, that doesn't seem to happen when you use 100% height and 100% width.

    0 讨论(0)
  • 2020-11-22 00:35

    Try setting the height of the html element to 100% as well.

    html, 
    body {
        height: 100%;
    }
    

    Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well.

    However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.

    html {
      height: 100%;
    }
    body {
      min-height: 100%;
    }
    
    0 讨论(0)
  • 2020-11-22 00:35

    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.

    0 讨论(0)
提交回复
热议问题