100% height div between header and footer - html

I am trying to create a webpage layout with a header/footer (100% width, 145px height), a 'main area' between the header/footer (100% width, dynamic height), and a container around the content that is a unique background color (860px width, dynamic height but is always 'flush' against the footer).
(See Example for a visual)
The problem I am having is I can't seem to have the 'content container' always be flush with the footer when there is minimal content. Using a setup like the (original example) results in the footer floating over the content if there is a respectable/'normal' amount of content or if the window is resized.
And the Following CSS results in a gap between the content and the footer.
html,body{
margin:0;
padding:0;
height:100%;
background:yellow;
}
.wrap{
min-height:100%;
position:relative;
}
header{
background:blue;
padding:10px;
}
#content{
height:100%;
width: 400px;
margin:0 auto;
background:orange;
padding:30px;
}
footer{
background:blue;
position:absolute;
bottom:0;
width:100%;
height:60px;
}
How can I make the content container be the full height of the screen when content is minimal and have the footer 'stick' to the bottom of the page, while also being dynamic to resize appropriately if there is a normal amount of content (footer is always at the bottom of the content)?
Thank you!

FIDDLE: http://jsfiddle.net/3R6TZ/2/
Fiddle Output: http://fiddle.jshell.net/3R6TZ/2/show/
CSS
html, body {
height: 100%;
margin:0;
}
body {
background:yellow;
}
#wrapper {
position: relative;
min-height: 100%;
vertical-align:bottom;
margin:0 auto;
height:100%;
}
#header {
width: 100%;
height: 150px;
background:blue;
position:absolute;
left:0;
top:0;
}
#content {
background:pink;
width:400px;
margin:0 auto -30px;
min-height:100%;
height:auto !important;
height:100%;
}
#content-spacer-top {
height:150px;
}
#content-spacer-bottom {
height:30px;
}
#divFooter {
width:100%;
height: 30px;
background:blue;
}
HTML
<div id="wrapper">
<div id="header">Header</div>
<div id="content">
<div id="content-spacer-top"></div>
<div id="content-inner">
**Content Goes Here**
</div>
<div id="content-spacer-bottom"></div>
</div>
<div id="divFooter">Footer</div>
</div>
UPDATE
The #content-spacer-top and #content-spacer-bottom are used to pad the #content div without using padding or margin that would increase the box size past the 100% height causing problems.
In CSS3, there is the box-sizing property (more info here) that can fix this issue, but i'm assuming you don't want to depend on CSS3 features.
EDIT
Added a fix and tested down to IE7
UPDATE 2
Alternate method using :before and :after pseudo-elements instead of the spacer divs:
http://jsfiddle.net/gBr58/1/
Doesn't work in IE7 or 6 though, and to work in IE8, a <!DOCTYPE> must be declared (according to w3schools.com), but the HTML is nice and clean
UPDATE 3 (Sorry for so many updates)
Updated it to work down to IE6. I don't normally bother as my company doesn't support IE6, but it was an easy fix...

I think you need position: fixed on the footer:
footer {
width: 100%;
height: 30px;
position:fixed;
bottom:0;
}

Related

Default footer in bottom when no content without position:absolute

I need a layout like header content and footer.
My requirements are...
The default position of footer Div is at bottom even no content in content Div.
When the content expand of content Div then whole body(including header,footer) need to be expand based on the content Div
I don't need position:fixed for the footer Div....
I already tried some code in my project......
CSS:
body
{
height:100%;
margin-left: 0;
margin-top: 0;
padding:0;
width:100%;
position: relative;
display:block;
}
.container
{
display:table;
width:100%;
height:100%;
}
.header
{
top:0px;
height:75px;
width:100%;
}
.content
{
width:100%;
height:auto;
position: relative;
}
.footer
{
top:5px;
bottom:0px;
height:45px;
overflow:hidden;
width:100%;
}
CODE:
<div>
<div class="header">
Header Div
</div>
<div class="content">
Content Div
</div>
<div class="footer">
Footer Div(Need it,default at bottom position)
</div>
</div>
Any Idea?
Note:I need to run this code in IE also......
add the following style to your body tag or content div element:
div {
min-height: 500px;
height:auto !important;
height: 500px;
}
This works because IE treats "height" how "min-height" is supposed to be treated.

How to set a margin-top of the size of the windows?

I want to put a div with CSS just after the current window (i.e when you start scrolling you see it).
It seems trivial and I tried
#content {
margin-top: 100%;
}
JSFiddle
But It isn't working and the margin don't take the height the current window.
Solutions :
You can achieve your aim using position:absolute; and top:100%;
FIDDLE
second option is to add an element with height:100%; to "push" .content down FIDDLE
Explanation :
The issue is that percent margin-top (like margin-bottom or padding-top/bottom) is calculated according to parent's width. See here
Percentages refer to the width of the containing block
CSS :
body,html {
background-color: lightblue;
height:100%;
width:100%;
margin:0;
}
#content {
position:absolute;
top: 100%;
background-color: lightgrey;
}
Code option 2 :
HTML :
<div id="push"></div>
<div id="content">
<p>... content ...</p>
</div>
CSS :
body,html {
background-color: lightblue;
height:100%;
width:100%;
margin:0;
}
#push{
height:100%;
}
#content {
background-color: lightgrey;
}

Css height 100%

I have the following html:
<body>
<div id="page">
<div id="header"></div>
<div id="content"></div>
</div>
</body>
and css:
html,body {
margin:0;
padding:0;
height:100%;
}
#page {
height:100%;
margin:auto;
left:0;
right:0;
width:500px;
}
#header {
height:100px;
width:500px;
}
#content {
width:500px;
height:100%;
}
The problem is that content div is the height of the window + the height of the header.
How can i make it to be the height of the window - the height of the header, I mean to stretch horizontally all over the remaining window. ??
In case you don't need to support IE7 and below - you can use a useful trick with
position: absolute
for #header and
padding-top: 100px;
box-sizing: border-box;
for #content.
Check out this fiddle: http://jsfiddle.net/Jx4sC/2/
Details regarding box-sizing support: http://caniuse.com/#feat=css3-boxsizing
You could use calc() in modern browsers and let the browser calculate the height of your content box:
#content {
width:500px;
height: calc(100% - 100px);
}
Similarly you could use some JavaScript to do the same. But then make sure to update your calculations each time the browser height gets changed.
This is supprisingly hacky to get going and you may not have to do it. for example, say you wanted to give #content a background-color, put it on #page instead.
html,body {
margin:0;
padding:0;
/* body should have height:100% by default */
}
#page {
height:100%;
margin: 0 auto;
width:500px;
/* use page as you would have used #content*/
}
#header {
height:100px;
}
#content {
}
edit: but if you really need to do this, you can do it like so
#page {
position: relative;
}
#content{
position: absolute;
top: 100px;
bottom: 0px;
left: 0px;
right: 0px;
}
this is not ideal because if you wanted to make #header bigger you need to remember to update #content, you can no longer use the normal page layout

How do I make my footer go down as I add content?

I structured my site like this:
<body>
<div class="main">
<div class="header">
content
</div>
<div class="section">
content
</div>
<div class="sidebar">
content
</div>
<div class="clearing"></div>
<div class="footer">
content
</div>
</div>
</body>
and the css
.main {
position:relative;
width:908px;
margin-top:0px;
border:solid 0px;
margin:0 auto;
}
.header {
position:relative;
height:200px;
margin: auto;
}
div.section {
float:left;
position:absolute;
width: 584px;
height:500px;
margin-top: 0px;
margin-left: auto;
margin-right: auto;
text-align:left;
}
div.sidebar{
float:right;
position:relative;
width: 324px;
height:500px;
margin-top: 0px;
margin-left: auto;
margin-right: auto;
text-align:left;
}
.clearing {
clear:both;
}
.footer {
position:relative;
left:0px;
top:0px;
width:908px;
height:300px;
}
When I add content to the div section, I goes all over the footer. The footer is fixed at that point, and as content is added, the page grows and the footer remains still.
What I have to do everytime I add content is to increase in the css file either the height of the section or the height of the sidebar - either one would work - but I increase both heights (section and sidebar) to the same value, because it seems the right thing to do. The footer then is pushed down as the height of the section and the sidebar grows.
However, if I atribute a height to the content that I add to the section, it does not have any effect on the position of the footer, perhaps because it has nothing to do with the height of the section itself.
Is there a way to make the footer respond to the space the content being added will occupy, and just automatically move along, remaining at the bottom of the page ?
You have position:absolute set on your .section div, which will only ever use its CSS height value for the amount of space it takes up, regardless of its actual content. Removing that absolute position should solve your problems. There's (almost) always a way around absolute positioning. In your example, it seems completely unnecessary.
Here's a helpful site for CSS positioning:
http://www.barelyfitz.com/screencast/html-training/css/positioning/
Hope it helps!
Try this:
.footer {
position:relative;
left:0;
bottom:0;
width:908px;
height:300px;
}
Also when putting 0, there is no need to add the px.
I am checking the CSS code and have spotted position in every div. Try to balance it because it affects other divs
Also try
.footer{
clear: both
}

How to make a fluid sidebar?

I'm creating a sidebar with this CSS code:
.sidebar {
position: absolute;
z-index: 100;
top: 0;
left: 0;
width: 30%;
height: 100%;
border-right: 1px solid #333;
}
But the sidebar width doesn't scale when I change the browser width. How can I make the sidebar fluid?
Thanks.
Look at the height in body in CSS part.
Here is a working example for you:
Your HTML:
<div id="content">
<p>This design uses a defined body height of 100% which allows setting the contained left and
right divs at 100% height.</p>
</div>
<div id="sidebar">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>
Your CSS:
body {
margin:0;
padding:0;
width:100%; /* this is the key! */
}
#sidebar {
position:absolute;
right:0;
top:0;
padding:0;
width:30%;
height:100%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}
#content { margin-right: 200px; }
Its kind of an odd issue, but it seems its challenging to get the background color to stretch to the bottom of both columns, when using fluid layout.
I included the workaround along with a simple 2 column fluid layout.
Try this- jsFiddle
html, body {
margin:0;
padding:0;
background:silver;
/* workaround to get the columns to look even,
change color depending on which column is longer */
}
#sidebar {
position:absolute;
left:0px;
top:0px;
padding:0;
width:30%;
background:silver;
word-wrap:break-word;
}
#content {
position:absolute;
right:0px;
width:70%;
word-wrap:break-word;
background:gray;
}