Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Align on a Absolutely Positioned Div

Tags:

html

css

xhtml

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;

like image 559
Steve Avatar asked Oct 31 '08 08:10

Steve


People also ask

How do I center an absolute positioned div?

To center an element both vertically and horizontally: position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; But if you would like to understand how I came to these solutions, read further for more explanation.


1 Answers

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%; } 
like image 99
JacobE Avatar answered Oct 17 '22 07:10

JacobE