Just The Code Please

How to center an element horizontally using margins

January 9th 2024

Summary

One of the easiest ways of centering elements horizontally in CSS it to use margins. It is common to see this on approach used with wrapping container elements. This article shows you how.

The Code

CSS
.centered {
    display: block;
    margin-left: auto;
    margin-right: auto; 
}

Related Links