CSS – Easiest way to center Div horizontally

  • by

Let’s see how we can center the div horizontally by simply providing margin to its CSS.  The margin value when set to “0px auto”, the Html element automatically aligns in the center horizontally. This will adjust the left and right margins of the div evenly respective to its parent alignment.

Html code:

<title>Center Div</title>

margin: 0px auto;
color: #000;
font-family: Arial, Helvetica, sans-serif;
text-align: center;
width: 500px;
height: 500px;
font-weight: bold;
padding: 50px;
background-color:#3146b1 ;


<div class="container">
<span>I'm in center horizontally</span>


Center Div Horizontally using CSS

This will make our container (div class) to align in center.
This can fail if the parent element has smaller width than the div which we want to center horizontally.