164

In the example below, #logo is positioned absolutely and I need it to be horizontally centered within #header. Normally, I would do a margin:0 auto for relatively positioned elements but I am stuck here. Can someone show me the way?

JS Fiddle: http://jsfiddle.net/DeTJH/

HTML

<div id="header">
    <div id="logo"></div>
</div>

CSS

#header {
    background:black;
    height:50px;
    width:100%;
}

#logo {
    background:red;
    height:50px;
    position:absolute;
    width:50px
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
J82
  • 8,267
  • 23
  • 58
  • 87

6 Answers6

297

If you want to align center on left attribute.
The same thing is for top alignment, you could use margin-top: (width/2 of your div), the concept is the same of left attribute.
It's important to set header element to position:relative.
try this:

#logo {
    background:red;
    height:50px;
    position:absolute;
    width:50px;
    left:50%;
    margin-left:-25px;
}

DEMO

If you would like to not use calculations you can do this:

#logo {
  background:red;
  width:50px;
  height:50px;
  position:absolute;
  left: 0;
  right: 0;
  margin: 0 auto;
}

DEMO2

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
136

You will have to assign both left and right property 0 value for margin: auto to center the logo.

So in this case:

#logo {
  background:red;
  height:50px;
  position:absolute;
  width:50px;
  left: 0;
  right: 0;
  margin: 0 auto;
}

You might also want to set position: relative for #header.

This works because, setting left and right to zero will horizontally stretch the absolutely positioned element. Now magic happens when margin is set to auto. margin takes up all the extra space(equally on each side) leaving the content to its specified width. This results in content becoming center aligned.

Arjun
  • 1,922
  • 1
  • 13
  • 21
  • 7
    I like this much better than offsetting with a margin because it works with dynamic widths. – davidmdem Aug 28 '13 at 15:39
  • Can you explain, why this works?? – hugo der hungrige Nov 01 '13 at 18:01
  • 7
    Setting `left` and `right` to zero will horizontally stretch the absolutely positioned element. Now magic happens when `margin` is set to `auto`. `margin` takes up all the extra space(equally on each side) leaving the content to it's specified `width`. This results in content becoming center aligned. – Arjun Nov 05 '13 at 10:44
  • @davidmdem It won't always work with dynamic widths (unless you add a child div). Consider for example a div containing text only... – matpop Apr 11 '16 at 11:53
21

Was missing the use of calc in the answers, which is a cleaner solution.

#logo {
  position: absolute;
  left: calc(50% - 25px);
  height: 50px;
  width: 50px;
  background: red;
}

jsFiddle

Works in most modern browsers: http://caniuse.com/calc

Maybe it's too soon to use it without a fallback, but I thought maybe for future visitors it would be helpful.

pzin
  • 4,200
  • 2
  • 28
  • 49
7

In my experience, the best way is right:0;, left:0; and margin:0 auto. This way if the div is wide then you aren't hindered by the left: 50%; that will offset your div which results in adding negative margins etc.

DEMO http://jsfiddle.net/kevinPHPkevin/DeTJH/4/

#logo {
    background:red;
    height:50px;
    position:absolute;
    width:50px;
    margin:0 auto;
    right:0;
    left:0;
}
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37
  • Yep, I said the same sometime back here http://stackoverflow.com/a/16758185/1571437 :D – Arjun May 26 '13 at 10:18
  • thanks, i like more this method because allows you to have dynamically sizes on the item, regards! – Alexis Apr 21 '15 at 03:36
4

here is the best practiced method to center a div as position absolute

DEMO FIDDLE

code --

#header {
background:black;
height:90px;
width:100%;
position:relative; // you forgot this, this is very important
}

#logo {
background:red;
height:50px;
position:absolute;
width:50px;
margin: auto; // margin auto works just you need to put top left bottom right as 0
top:0;
bottom:0;
left:0;
right:0;
}
Rohit Agrawal
  • 5,372
  • 5
  • 19
  • 30
2

Its easy, just wrap it in a relative box like so:

<div class="relative">
 <div class="absolute">LOGO</div>
</div>

The relative box has a margin: 0 Auto; and, important, a width...

user3849374
  • 127
  • 1
  • 3
  • Wow, this is great! Can't believe there are so many ways to achieve centering an absolute positioned div, but this one is definitely better then setting negative margin and works with any width as well! – YemSalat Sep 19 '14 at 07:01