(CSS) How to center a div without width

By | June 2, 2015

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

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

CSS

.centered {
   margin: 0 auto;
   display: table;
}

Leave a Reply

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