text-align:center
When you apply the text-align:center
style to an element containing other elements, it will center those elements.
<div id="method-one" style="text-align:center">
CSS `text-align:center`
</div>
Thomas Shields mentions this method
margin:0 auto
Using the margin:0 auto
style on a block element will center it within its container element.
<div id="method-two" style="background-color:green">
<div style="margin:0 auto;width:50%;background-color:lightblue">
CSS `margin:0 auto` is used to center a block element horizontally within another element.
</div>
</div>
user1468562 mentions this method
Center tag
In the past, I suggested using the <center></center>
tag to center content. However, this tag has been deprecated since HTML4. It is still technically supported today, but it's better to use the CSS alternatives mentioned above.
<h3>Method 3</h1>
<div id="method-three">
<center>Center tag (not recommended and deprecated in HTML4)</center>
</div>
You can see these three code samples in action in this jsfiddle.
I realized my previous answer was outdated, so I decided to revise it. The <center>
tag was already deprecated when I initially suggested it as a solution, making it even more important to avoid using it now, nine years later.