100% Min Height CSS layout - html

What's the best way to make an element of 100% minimum height across a
wide range of browsers ?
In particular if you have a layout with a header and footer of fixed height,
how do you make the middle content part fill 100% of the space in between with the footer fixed to the bottom ?

I am using the following one: CSS Layout - 100 % height
Min-height
The #container element of this page has a min-height of 100%. That
way, if the content requires more height than the viewport provides,
the height of #content forces #container to become longer as well.
Possible columns in #content can then be visualised with a background
image on #container; divs are not table cells, and you don't need (or
want) the physical elements to create such a visual effect. If you're
not yet convinced; think wobbly lines and gradients instead of
straight lines and simple color schemes.
Relative positioning
Because #container has a relative position, #footer will always remain
at its bottom; since the min-height mentioned above does not prevent
#container from scaling, this will work even if (or rather especially when) #content forces #container to become longer.
Padding-bottom
Since it is no longer in the normal flow, padding-bottom of #content
now provides the space for the absolute #footer. This padding is
included in the scrolled height by default, so that the footer will
never overlap the above content.
Scale the text size a bit or resize your browser window to test this
layout.
html,body {
margin:0;
padding:0;
height:100%; /* needed for container min-height */
background:gray;
font-family:arial,sans-serif;
font-size:small;
color:#666;
}
h1 {
font:1.5em georgia,serif;
margin:0.5em 0;
}
h2 {
font:1.25em georgia,serif;
margin:0 0 0.5em;
}
h1, h2, a {
color:orange;
}
p {
line-height:1.5;
margin:0 0 1em;
}
div#container {
position:relative; /* needed for footer positioning*/
margin:0 auto; /* center, not in IE5 */
width:750px;
background:#f0f0f0;
height:auto !important; /* real browsers */
height:100%; /* IE6: treaded as min-height*/
min-height:100%; /* real browsers */
}
div#header {
padding:1em;
background:#ddd url("../csslayout.gif") 98% 10px no-repeat;
border-bottom:6px double gray;
}
div#header p {
font-style:italic;
font-size:1.1em;
margin:0;
}
div#content {
padding:1em 1em 5em; /* bottom padding for footer */
}
div#content p {
text-align:justify;
padding:0 1em;
}
div#footer {
position:absolute;
width:100%;
bottom:0; /* stick to bottom */
background:#ddd;
border-top:6px double gray;
}
div#footer p {
padding:1em;
margin:0;
}
Works fine for me.

To set a custom height locked to somewhere:
body, html {
height: 100%;
}
#outerbox {
width: 100%;
position: absolute; /* to place it somewhere on the screen */
top: 130px; /* free space at top */
bottom: 0; /* makes it lock to the bottom */
}
#innerbox {
width: 100%;
position: absolute;
min-height: 100% !important; /* browser fill */
height: auto; /*content fill */
}
<div id="outerbox">
<div id="innerbox"></div>
</div>

Here is another solution based on vh, or viewpoint height, for details visit CSS units. It is based on this solution, which uses flex instead.
* {
/* personal preference */
margin: 0;
padding: 0;
}
html {
/* make sure we use up the whole viewport */
width: 100%;
min-height: 100vh;
/* for debugging, a red background lets us see any seams */
background-color: red;
}
body {
/* make sure we use the full width but allow for more height */
width: 100%;
min-height: 100vh; /* this helps with the sticky footer */
}
main {
/* for debugging, a blue background lets us see the content */
background-color: skyblue;
min-height: calc(100vh - 2.5em); /* this leaves space for the sticky footer */
}
footer {
/* for debugging, a gray background lets us see the footer */
background-color: gray;
min-height:2.5em;
}
<main>
<p>This is the content. Resize the viewport vertically to see how the footer behaves.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
<p>This is the content.</p>
</main>
<footer>
<p>This is the footer. Resize the viewport horizontally to see how the height behaves when text wraps.</p>
<p>This is the footer.</p>
</footer>
The units are vw , vh, vmax, vmin. Basically, each unit is equal to 1% of viewport size. So, as the viewport changes, the browser computes that value and adjusts accordingly.
You may find more information here:
Specifically:
1vw (viewport width) = 1% of viewport width
1vh (viewport height) = 1% of viewport height
1vmin (viewport minimum) = 1vw or 1vh, whatever is smallest
1vmax (viewport minimum) = 1vw or 1vh, whatever is largest

kleolb02's answer looks pretty good. another way would be a combination of the sticky footer and the min-height hack

For min-height to work correctly with percentages, while inheriting it's parent node min-height, the trick would be to set the parent node height to 1px and then the child's min-height will work correctly.
Demo page

A pure CSS solution (#content { min-height: 100%; }) will work in a lot of cases, but not in all of them - especially IE6 and IE7.
Unfortunately, you will need to resort to a JavaScript solution in order to get the desired behavior.
This can be done by calculating the desired height for your content <div> and setting it as a CSS property in a function:
function resizeContent() {
var contentDiv = document.getElementById('content');
var headerDiv = document.getElementById('header');
// This may need to be done differently on IE than FF, but you get the idea.
var viewPortHeight = window.innerHeight - headerDiv.clientHeight;
contentDiv.style.height =
Math.max(viewportHeight, contentDiv.clientHeight) + 'px';
}
You can then set this function as a handler for onLoad and onResize events:
<body onload="resizeContent()" onresize="resizeContent()">
. . .
</body>

I agree with Levik as the parent container is set to 100% if you have sidebars and want them to fill the space to meet up with the footer you cannot set them to 100% because they will be 100 percent of the parent height as well which means that the footer ends up getting pushed down when using the clear function.
Think of it this way if your header is say 50px height and your footer is 50px height and the content is just autofitted to the remaining space say 100px for example and the page container is 100% of this value its height will be 200px. Then when you set the sidebar height to 100% it is then 200px even though it is supposed to fit snug in between the header and footer. Instead it ends up being 50px + 200px + 50px so the page is now 300px because the sidebars are set to the same height as the page container. There will be a big white space in the contents of the page.
I am using internet Explorer 9 and this is what I am getting as the effect when using this 100% method. I havent tried it in other browsers and I assume that it may work in some of the other options. but it will not be universal.

First you should create a div with id='footer' after your content div and then simply do this.
Your HTML should look like this:
<html>
<body>
<div id="content">
...
</div>
<div id="footer"></div>
</body>
</html>
And the CSS:
​html, body {
height: 100%;
}
#content {
height: 100%;
}
#footer {
clear: both;
}

Try this:
body{ height: 100%; }
#content {
min-height: 500px;
height: 100%;
}
#footer {
height: 100px;
clear: both !important;
}
The div element below the content div must have clear:both.

Probably the shortest solution (works only in modern browsers)
This small piece of CSS makes "the middle content part fill 100% of the space in between with the footer fixed to the bottom":
html, body { height: 100%; }
your_container { min-height: calc(100% - height_of_your_footer); }
the only requirement is that you need to have a fixed height footer.
For example for this layout:
<html><head></head><body>
<main> your main content </main>
</footer> your footer content </footer>
</body></html>
you need this CSS:
html, body { height: 100%; }
main { min-height: calc(100% - 2em); }
footer { height: 2em; }

You can try this: http://www.monkey-business.biz/88/horizontal-zentriertes-100-hohe-css-layout/
That's 100% height and horizontal center.

just share what i've been used, and works nicely
#content{
height: auto;
min-height:350px;
}

As mentioned in Afshin Mehrabani's answer, you should set body and html's height to 100%, but to get the footer there, calculate the height of the wrapper:
#pagewrapper{
/* Firefox */
height: -moz-calc(100% - 100px); /*assume i.e. your header above the wrapper is 80 and the footer bellow is 20*/
/* WebKit */
height: -webkit-calc(100% - 100px);
/* Opera */
height: -o-calc(100% - 100px);
/* Standard */
height: calc(100% - 100px);
}

As specified the height property in MDN docs it is not inherited. So you need it to set to 100%. As it is known any web page starts with html then body as its child, you need to set height: 100% in both of them.
html,
body {
height: 100%;
}
<html>
<body>
</body>
</html>
And any child marked with height 100% will be the portion of 100 of parent height. In the example style above, we set the html tag as a 100%, it will be full height of the available screen height. Then the body is 100%, and so it will also be the full height, since it's parent is html.

Related

Layout with header and footer and content without overlapping

I would like to create a layout for a webpage with the following conditions :
A header div that sticks to the top of the browser of a defined height.
A footer div that sticks to the bottom of the browser of a defined height.
A main div that fills all the space between the header and the footer.
The 3 parts shall not overlap when the height of the browser is reduced to lesser than the height of the footer and the header and the content of the main div.
If the height of the browser is reduced to lesser than that, scrollbars should appear for the whole document, not just for the main content.
In other words and with numerical values :
Let's assume the header and the footer are 100 px each and the browser height which is of course variable is 800 px; I want the main div which, lets suppose, has a content that takes only 200px to occupy the whole remaining 600px.
When the browser is reduced to a height lesser than 100px (header) + 100px (footer) + 200px (content of main div) = 400px; I don't want the three parts to overlap and I wand scrollbars to appear for the whole document not just the main content.
Is this achievable with only HTML and CSS and without using flexboxes nor javascript ?
Here is the sample code (snippet) :
* {
margin: 0;
padding: 0;
}
body{
}
#container {
min-height:100vh;
position:relative;
}
#header {
background-color : red;
height : 100px;
width:100%;
}
#main {
background-color : blue;
width:100%;
}
#footer {
position:absolute;
bottom:0;
background-color : yellow;
height : 100px;
width:100%;
}
<div id="container">
<div id="header">I'm a header that gets overlapped by the footer when the browser height is reduced</div>
<div id="main">I'm a main who refuses to stretch and fill the remaining white space and which is overlapped by the footer when the browser height is reduced</div>
<div id="footer">I'm a footer and I overlap all the other divs when the height of the browser is reduced</div>
</div>
You should be able to achieve this with a combination of overflow for the parent and using calc() for the height of main. Try the snippet below and play around with the height of the container. I would suggest to also give a min-height to main, so that it doesn't collapse entirely, but that depends on your needs.
In general, however, I think flex is the cleaner solution, see the other answer(s).
#container {
overflow: auto; /* Show scrollbars if content larger than #container */
height: 320px;
}
header {
width: 100%;
height: 100px; /* Absolute height */
background-color: red;
}
main {
width: 100%;
height: calc(100% - 200px); /* Dynamically calculated height */
overflow: hidden;
background-color: blue;
}
footer {
width: 100%;
height: 100px; /* Absolute height */
background-color: yellow
}
<div id="container">
<header>I'm a header that gets overlapped by the footer when the browser height is reduced</header>
<main>I'm a main who refuses to stretch and fill the remaining white space and which is overlapped by the footer when the browser height is reduced</main>
<footer>I'm a footer and I overlap all the other divs when the height of the browser is reduced</footer>
</div>
Also note that HTML5 gives you the actual elements header, main and footer, so you should use these in favor of divs.

How to make fixed page template?

i want do fixed page height without scrolling, header fixed on top, and fill center to the remainder of the page. Center area will be have own scrolling bar when content overflow.
I set height 100% for central div, but this height ≠ height free space between top and bottom blocks.
What can i do? Many thanks!
Look code on jsfiddle
I have made a js fiddle which i have created with the points that i have understood from your problem.
I am using jquery $( window ).height() for getting browser viewport height.
By subtracting the height of header and footer (50px + 50px =100px) from browser viewport height i will get the height of extra space.
This height will be equal to the content height.
check the fiddle
http://jsfiddle.net/bnx4uuse/1/
html,body {height:100%; }
* { padding: 0; margin: 0; }
body { border:1px solid blue; }
#head {background-color:#FC6; height:50px;position:fixed;top:0px;width:100%;}
#center {background-color:#3CC; height:100%;margin:49px 0px; }
#foot {background-color:#9C0; height:50px;position:fixed;bottom:0px;width:100%;}
fiddle: http://jsfiddle.net/9xrz9mbf/4/
Add a wrapper div around your center div.
Set it to full screen and pad:
height: 100%;
width: 100%;
padding-top: 150px; /* size of your header */
padding-bottom: 100px; /* size of your footer */
On your center div you set overflow:
overflow: auto
The overflow property hides the extra content and adds a scrollbar to your div.
You header will have:
height: 150px; /* your desired size */
position: fixed; /* or absolute */
top: 0px;
Your footer will have:
height: 100px; /* your desired size */
position: fixed; /* or absolute */
bottom: 0px;
Then you have a "full-screen" app (full-window in that case)

How to make 2 divs float, left div with fixed right margin?

I actually don't know how to name my question. But I will explain what I need to do.
HTML is simple as this:
<div id="left_div"></div>
<div id="right_div"></div>
I need left_div to be on the left, to have 100% width, but with fixed right margin 320px. right_div has fixed width 300px and must be alongside left_div.
I know I can do this very easily, when I would do this:
<div id="right_div" style="float:right;width:300px"></div>
<div id="left_div" style="margin-right:320px;"></div>
But the problem is that I need HTML to be as I mentioned before. The order of DIVs matter. If someone wonders why, it's because I am working on responsive website, where I need, when the viewport is too narrow, the right_div to be below left_div. And that I can't do with simple solution I have put above.
I hope my question makes sense and I am thankful for any answers or helpful hints.
Oh, and I forgot to mention I need this to be pure HTML+CSS, no JS. And I don't need to support IE7 and below.
UPDATE:
left_div must be width:auto and right margin must be fixed (e.g. 300px).
If you want your layout to be responsive you should use a CSS framework like Columnal, 1140, or more in this list.
Most of these frameworks supports the grid system, which is the best way to structure your layout and you don't have to worry about floats and pixels anymore.
I think that what do you want is almost impossible with just pure HTML + CSS.
What may work for you is something like this one I did: http://jsfiddle.net/fmZAm/
HTML:
<div class="main">
<div class="left"></div>
<div class="right">
<div class="fixed_content"></div>
</div>
</div>
CSS:
div.main
{
min-height: 500px; /* force some height */
min-width: 300px; /* min width to show content */
text-align: center; /* center content when in vertical responsive mode */
font-size: 0px; /* remove blank space from 'inline-block' display */
}
div.main > div /* left and right divs */
{
width: 100%; /* force both to have as max width as possible */
min-height: inherit; /* same min height as parent */
min-width: inherit; /* same min width as parent to show content */
display: inline-block;
}
div.left
{
max-width: 58%; /* 100% width from max of 58% parent width */
background-color: lightgreen;
}
div.right
{
max-width: 42%; /* 100% width from max of 42% parent width */
text-align: right; /* put child 'inline-block' divs to the right */
background-color: dodgerblue;
}
div.right > div.fixed_content
{
width: 300px; /* set the 300px right div you want */
min-height: inherit; /* same min height as parent */
background: orange;
display: inline-block;
}
As both divs (left and right) will have % widths, both will resize based on the current max width, but you'll have your fixed width div inside of the right div. So, when your right div resize to 300px width (the fixed with of its child div) it will go below the left div.
Hope it helps!
I had the same issue, I solved it using position:absolute.
HTML
<div class="container">
<div id="left_div"></div>
<div id="right_div"></div>
</div>
CSS
.container {
position:relative;
}
#left_div {
float: left;
width: auto;
margin-right: 320px;
}
#right_div {
position:absolute;
top:0;
right:0;
width: 300px;
}

Body height 100% displaying vertical scrollbar

Out of curiosity, considering the example below, why does having the margin on the #container div cause a vertical scrollbar to appear in the browser? The container is much smaller in height than the body height which is set to 100%.
I have set the padding and margins to 0 for all elements except the #container. Note that I have deliberately omitted absolute positioning on the #container div. In this case how is the browser calculating the height of the body and how is the margin affecting it?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* { padding:0; margin:0;}
html, body { height:100%; }
#container
{
padding:10px;
margin:50px;
border:1px solid black;
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id='container'>
</div>
</body>
</html>
Example also on JSFiddle
If you paint the backgrounds of html and body (giving each its own color), you'll quickly notice that body is being shifted down along with #container, and #container itself isn't offset from the top of body at all. This is a side effect of margin collapse, which I cover in detail here (although that answer describes a slightly different setup).
It's this behavior that's causing the scrollbar to appear, since you've declared body to have 100% the height of html. Note that the actual height of body is unaffected, as margins are never included in height calculations.
Based upon #BoltClock♦'s answer, I fixed it by zeroing the margin...
so
html,body, #st-full-pg {
height: 100%;
margin: 0;
}
works where id "st-full-pg" is assigned to a panel div (which further contained panel-heading and panel-body)
A bit late, but maybe it helps someone.
Adding float: left; to #container removes the scrollbar, as W3C says:
•Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
html,body {
height: 100%;
margin: 0;
overflow: hidden;
}
This worked for me
adding float:left; is nice, but will interfere with central horizontal positioning using margin:auto;
if you know how big your margin is, you can account for that in your height percentage using calc:
height: calc(100% - 50px);
browser support is good, but only IE11+
https://caniuse.com/#feat=calc
/*removes default margin & padding*/
html, body{
padding: 0px !important;
margin: 0px !important;
}
/*sets body height to max; and allows scrollbar as page content grows*/
body{
min-height: 100vh;
}
I have found a solution: add padding: 1px 0; to body prevents vertical scrollbars to appear
For those who are coming here for an easier to understand answer that even includes code samples, this answer (copied from here) is for you.
No JavaScript or definite pixel values (such as 100px) are required, just, pure CSS and percentages.
If your div is just sitting there on its own, height: 50% will mean 50% the height of the body. Normally, the height of the body is zero without any visible content, so 50% of that is just, well, zero.
This is the solution (based on this) (uncomment the background lines to get a visualisation of the padding):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html
{
height: 100%;
/* background: green; */
}
body
{
/*
100% the height of <html> minus 1 multiple of the total extra height from the padding of <html>.
This prevents an unnecessary vertical scrollbar from appearing.
*/
height: calc(100% - 1em);
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS. */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
The above was written so that there would still be the usual padding. You could set the dimensions of the red div to 100% and still see padding on each side/end. If you don't want this padding, use this (although it doesn't look nice, I recommend you stick with the first example):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html, body
{
height: 100%;
}
/* You can uncomment it but you wouldn't be able to see it anyway. */
/*
html
{
background: green;
}
*/
body
{
margin: 0;
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
I saw this problem fixed before where you put all the contents of body in a div called wrap. Wrap's style should be set to position: relative; min-height: 100%;. To position #container div 50px from the top and left put a div inside wrap with a padding set to 50px. Margins will not work with wrap and the div we just made, but they will work in #container and everything inside it.
here's my fix on jsfiddle.
you can add non-breaking space into the body tag.
<body> <othertags>...</body>
html, body {
height: 100%;
overflow: hidden;
}
If you want to remove the body scrolling add the following style:
body {
height: 100%;
overflow: hidden;
}
Inspired by #BoltClock, I tried this and it worked, even when zoom out and in.
Browser: Chrome 51
html{
height: 100%;
}
body{
height: 100%;
margin: 0px;
position: relative;
top: -20px;
}
I guess body was shifted down 20px.
It works for me:
html,
body {
height: 100%;
height: -webkit-fill-available; // Chrome
}
// Firefox
#-moz-document url-prefix() {
body {
box-sizing: border-box;
margin: 0;
padding: 1px;
}
}
Add overflow: hidden; to html and body.
html, body {
height: 100%;
overflow: hidden;
}
I found a quick solution: try set height to 99.99% instead of 100%

proper css to ensure that the body element fills the entire screen

I have a problem with my body element. It seems that it is filling 100% percent of the screen. However, if you drag the browser small and then scroll down - the body doesn't extend.
Please see this jsFiddle as a prime example.
height: 100%; is the height of the window your site is displayed in not the height of the website, which causes the background getting purple when srolling down.
Just add this:
html { background: green; }
And remove the
body { background: green; }
to get the background to always be green. (JSFiddle)
I believe that THIS FIDDLE answers the question. I have been using this in production and it has been working great.
HTML:
<html>
<body>
<div class="main-wrapper contain">
<section class="main-content">
Main Content
</section> <!-- end .main-wrapper -->
<div class="other-thing">
Other thing for example.
</div>
</div> <!-- .main-wrapper -->
</body>
</html>
CSS:
/* hard reset */
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
position: relative;
padding: 0; margin: 0;
}
/* micro clear fix (clears floats) */
.contain:before,
.contain:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.contain:after {
clear: both;
}
.contain {
*zoom: 1;
}
html {
height: 100%; /* 01 */
/* tells html to go ahead and feel free to be 100% height */
}
body {
min-height: 100%; /* 02 */
/* expands to push html to the limit */
}
.main-wrapper {
/* overflow: hidden; */
/* in this case - forces wrapper to contain content (like a clear-fix) */
/* use clear fix instead */
}
/* if you see yellow - we are failing */
.main-content {
width: 100%;
float: left;
}
.other-thing {
width: 100%;
float: left;
}
I've tested this - and it seems to work in every situation, assuming that you keep all of your containers and stuff actually containing properly. There must be downfalls to this overflow: hidden; or people wouldn't use clear-fix. So - I would love hear more input.
Alternatively, I think that the html and body can be 100% and then the .main-wrapper can be min-height: 100%; and that works as well. Basically - something needs to force all of its containers to stretch. and in order to do that, all of those containers must be set to 100% so that they remember that they have that ability. Or am I anthropomorphizing the divs too much...
UPDATE 2021:
The nature of the web is to allow the content to define the 'shape' or the 'space' or whatever you want to call it... so - the body doesn't really know how 'tall' it is. It knows it's 100% width, because it's a block level element. So, unless you tell the HTML to be height: 100%, and then every child... then they wouldn't really know what "100%" really meant. 100% of what? For dashboard apps and desktop full-screen layouts you may want to set the hight (but not in most cases) - and using 100vh units is available now. General rule: just let the content decide the size of it's parent element and work with the nature of The Web. (ignore all that code up there! It's 2021: flex-box + grid! : )
Just remove the height: 100%; from your <body> and and also remove the height: 300px; from your <figure> and you are ready to go.
You can also use this code: http://jsfiddle.net/Asustaba/LBu8z/8/
1) If you want to have the body fill the whole screen, while solving 2 things simultaneously (due to the body having dynamic content)
not enough content: the body is at least as tall as the viewport, since your body doesn't have enough content to fill the screen
too much content: the body should be as tall as the html
Now you can use min-height:100vh for that, which means 100% of the viewport's height:
http://jsfiddle.net/LBu8z/89/
Except the Opera Mini it is supported by all browsers: caniuse.com/#search=vh
2) if you want to have a fixed background image, then I suggest to stretch a fixed position body:after
I needed this solution in production since a background-sizing:cover won't work properly with a fixed backround, thus I had to make the body:after fixed and the background image not fixed. You can check it here: https://www.doklist.com/
body:after{
content:"";
background:green;
position:fixed;
top:0;
bottom:0;
left:0;
right:0;
z-index:-1;
}
3) If you want to do it with only the body, then: stretch a fixed body with overflow scroll. But be aware it may interfere with some elements (eg. bootstrap tooltips and popovers)
body {
background: green;
overflow-y:scroll;
position:fixed;
top:0;
bottom:0;
left:0;
right:0;
}