CSS TIP: How to center a <div> with a variable width

May 13, 2015 Yoav Schwartz

Conventional wisdom has it that if you want to center a container such as a div, your best bet is to define a width and set margin left and right to "auto". For example:

&lt;div id=&quot;container&quot; style=&quot;width:200px;margin:0 auto&quot;&gt; 
    your centered, 200 pixel wide container

This is fine when you have a defined width, but what happens when the width varies? 

There's a great example close to home - within a Hub (such as Uberflip's), a set of floated Item tiles are contained in a div and are required to be centered on the page such that, for example, on a wide screen that can fit it, 6 tiles displays on a single row.

Since we want a maximum of 6 tiles to display on a row, and each tile has a width of 250px, and each tile should be 20px away from its neighbor, the container width is being set to (250 + 20) * 6 = 1620px.

You'll find this in our CSS which defines the wrapper (.page-aligner) that houses the tiles container:

@media only screen and (max-width: 9999px){
  .page-aligner {
    width: 1620px;

While this works, it's rather limiting. If for example a Hub has only 4 Items, those 4 tiles will be off center, because the container will still be 1620px, with the last 2 spots for tiles unfilled.

There's a better way. And when I say better, I mean different.

Rather, than forcing a width to the container, we could do something like this:

&lt;div id=&quot;wrapper&quot; style=&quot;text-align:center&quot;&gt;
  &lt;div id=&quot;container&quot; style=&quot;display:inline-block;text-align:left&quot;&gt;
       your variable width, centered container

By setting your container to display inline-block (which is safe to use cross-browser), it can be aligned according to the text-align property. You'll notice I set the #container's text alignment to "left" to return it to the default.

In this example, rather than setting a width on the wrapper, we could set a max-width instead. That will force the 7th tile to wrap in this case.

It will also mean not having to define any break-points for smaller screens as now the screen width will determine smaller widths for that wrapper.

What are some handy CSS tips you've come across lately that have made your life easier? Let me know in the comments below!

About the Author

Yoav Schwartz

Yoav's the CEO and Founder at Uberflip. When not busy driving the vision and mission behind the world's most marketer-friendly content management platform, he's busy tinkering with front-end code...

Follow on Twitter More Content by Yoav Schwartz
Previous Article
Developer Growth Series - Part 1: Write code everyday
Developer Growth Series - Part 1: Write code everyday

Write code every day, you will pat yourself on the back for it eventually. Eventually you'll come out a str...

Next Article
Getting Started
Getting Started

Here you will find access to our API documentation, our developer blog, and...


Get our latest content delivered straight to your inbox!

First Name
Error - something went wrong!