Most efficient and compatible way to achieve docked footer - html

I am trying to do the following in a CSS template:
Dock the footer to the bottom when there is not enough content to
fill the page
Stretch the header and footer background across the whole width
Position all the content in the middle of the page
This is the code I have, created with help on here:
http://tinkerbin.com/lCNs7Upq
My question is, I have seen a few ways to achieve this. Is this the best? It seems a shame to have to have the empty div as well, is this a bodge?

You can fix and element to the footer using CSS:
position: fixed;
bottom: 0;
However, I'm trying to figure out what exactly your trying to do.
You header and footer should automatically go 100% across the page if it's a div.
Your middle section can be set to height: auto; via css and will fill up the viewport pushing the footer all the way to the bottom, but to do this you also have to set the body to 100% in order to get it to work.
html, body, #content {
height: 100%;
min-height: 100%;
}
#header {
height: 100px;
width: 100%;
background: blue;
position: fixed;
top: 0;
}
#content {
height: auto;
margin: 100px auto;
background: green;
}
#footer {
position: fixed;
bottom: 0;
height: 100px;
width: 100%;
background: red;
}
Your HTML should look somewhat like this:
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
Working Example: http://jsfiddle.net/s4rT3/1/

This is the best example I have seen:
http://css-tricks.com/snippets/css/sticky-footer/
* {
margin: 0;
}
html, body {
height: 100%;
}
.page-wrap {
min-height: 100%;
/* equal to footer height */
margin-bottom: -142px;
}
.page-wrap:after {
content: "";
display: block;
}
.site-footer, .page-wrap:after {
height: 142px;
}
.site-footer {
background: orange;
}
<div class="page-wrap">
Content!
</div>
<footer class="site-footer">
I'm the Sticky Footer.
</footer>
Update: In 2019 using flex is a better option.

Related

CSS: Position DIV at bottom of page

This is a sample HTML page:
<html>
<body>
<div class="content">
</div>
<footer> </footer>
</body>
</html>
This is my style sheet:
html {
height: 100%;
}
body {
position: relative;
margin: 0;
height: 100%;
}
.content {
width: 8cm;
position: absolute;
background-color: #ff0;
height: 15cm;
}
footer {
position: absolute;
bottom: 0;
background-color: #f00;
width: 100%;
}
This is how it looks like:
My problem is that I want the red footer to be at the bottom of the page (not the bottom of the viewport), assuming that the .content is of an variable height actually. Is that possible without JavaScript?
This Fiddle shows a footer that is always either at the lowest point on the page or on the bottom of the viewport.
The DIV is positioned at the bottom of the viewport when the content does not fill the page, and stays below the content when the content gets taller than the viewport.
To accomplish this, use a min-height on the body like this:
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
}
footer {
position: absolute;
bottom: 0;
}
Tested in Safari 8.0.3.

Sidebar to fill height of entire page

This is perhaps a common question, but all the answers I have found around the web, didn't work properly.
I want to create a sidebar for my webpage, which fills the entire height of the webpage.
Then when you scroll the sidebars content should move along the rest of the sites content.
I tried this methods:
HTML:
<div id="wrapper">
<div id="sidebar"></div>
<div id="content-area"></div>
</div>
CSS:
body {
margin: 0;
}
#wrapper {
width: 100%;
}
#sidebar {
width: 280px;
position: absolute;
top: 0;
bottom: 0;
}
#content-area {
width: 900px;
margin-left: 280px;
}
..and:
HTML:
<div id="wrapper">
<div id="sidebar"></div>
<div id="content-area"></div>
</div>
CSS:
body {
height: auto;
min-height: 100%;
margin: 0;
}
#wrapper {
width: 100%;
height: 100%;
}
#sidebar {
width: 280px;
height: 100%;
float: left;
}
#content-area {
width: 900px;
height: 100%;
float: left;
}
The first one works fine, until the content is extended: http://jsfiddle.net/B3bCb/1/
If that happens then the sidebar stops according to the browser-window height.
The second one didn't worked at all: http://jsfiddle.net/B3bCb/2/
I have also tried the faux column method, but I need to have an CSS-shadow (which blur, spread and color, can be changed dynamically on the site) on my sidebar, which I cannot do properly in the faux column method (Faux column is just an image).
So how do I make my sidebar 100% in height, no matter how much content I have?
Making the sidebar position as "fixed" it stays, doesn't matter how much content you have. I don't know if I solve your problem but hope it helps ;)
Here's the code:
#sidebar {
width: 280px;
position: fixed;
top: 0;
bottom: 0;
background: blue; }
Here's the fiddle
The following css can be another alternative for this
#sidebar {
width: 280px;
position: fixed;
top: 0;
height:100%;
background: blue;
}
#content-area {
position:relative;
left: 280px; // width of your side box.
}
You have a couple of choices, but the gist is to do with CSS position.
The reason position: absolute; does not work is because it needs some tweaking for that to work. You need to disable scrolling on the html, body, and wrapper classes, and enable scrolling on the content-area.
html, body, wrapper {
overflow: hidden;
}
.content-area {
overflow: auto;
}
You can see an example of this here. It's a responsive sidebar layout I made.
The other option is to use a position: fixed; on the sidebar, as others have noted.
Check this fiddle http://jsfiddle.net/dJ654/2/
I have changed some styles
#sidebar {
width: 10%;
height: 100%;
position:fixed;
right:0px;
top:0px;
background-color:gray;
}
#content-area {
width: 90%;
height: 100%;
float: left;
background-color:green;
}

Sticky footer not working correctly

Im trying to implement sticky footer to my site but for some reason it doesnt want to work and its pushed more than it needs to be. I've tried many "sticky footer" tutorials but there is always something not working.
Please check my fiddle: http://jsfiddle.net/Qx5Fz/1/
html, body {
height: 100%;
}
#content {
min-height: 100%;
}
#main {
overflow:auto;
padding-bottom: 40px;
}
#footer {
position: relative;
margin-top: -40px; /* negative value of footer height */
height: 40px;
clear:both;
}
Updated to allow for overlapping sidebar: http://jsfiddle.net/Qx5Fz/12/
The issue you are having is using position: fixed; height: 100%; on the sidebar. That causes the sidebar to be 100% of the window, which will always push your footer down when it should be at the bottom of the window.
I'm using sticky footer from here: Sticky footer + textarea height in percentage (source: http://css-tricks.com/snippets/css/sticky-footer/)
You need to put all your content inside of one div including your header. Then use this css to get the footer to work. This takes into account vertical margins, so you'll need to either incorporate any into the calcs, or just use padding.
html, body {
height: 100%;
/* The html and body elements cannot have any padding or margin. */
}
#wrap {
height:100%;
margin: 0;
}
#wrap {
min-height: 100%;
/* equal to footer height */
margin-bottom: -100px;
}
#wrap:after {
content: "";
display: block;
}
#footer, #wrap:after {
/* .push must be the same height as footer */
height: 100px;
}
I added top: 0 to sidebar.
try
#footer { position: fixed; }
instead of relative
try this: http://jsfiddle.net/Qx5Fz/8/
#footer {
position: fixed;
height: 40px;
bottom: 0;
left: 0;
width: 100%;
z-index: 999;
}

2-Column Container 100% Body Height

I'm using this example fyi: http://www.dynamicdrive.com/style/layouts/item/css-liquid-layout-21-fixed-fluid/ (where the left column is a fixed width via px and the right is re-sizable)
I'm trying to make the height of the container 100% of the body height.
e.g. forget the bottom content (the copy code) on that page, just the top layout example, how would I make the height of that (no matter if there's content or not) 100% the body height. With content I'm trying to get it to scroll, without (if any) it needs to be 100% height of the body minimum.
Any suggestions, no luck so far..
My jsfiddle example:
http://jsfiddle.net/GtK98/1/
Have you tried
min-height:100%;
in the css?
this is a layout without a footer, If you want to add a footer comment here and I'll do that to.
Pure CSS, without Fixing the header height, with/without fixing the left side width, cross browser (IE8+)
Take a look at that Working Fiddle
HTML: (very basic)
<div class="Container">
<div class="Header">
</div>
<div class="HeightTaker">
<div class="Wrapper">
<div class="LeftContent">
</div>
<div class="RightContent">
</div>
</div>
</div>
</div>
CSS:
*
{
margin: 0;
padding: 0;
}
html, body, .Container
{
height: 100%;
}
.Container:before
{
content: '';
height: 100%;
float: left;
}
.HeightTaker
{
position: relative;
z-index: 1;
}
.HeightTaker:after
{
content: '';
clear: both;
display: block;
}
.Wrapper
{
position: absolute;
width: 100%;
height: 100%;
}
.Header
{
/*for demonstration only*/
background-color: #bf5b5b;
}
.Wrapper > div
{
height: 100%;
overflow: auto;
}
.LeftContent
{
float: left;
/*for demonstration only*/
background-color: #90adc1;
}
.RightContent
{
/*for demonstration only*/
background-color: #77578a;
}

Website Footer wont stick to the bottom of page

Im trying to get a footer to stick to the bottom of my webpage but it floats only half way up. I've looked at a few examples and I cant see what im doing wrong. Any assistance would be greatly appreciated. My simplified code is shown below.
<html>
<head>
</head>
<body>
<div id = "wrapper">
<!--Wrapper div for the main content-->
</div>
<!--Footer container-->
<div class="footer">
</div>
</body>
</html>
--CSS--
body
{
height: 100%;
margin: 0;
padding:0;
background-color: #1A1F2B;
min-width: 960px;
}
#wrapper{
min-height: 100%;
}
.footer{
position: relative;
margin-top: -150px; /* negative value of footer height */
height: 150px;
clear:both;
display: block;
background-color: #232A3B;
}
If you want it to be at the bottom of the document:
.footer {
position: absolute;
bottom: 0;
}
If you want it to be at the bottom of the viewport:
.footer {
position: fixed;
bottom: 0;
}
If you'd like the footer div to be on the bottom of the page and span the entire width this would work:
.footer {
position: absolute;
bottom:0;
height: 150px;
width: 100%;
background-color: #232A3B;
}
HTML5 also supports the <footer> Tag which may make it easier for bots to process the information on your webpage. To change that just use footer { instead of .footer { and change the HTML markup.