How to centre an element in CSS

- by

In the olden days of HTML development, in the late 90ies, we only had inline styles. Centring an element was done using the <center> property. That approach isn’t very elegant anymore in modern CSS πŸ™‚

Today we can use something like this:

#your-id .your-class {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

It’s slightly more complicated than a single tag, but also opens up more options to tweak top and bottom margins at the same time.

1 thought on “How to centre an element in CSS”

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.