tags:

views:

1330

answers:

4
div#thing
{
 position: absolute;
 top: 0px;
 z-index: 2;
 margin: 0 auto;
}

<div id="thing">
<p>text text text with no fixed size, variable font</p>
</div>

The div is at the top, but I can't center it with <center> or margin: 0 auto;

+6  A: 

Your problem may be solved if you give your div a fixed width, as follows:

div#thing
{
 position: absolute;
 top: 0px;
 z-index: 2;
 width:400px;
 margin-left:-200px;
 left:50%;
}
JacobE
I don't know why I didn't think of that, even though we use the same technique for vertical centering all the time...Thanks anyway, you saved me a lot of time.
Aayush
+2  A: 

Dave Shea (mezzoblue) has written a good article on the subject with explanations and examples: Horizontally Centered Absolute Positioning

Christian Lescuyer
A: 

@JacobE Can I center the <p> inside the <div>?

Steve
div#thing p{ text-align:center;}
JacobE
A: 

Yes:

div#thing { text-align:center; }