Vertical centering in CSS

CSS is still confusing for most of us: it has hundreds of poorly named properties, each of which can affect the result in non-intuitive ways. Vertical positioning of elements in web development is tricky and has often been a headache in the past, but those days are thankfully over.


It's hard to believe: in 2024, it is now possible to achieve the desired result with one line of CSS:

0f1273b25d9806459716

0f1273b25d9806459716

In the past, a variety of other options were used to achieve the same result:

Flexbox

0f1273b25d9806459716

Absolute positioning

0f1273b25d9806459716

Inline block

0f1273b25d9806459716

The Browser support for align-content is now very good and there is no longer any good argument for using outdated techniques. Among other things, you no longer need a flexbox for this task and the child element (unlike in the example above) does not have to be placed in a separate div It is amazing that after decades of progress, CSS only now has a single feature to control vertical centering.

Back