Make body have 100% of the browser height

后端 未结 21 1387
离开以前
离开以前 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:43

    As an alternative to setting both the html and body element's heights to 100%, you could also use viewport-percentage lengths.

    5.1.2. Viewport-percentage lengths: the ‘vw’, ‘vh’, ‘vmin’, ‘vmax’ units

    The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly.

    In this instance, you could use the value 100vh - which is the height of the viewport.

    Example Here

    body {
      height: 100vh;
      padding: 0;
    }
    
    body {
      min-height: 100vh;
      padding: 0;
    }
    

    This is supported in most modern browsers - support can be found here.

提交回复
热议问题