How can I make a div auto-adjust its height between two divs, according to window height using CSS/Javascript?

旧街凉风 提交于 2019-12-05 18:39:53

Assuming you are doing this because you want a footer that is flushed to the bottom of the page, then this will achieve a similar effect: http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

However solution does not resize the middle div but merely positions the footer over it and then use padding to prevent the contents of the middle div from going onto the footer.

If you want to actually change the size of the middle div, here's the JavaScript for it using jQuery: http://jsfiddle.net/BnJxE/

JavaScript

var minHeight = 30; // Define a minimum height for the middle div

var resizeMiddle = function() {
    var h = $('body').height() - $('#header').height() - $('#footer').height();
    h = h > minHeight ? h : minHeight;
    $('#body').height(h);
}

$(document).ready(resizeMiddle);
$(window).resize(resizeMiddle);

HTML

<div id="container">
   <div id="header"></div>
   <div id="body"></div>
   <div id="footer"></div>
</div>

CSS

html,
body {
   margin:0;
   padding:0;
   height: 100%;
}

#header {
   background:#ff0;
   height: 100px;
}
#body {
   background: #aaa;
}
#footer {
   height: 60px;
   background:#6cf;
}

The correct way of ending <div> tag is by using </div>.

Try this code-

<body>
  <div style="position: fixed; top: 0px; left: 0px; width: 200px; height: 100%;">
    <div style="float: left; height: 50px; width: 200px; background-color: green; position: fixed;"></div>
    <div style="float: left; height: 100%; width: 200px; background-color: red;"></div> 
    <div style="float: left; bottom: 0px; left:0px; position: fixed; height: 50px; width: 200px; background-color: blue;"></div>
  </div>
</body>

HTML:

<body>
    <div id="wrapper">
        <div id="div1">...</div>
        <div id="div2">...</div>
        <div id="div3">
            content<br/>
            content<br/>
        </div>
    </div>
</body>

CSS:

html, body {
    height:100%;
}
#wrapper {
    position:relative;
    height:100%;
}
#div3 {
    background:pink;
    bottom:0;
    position:absolute;
    width:100%;
}

jsfiddle: http://jsfiddle.net/BnJxE/

You can use paddings the same height of your top and bottom elements and set box-sizing to border-box. By setting the height to 100% it will cover the entire height minus the paddings.

JSFiddle with scrollable content

#container {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 200px;
  height: 100%;
}
#top {
  float: left;
  height: 50px;
  width: 200px;
  background-color: green;
  position: fixed;
  top: 0;
}
#middle {
  float: left;
  height: 100%;
  width: 200px;
  background-color: red;
  box-sizing: border-box;
  padding-bottom: 50px;
  padding-top: 50px;
}
#bottom {
  float: left;
  height: 50px;
  width: 200px;
  background-color: blue;
  position: fixed;
  bottom: 0;
}
<body>
  <div id="container">
    <div id="top"></div>
    <div id="middle"></div> 
    <div id="bottom"></div>
  </div>
</body>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!