menu

Monday, September 30, 2013

Keeping the footer at bottom using CSS and HTML only

Hey guys,

Yup another post on the same day, again something old but havent answered properly on the internet.
The issue is what every web designer keep searching for years after properly coding the theme :
  "the damn footer doesnt stay at the bottom"



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

Steps

1) We deviate from the standard structure of

  ----header


  ----content


  ----footer


model of web page.


A slight adjustment.
The new structure is :

  ----top
           --header

           --content


 -----footer





  1. <html>
  2.  <head>
  3.    <title>Put my footer down</title>
  4.    <link href="style.css" rel="stylesheet" type="text/css">
  5.   </head>
  6.  <body>
  7.     <div id="top">
  8.         <div id="header">This is header stuff</div>
  9.         <div id="content">This is awesome text stuff</div>
  10.     </div>
  11.     <div id="footer">This is footer stuff</div>
  12.  </body>
  13. </html>

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

2) Now we define the CSS stuff, the important elements are the top and footer elements. Others we dont 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 scrollbar 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
  1. #footer{
  2.         height: 50px;
  3. }
  4. #top{
  5.         min-height: 100%;
  6.         margin-bottom: -50px;
  7. }

DONE ! yup, in 3 easy code lines everything works fine
[works on all usable browsers :D]




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

Be back soon
Cyaa

No comments:

Post a Comment