views:

3224

answers:

6

Hi,

I have a <div>...</div> section in my HTML that is basically like a toolbar.

Is there a way I could force that section to the bottom of the web page (the document, not the viewport) and center it?

A: 

Try this: Fixed footers without Javascript. I don't know if it will be a perfect fit, but I think it's close enough.

Srdjan Pejic
Doesn't work when resizing the browser for me. Looks good otherwise -- snaps to the bottom of the document or, if the document doesn't scroll, to the bottom of the viewport (instead of just floating).
strager
A: 

You can just give the div a:

clear:both; text-align:center;

and put the div as the last element before the closing body statement. That would force it to be the last element without anything next to it.

jeroen
+9  A: 

I think what you're looking for is this: http://ryanfait.com/sticky-footer/

It's an elegant, CSS only solution!

I use it and it works perfect with all kinds of layouts in all browsers! As far as I'm concerned it is the only elegant solution which works with all browsers and layouts.

@Josh: No it isn't and that's what Blankman wants, he wants a footer that sticks to the bottom of the document, not of the viewport (browser window). So if the content is shorter than the browser window, the footer sticks to the lower end of the window, if the content is longer, the footer goes down and is not visible until you scroll down.

tharkun
Is the footer always visible? Regardless of scroll position?
Josh Stodola
Thanks for clarifying!
Josh Stodola
A: 

Your best bet is to use javascript to determine the size of your page. You can get the height with window.innerHeight with non-IE browsers and document.documentElement.clientHeight with IE. With that value you should be able to absolutely position your element on the page setting top to that value minus the height of your div. If the height of your div is variable you will need to check the div's offsetHeight property to get the real height.

For centering use the following example:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
 <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
     <style>
      .wrapper
      {
       width: 100%;
       padding-left: 50%;
      }
      .test
      {
       width: 400px;
       margin-left: -200px;
       border: 1px solid black;
       padding-left: -200px;
      }
     </style>
    </head>
    <div class="wrapper">
    <div class="test">This is a test</div>
    </div>
 </html>

You have a wrapper div around the div you want centered. The wrapper div has a width of 100% the inner div has a width set to whatever you want it to be. Give the wrapper div a left padding of 50% and the inner div a negative left margin equal to half of its width.

Ross Pace
-1 because it talks about the viewport, that's not what he wants!
tharkun
+2  A: 

I just want to be clear on what your saying here:

bottom of the web page (the document, not the viewport)

Naturally, a div will be at the bottom of the "document", depending on your layout.

If it's not going to the bottom of a document, or not paying attention to how tall your columns are, is it because your floating? Clear: both; would be in order to solve that.

The sticky footers are what I think your looking for, but when you say document, and not the viewport, I get a bit confused. Sticky footers typically do this: Watch for short pages, and if its shorter than the view port, the sticky footer tacks the footer div to the bottom.

Here's some sticky footers (there's gajillions of em, but this is in order of my favorites):

Maybe if you gave a quick illustration or were a bit more specific on what you want? Hope this helps :D

-Ken

Ken Hanson
I think he just ment that if the document is longer than the viewport, the footer should not show until you scroll to the end. which is a sticky footer. with position absolute you would get a sticky viewport-footer which is in most cases ugly.
tharkun
+3  A: 

If I understand you correctly, you want the toolbar to always be visible, regardless of the vertical scroll position. If that is correct, I would recommend the following CSS...

  body {
    margin:0;
    padding:0;
    z-index:0;
  }

  #toolbar {
    background:#ddd;
    border-top:solid 1px #666;
    bottom:0;
    height:15px;
    padding:5px;
    position:fixed;
    width:100%;
    z-index:1000;
  }

See an example here: http://blog.josh420.com/examples/footer.htm

Best regards...

Josh Stodola
+1 Helps for me. Thnx
Martijn
Josh, any cross browser issues with this technique?
Blankman
Well, I know that IE6 doesn't support fixed positioning. But I tried it in IE7 and FF2 and it works.
Josh Stodola
This doesn't work in Safari on the iPhone... dang.
Hector Scout
@Hector I wonder if adding position:relative to the body fixes that
Josh Stodola
position:relative makes it so it doesn't stick to the bottom.
Hector Scout