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
;
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.
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%; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With