Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div with dynamic min-height based on browser window height

Tags:

css

viewport

I have three div elements: one as a header, one as a footer, and a center content div.
the div in the center needs to expand automatically with content, but I would like a min-height such that the bottom div always at least reaches the bottom of the window, but is not fixed there on longer pages.

For example:

<div id="a" style="height: 200px;">   <p>This div should always remain at the top of the page content and should scroll with it.</p> </div> <div id="b">   <p>This is the div in question. On longer pages, this div needs to behave normally (i.e. expand to fit the content and scroll with the entire page). On shorter pages, this div needs to expand beyond its content to a height such that div c will reach the bottom of the viewport, regardless of monitor resolution or window size. </div> <div id="c" style="height: 100px;">   <p>This div needs to remain at the bottom of the page's content, and scroll with it on longer pages, but on shorter pages, needs to reach the bottom of the browser window, regardless of monitor resolution or window size.</p> </div> 
like image 428
Bryan Avatar asked Sep 23 '11 09:09

Bryan


People also ask

How can we get dynamic height of div?

We use css property height: calc( 100% - div_height ); Here, Calc is a function. It uses mathematical expression by this property we can set the height content div area dynamically.

How set dynamic width and height in CSS?

Top header with 100% width and 50px height. Left navigation bar with 200px width and dynamic height to fill the screen. A container on the right of the nav bar and under the header with dynamic width and height to fill the screen.

What is min height 100vh?

Min height 100vh means the element should occupy the web browser viewport height. This is always 100 percent of the web browser's viewport height. If there is more content, the element will stretch more than the viewport's height, which is a code example that will clear things up.


1 Answers

Just look for my solution on jsfiddle, it is based on csslayout

html,  body {    margin: 0;    padding: 0;    height: 100%; /* needed for container min-height */  }  div#container {    position: relative; /* needed for footer positioning*/    height: auto !important; /* real browsers */    min-height: 100%; /* real browsers */  }  div#header {    padding: 1em;    background: #efe;  }  div#content {    /* padding:1em 1em 5em; *//* bottom padding for footer */  }  div#footer {    position: absolute;    width: 100%;    bottom: 0; /* stick to bottom */    background: #ddd;  }
<div id="container">      <div id="header">header</div>      <div id="content">      content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>content<br/>    </div>      <div id="footer">      footer    </div>  </div>
like image 111
huston007 Avatar answered Oct 06 '22 00:10

huston007