-1

I have a DIV that I want to keep centered and docked to the bottom of the page. I've pretty much achieved that except that it lists to the right. I've setup a jsFiddle to demonstrate the problem.

I suspect that I need to adjust margins but so far my attempts have been fruitless. I tried adding:

margin: 0;

but nothing. What am I missing?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

8 Answers8

3

If you mean the little bit of space to the left, then I would think adding left: 0; would solve the problem.

#footer { 
    position: fixed; 
    bottom: 0; 
    width: 100%;
    border: 1px solid red;
    text-align: center;
    margin: 0;
    left: 0;
}

You need to watch though because you are setting the width to be 100% and also setting a border which makes the div wider than 100%.

Tim B James
  • 20,084
  • 4
  • 73
  • 103
3

I suspect this is the default margin/padding on the body

body {
    margin: 0;
}

will fix it.

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
2

Remove border: 1px solid red; and add left: 0; to #footer.

Here is the JSFiddle.

Kai Feller
  • 653
  • 3
  • 14
2

You need to set left to 0 (zero) and box-sizing to border-box!

Check this: http://jsfiddle.net/pR4P5/7/

#footer { 
    position: fixed; 
    bottom: 0; 
    width: 100%;
    border: 1px solid red;
    text-align: center;
    margin: 0;
    box-sizing: border-box;
    left:0;
}

Have a nice day,

Alberto

Alberto
  • 1,853
  • 1
  • 18
  • 22
1

Change the width: 100% to

left: 0;
right: 0;

So it would become: http://jsfiddle.net/pR4P5/6/

Complete CSS:

#footer {
  position: fixed; 
  bottom: 0; 
  left:0;
  right: 0;
  border: 1px solid red;
  text-align: center;
  margin: 0;
}

Result would be 100% width while you can keep your border of 1px. Note when defining width 100% and adding a border you might be better of adding box-sizing to it. Also, margin: 0; to body is a great way to avoid headaches.

(box-sizing: content-box; - http://quirksmode.org/css/user-interface/boxsizing.html)

Marcel
  • 1,279
  • 10
  • 12
1

Try replacing margin: 0; with left: 0;. This should lock the div onto the left of the page, centering it. I would also not use a border, that will add on some pixels to the width and height, making it less centered. I would instead use only border-top: 1px solid red;.

Hope I helped you solve your annoying problem.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
1

Paulie_D's answer is correct you need to add margin: 0 to the body. I'd imagine you think its not properly centred because of the border on the footer.

Try adding:

box-sizing: border-box;

to your footer element.

Paul. B
  • 1,328
  • 1
  • 13
  • 22
1

To Center the div:

html, body {
    margin:0;
    padding:0;
    width:100%;
    height:100%;
}

To keep the borders included in the positioning, too:

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

DEMO: http://jsfiddle.net/pR4P5/8/

Read More at:

CSS Reset: http://meyerweb.com/eric/tools/css/reset/

box-sizing: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

Arbel
  • 30,599
  • 2
  • 28
  • 29