Make body have 100% of the browser height

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

    Here Update

    html
      {
        height:100%;
      }
    
    body{
         min-height: 100%;
         position:absolute;
         margin:0;
         padding:0; 
    }
    
    0 讨论(0)
  • 2020-11-22 00:39

    Here:

    html,body{
        height:100%;
    }
    
    body{
      margin:0;
      padding:0
      background:blue;
    }
    
    0 讨论(0)
  • 2020-11-22 00:40
    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.

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

    CSS3 has a new method.

     height:100vh
    

    It makes ViewPort 100% equal to the height.

    So your Code should be

     body{
     height:100vh; 
     }
    
    0 讨论(0)
  • 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.

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

    all answers are 100% correct and well explained, but i did something good and very simple to make it responsive.

    here the element will take 100% height of view port but when it comes to mobile view it don't look good specially on portrait view ( mobile ), so when view port is getting smaller the element will collapse and overlap on each other. so to make it little responsive here is code. hope someone will get help from this.

    <style>
    .img_wrap{
          width:100%;
          background: #777;
    }
    .img_wrap img{
          display: block;  
          width: 100px;
          height: 100px;
          padding: 50px 0px;
          margin: 0 auto;
    }
    .img_wrap img:nth-child(2){
          padding-top: 0;
    }
    </style>
    
    <div class="img_wrap">
      <img src="https://i.pinimg.com/originals/71/84/fc/7184fc63db0516a00e7d86900d957925.jpg" alt="">
      <img src="https://i.pinimg.com/originals/71/84/fc/7184fc63db0516a00e7d86900d957925.jpg" alt="">
    </div>
    
    <script>
       var windowHeight = $(window).height();
       var elementHeight = $('.img_wrap').height();
    
       if( elementHeight > windowHeight ){
           $('.img_wrap').css({height:elementHeight});
       }else{
           $('.img_wrap').css({height:windowHeight});
       }
    </script>
    

    here is JSfiddle Demo.

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