Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Tutorials (http://www.go4expert.com/articles/web-design/)
-   -   Keeping Footer at the Bottom With CSS and HTML (http://www.go4expert.com/articles/keeping-footer-css-html-t29846/)

ManzZup 30Sep2013 21:14

Keeping Footer at the Bottom With CSS and HTML
 
Yup another post on the same day, again something old but haven't answered properly on the internet.

The issue is what every web designer keep searching for years after properly coding the theme :

"the damn footer doesn't stay at the bottom"

http://imgs.g4estatic.com/bottom-foo...bla-footer.png

Usual solution


  1. One method suggested by most is to use position : absolute to the footer. But this is the what that wrecks a good template. The absolute position means it position the element according to absolute pixel values. But the problem is when the website is displayed in a monitor other than yours. Guess what will happen?
  2. Other involved using position : fixed to the footer. Again this is a lousy patch since this would put a permanent footer not at the real bottom of the page but the bottom of the screen. [know the difference]
So here's a new way

The Code Steps



Step 1

We deviate from the standard structure of

----header
----content
----footer

model of web page.

http://imgs.g4estatic.com/bottom-foo...t-footer-1.png

A slight adjustment. The new structure is :

----- top
----- ----- header
----- ----- content
----- footer

http://imgs.g4estatic.com/bottom-foo...ent-footer.png

HTML Code:

<html>
 <head>
  <title>Put my footer down</title>
  <link href="style.css" rel="stylesheet" type="text/css">
  </head>
 <body>
    <div id="top">
        <div id="header">This is header stuff</div>
        <div id="content">This is awesome text stuff</div>
    </div>
    <div id="footer">This is footer stuff</div>
 </body>
</html>

NOTE: style.css is the style sheet we define stuff

Step 2:

Now we define the CSS stuff, the important elements are the top and footer elements. Others we don't have to worry about.

Logic:



We make the #top element with 100% height BUT it would push down the footer as well. Then an unnecessary scroll-bar appears. So what we do is tell to the #top element "hey bro give some of your space below to the #footer element as well". How we do this? by giving a (-)ve value to the margin property of the #top element

NOTE: THE margin (-) value of #top element SHOULD BE THE SAME as the height property of the #footer element.

style.css
HTML Code:

#footer{
        height: 50px;
}
#top{
        min-height: 100%;
        margin-bottom: -50px;
}

DONE ! yup, in 3 easy code lines everything works fine

[works on all usable browsers :D]

http://imgs.g4estatic.com/bottom-foo...ent-footer.png


Defects:


  1. When your #footer element have borders and stuff you have to adjust the (-)ve value carefully.
  2. The height: 100% has some problems with strict doctypes.

But overall the good is far more than the bad, so happy footer-downing :D


All times are GMT +5.5. The time now is 20:10.