courses.reviews - My new website for finding the best coding courses, with reviews & exclusive discounts. It's free!

Published
2 min read
Up to date

Trevor I. Lasn

Staff Engineer, EM & Entrepreneur

align-content: The Simplest Way to Center Content with CSS

Finally, we can center things in block layouts without flexbox gymnastics

Block layouts have been part of CSS since the beginning, but they’ve always lacked proper alignment controls. Now, with align-content support in block layouts, we can distribute content vertically with the same flexibility we’ve enjoyed in flex and grid.

The old approaches required either flexbox or grid as a workaround. With flex, we needed multiple properties to achieve vertical centering. Grid made it a bit simpler with align-content, but still required changing the layout mode.

With the new align-content support in block layouts, we can achieve the same result while maintaining block layout semantics. No need to switch layout modes - just add align-content to your block container.

The browser will distribute the available space automatically, positioning your content in the center of the container. This works with any block container that has a defined block size.

display: flex

Centers with flex

justify-content: center
display: grid

Centers with grid

align-content: center
display: block

Centers with block

align-content: center

Browser Support and Fallbacks

This feature requires browsers that support the latest CSS Box Alignment specification. For older browsers, you can use either the flex or grid approach as a fallback.

The safest approach is to use @supports to provide a fallback for browsers that don’t support align-content in block layouts. This way, you can ensure a consistent experience across all browsers.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.



This article was originally published on https://www.trevorlasn.com/blog/css-align-content-in-block-layouts. It was written by a human and polished using grammar tools for clarity.