CSS Layout – Horizontal & Vertical Align

Center Align Elements

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

Setting the width of the element will prevent it from stretching out to the edges of its container.

The element will then take up the specified width, and the remaining space will be split equally between the two margins:

Center Align Text

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

Example:

Output:

Left and Right Align – Using float

Another method for aligning elements is to use the float property:

Leave a comment

Your email address will not be published. Required fields are marked *