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

后端 未结 17 1889
名媛妹妹
名媛妹妹 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条回答
  •  猫巷女王i
    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

    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');
    });
    

提交回复
热议问题