How can an html element fill out 100% of the remaining screen height, using css only?

后端 未结 17 1688
名媛妹妹
名媛妹妹 2020-12-22 16:13

I have a header element and a content element:

#header
#content

I want the header to be of fixed height and the content to fill up all the

相关标签:
17条回答
  • 2020-12-22 17:10

    Very simple:

    #content {
        max-height: 100%;
    }
    
    0 讨论(0)
  • 2020-12-22 17:12

    forget all the answers, this line of CSS worked for me in 2 seconds :

    height:100vh;
    

    1vh = 1% of browser screen height

    source

    For responsive layout scaling, you might want to use :

    min-height: 100vh
    

    [update november 2018] As mentionned in the comments, using the min-height might avoid having issues on reponsive designs

    [update april 2018] As mentioned in the comments, back in 2011 when the question was asked, not all browsers supported the viewport units. The other answers were the solutions back then -- vmax is still not supported in IE, so this might not be the best solution for all yet.

    0 讨论(0)
  • 2020-12-22 17:13

    Unless you need to support IE 9 and below, I would use flexbox

    body { display: flex; flex-direction: column; }
    .header { height: 70px; }
    .content { flex: 1 1 0 }
    

    You also need to get body to fill the whole page

    body, html{ width:100%; height:100%; padding: 0; margin: 0;}
    
    0 讨论(0)
  • 2020-12-22 17:14

    The best solution I found so far is setting a footer element at the bottom of the page and then evaluate the difference of the offset of the footer and the element we need to expand. e.g.

    The html file

    <div id="contents"></div>
    <div id="footer"></div>
    

    The css file

    #footer {
        position: fixed;
        bottom: 0;
        width: 100%;
    }
    

    The js file (using jquery)

    var contents = $('#contents'); 
    var footer = $('#footer');
    contents.css('height', (footer.offset().top - contents.offset().top) + 'px');
    

    You might also like to update the height of the contents element on each window resize, so...

    $(window).on('resize', function() {
      contents.css('height', (footer.offset().top -contents.offset().top) + 'px');
    });
    
    0 讨论(0)
  • 2020-12-22 17:16
    #Header
    {
    width: 960px;
    height: 150px;
    }
    
    #Content
    {
    min-height:100vh;
    height: 100%;
    width: 960px;
    }
    
    0 讨论(0)
提交回复
热议问题