Centering with CSS

By Xah Lee. Date: . Last updated: .

To center a element:

Use text-align:center to center a text inside a element.

Use margin-left:auto; margin-right:auto; to center a block level element.

Here's a centered element.

I'M IN CENTER

Here's the code:

<div style="
margin-left: auto;
margin-right: auto;
text-align:center;
color:red;
">I'M IN CENTER</div>
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon