CSS Layout - Horizontal & Vertical Align

Center Align Elements

To horizontally center a block element (like <div>), use margin: auto;.

Setting a width ensures the element doesn't stretch to full width, and the space on both sides is equally distributed.

This div is horizontally centered!
Output :

Center Align Text

To just center the text inside an element, use text-align: center;.

Output :

Center an Image

To center an image, set the left and right margins to auto and make it a block element using display: block;.

Output 3:

Left and Right Align - Using position

One method for aligning elements is to use position: absolute;.

Output :

The clearfix Hack

Output:-: