Following is a list of solutions to centering things in CSS horizontally. The snippet includes all of them.
html {_x000D_
font: 1.25em/1.5 Georgia, Times, serif;_x000D_
}_x000D_
_x000D_
pre {_x000D_
color: #fff;_x000D_
background-color: #333;_x000D_
padding: 10px;_x000D_
}_x000D_
_x000D_
blockquote {_x000D_
max-width: 400px;_x000D_
background-color: #e0f0d1;_x000D_
}_x000D_
_x000D_
blockquote > p {_x000D_
font-style: italic;_x000D_
}_x000D_
_x000D_
blockquote > p:first-of-type::before {_x000D_
content: open-quote;_x000D_
}_x000D_
_x000D_
blockquote > p:last-of-type::after {_x000D_
content: close-quote;_x000D_
}_x000D_
_x000D_
blockquote > footer::before {_x000D_
content: "\2014";_x000D_
}_x000D_
_x000D_
.container,_x000D_
blockquote {_x000D_
position: relative;_x000D_
padding: 20px;_x000D_
}_x000D_
_x000D_
.container {_x000D_
background-color: tomato;_x000D_
}_x000D_
_x000D_
.container::after,_x000D_
blockquote::after {_x000D_
position: absolute;_x000D_
right: 0;_x000D_
bottom: 0;_x000D_
padding: 2px 10px;_x000D_
border: 1px dotted #000;_x000D_
background-color: #fff;_x000D_
}_x000D_
_x000D_
.container::after {_x000D_
content: ".container-" attr(data-num);_x000D_
z-index: 1;_x000D_
}_x000D_
_x000D_
blockquote::after {_x000D_
content: ".quote-" attr(data-num);_x000D_
z-index: 2;_x000D_
}_x000D_
_x000D_
.container-4 {_x000D_
margin-bottom: 200px;_x000D_
}_x000D_
_x000D_
/**_x000D_
* Solution 1_x000D_
*/_x000D_
.quote-1 {_x000D_
max-width: 400px;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
}_x000D_
_x000D_
/**_x000D_
* Solution 2_x000D_
*/_x000D_
.container-2 {_x000D_
text-align: center;_x000D_
}_x000D_
_x000D_
.quote-2 {_x000D_
display: inline-block;_x000D_
text-align: left;_x000D_
}_x000D_
_x000D_
/**_x000D_
* Solution 3_x000D_
*/_x000D_
.quote-3 {_x000D_
display: table;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
}_x000D_
_x000D_
/**_x000D_
* Solution 4_x000D_
*/_x000D_
.container-4 {_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
.quote-4 {_x000D_
position: absolute;_x000D_
left: 50%;_x000D_
transform: translateX(-50%);_x000D_
}_x000D_
_x000D_
/**_x000D_
* Solution 5_x000D_
*/_x000D_
.container-5 {_x000D_
display: flex;_x000D_
justify-content: center;_x000D_
}
_x000D_
<main>_x000D_
<h1>CSS: Horizontal Centering</h1>_x000D_
_x000D_
<h2>Uncentered Example</h2>_x000D_
<p>This is the scenario: We have a container with an element inside of it that we want to center. I just added a little padding and background colors so both elements are distinquishable.</p>_x000D_
_x000D_
<div class="container container-0" data-num="0">_x000D_
<blockquote class="quote-0" data-num="0">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
_x000D_
<h2>Solution 1: Using <code>max-width</code> & <code>margin</code> (IE7)</h2>_x000D_
_x000D_
<p>This method is widely used. The upside here is that only the element which one wants to center needs rules.</p>_x000D_
_x000D_
<pre><code>.quote-1 {_x000D_
max-width: 400px;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
}</code></pre>_x000D_
_x000D_
<div class="container container-1" data-num="1">_x000D_
<blockquote class="quote quote-1" data-num="1">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
_x000D_
<h2>Solution 2: Using <code>display: inline-block</code> and <code>text-align</code> (IE8)</h2>_x000D_
_x000D_
<p>This method utilizes that <code>inline-block</code> elements are treated as text and as such they are affected by the <code>text-align</code> property. This does not rely on a fixed width which is an upside. This is helpful for when you don’t know the number of elements in a container for example.</p>_x000D_
_x000D_
<pre><code>.container-2 {_x000D_
text-align: center;_x000D_
}_x000D_
_x000D_
.quote-2 {_x000D_
display: inline-block;_x000D_
text-align: left;_x000D_
}</code></pre>_x000D_
_x000D_
<div class="container container-2" data-num="2">_x000D_
<blockquote class="quote quote-2" data-num="2">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
_x000D_
<h2>Solution 3: Using <code>display: table</code> and <code>margin</code> (IE8)</h2>_x000D_
_x000D_
<p>Very similar to the second solution but only requires to apply rules on the element that is to be centered.</p>_x000D_
_x000D_
<pre><code>.quote-3 {_x000D_
display: table;_x000D_
margin-right: auto;_x000D_
margin-left: auto;_x000D_
}</code></pre>_x000D_
_x000D_
<div class="container container-3" data-num="3">_x000D_
<blockquote class="quote quote-3" data-num="3">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
_x000D_
<h2>Solution 4: Using <code>translate()</code> and <code>position</code> (IE9)</h2>_x000D_
_x000D_
<p>Don’t use as a general approach for horizontal centering elements. The downside here is that the centered element will be removed from the document flow. Notice the container shrinking to zero height with only the padding keeping it visible. This is what <i>removing an element from the document flow</i> means.</p>_x000D_
_x000D_
<p>There are however applications for this technique. For example, it works for <b>vertically</b> centering by using <code>top</code> or <code>bottom</code> together with <code>translateY()</code>.</p>_x000D_
_x000D_
<pre><code>.container-4 {_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
.quote-4 {_x000D_
position: absolute;_x000D_
left: 50%;_x000D_
transform: translateX(-50%);_x000D_
}</code></pre>_x000D_
_x000D_
<div class="container container-4" data-num="4">_x000D_
<blockquote class="quote quote-4" data-num="4">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
_x000D_
<h2>Solution 5: Using Flexible Box Layout Module (IE10+ with vendor prefix)</h2>_x000D_
_x000D_
<p></p>_x000D_
_x000D_
<pre><code>.container-5 {_x000D_
display: flex;_x000D_
justify-content: center;_x000D_
}</code></pre>_x000D_
_x000D_
<div class="container container-5" data-num="5">_x000D_
<blockquote class="quote quote-5" data-num="5">_x000D_
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>_x000D_
<footer>Tasha Yar about Data</footer>_x000D_
</blockquote>_x000D_
</div>_x000D_
</main>
_x000D_
display: flex
.container {
display: flex;
justify-content: center;
}
Notes:
max-width
& margin
You can horizontally center a block-level element by assigning a fixed width and setting margin-right
and margin-left
to auto
.
.container ul {
/* for IE below version 7 use `width` instead of `max-width` */
max-width: 800px;
margin-right: auto;
margin-left: auto;
}
Notes:
transform: translatex(-50%)
& left: 50%
This is similar to the quirky centering method which uses absolute positioning and negative margins.
.container {
position: relative;
}
.container ul {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
Notes:
top
instead of left
and translateY()
instead of translateX()
. The two can even be combined. transform2d
display: table
& margin
Just like the first solution, you use auto values for right and left margins, but don’t assign a width. If you don’t need to support IE7 and below, this is better suited, although it feels kind of hacky to use the table
property value for display
.
.container ul {
display: table;
margin-right: auto;
margin-left: auto;
}
display: inline-block
& text-align
Centering an element just like you would do with regular text is possible as well. Downside: You need to assign values to both a container and the element itself.
.container {
text-align: center;
}
.container ul {
display: inline-block;
/* One most likely needs to realign flow content */
text-align: initial;
}
Notes: