Set div height equal to screen size

后端 未结 6 1604
情深已故
情深已故 2020-11-30 02:10

I have a div element in twitter-bootstrap which will have content that will overflow vertically outside the screen.

I would like the div to take the height of the si

相关标签:
6条回答
  • 2020-11-30 02:53

    try this

    $(document).ready(function(){
        $('#content').height($(window).height());
    });
    
    0 讨论(0)
  • 2020-11-30 02:56

    Using CSS {height: 100%;} matches the height of the parent. This could be anything, meaning smaller or bigger than the screen. Using {height: 100vh;} matches the height of the viewport.

    .container {
        height: 100vh;
        overflow: auto;
    }
    

    According to Mozilla's official documents, 1vh is:

    Equal to 1% of the height of the viewport's initial containing block.

    0 讨论(0)
  • 2020-11-30 02:57

    You need to give height for the parent element too! Check out this fiddle.

    CSS:

    html, body {height: 100%;}
    
    #content, .container-fluid, .span9
    {
        border: 1px solid #000;
        overflow-y:auto;
        height:100%;
    }​
    

    JavaScript (using jQuery) Way:

    $(document).ready(function(){
        $(window).resize(function(){
            $(".fullheight").height($(document).height());
        });
    });
    
    0 讨论(0)
  • 2020-11-30 02:58

    Use simple CSS height: 100%; matches the height of the parent and using height: 100vh matches the height of the viewport.

    Use vh instead of %;

    0 讨论(0)
  • 2020-11-30 03:01

    This worked for me JsFiddle

    Html

    ..bootstrap
    <div class="row">
      <div class="col-4 window-full" style="background-color:green">
        First Col
      </div>
      <div class="col-8">
        Column-8
      </div>
    </div>
    

    css

    .row {
       background: #f8f9fa;
       margin-top: 20px;
    }
    
     .col {
       border: solid 1px #6c757d;
       padding: 10px;
    }
    

    JavaScript

    var elements = document.getElementsByClassName('window-full');
    var windowheight = window.innerHeight + "px";
    
    fullheight(elements);
    function fullheight(elements) {
        for(let el in elements){
            if(elements.hasOwnProperty(el)){
                elements[el].style.height = windowheight;
            }
        }
    }
    
    window.onresize = function(event){
         fullheight(elements);
    }
    

    Checkout JsFiddle link JsFiddle

    0 讨论(0)
  • 2020-11-30 03:02

    use

     $(document).height()
    property and set to the div from script and set

      overflow=auto 

    for scrolling

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