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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

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

Leave a Comment!

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