(CSS) How to center a div without width


I needed to center a div with dynamic content, so I couldn’t set a fixed width.
Since margin: 0 auto; really won’t do anything if the element doesn’t have a fixed width you can solve it like this:

HTML
[html]
<div id="container">
<div class="centered">My dynamic content, that will be centerd</div>
</div>
[/html]

CSS
[css]
.centered {
margin: 0 auto;
display: table;
}
[/css]

Leave a Reply

Your email address will not be published. Required fields are marked *