CSS New Align-Content for Block Elements

Frank LamFrank Lam
3 min read

In this post, we’ll explore what the new align-content property is, how it works with block elements, and practical use cases where it can simplify your CSS.

What is align-content?

The align-content property in CSS controls the alignment of multiple lines of content within a container. It is primarily used in flexbox and grid layouts to distribute space between and around items along the cross axis. Now, its application has been extended to block elements, enabling developers to align their content vertically with more precision.


Syntax

align-content: start | end | center | space-between | space-around | space-evenly | stretch;

Here’s what each value does:

  • start: Aligns content to the start of the container.

  • end: Aligns content to the end of the container.

  • center: Centers content vertically within the container.

  • space-between: Distributes content with space between lines but no space at the edges.

  • space-around: Distributes content with equal space around each line.

  • space-evenly: Distributes content with equal space between and around all lines.

  • stretch: Stretches the lines to fill the container.


How align-content Works for Block Elements

When applied to a block container, align-content controls the vertical alignment of block-level content inside the container. It brings the kind of flexibility previously only available in layout systems like flexbox and grid.

Requirements:

  1. The block container must have a definite height (either set explicitly or defined by its parent).

  2. The block container can include multiple lines of text, inline-block elements, or other child blocks.


Practical Examples

1. Centering Content Vertically

Centering content vertically has always been a CSS challenge. With align-content, it’s now straightforward for block elements.

<div class="container">
  <p>This is vertically centered content.</p>
</div>
.container {
  height: 400px;
  display: block;
  align-content: center;
  background: #f4f4f4;
  border: 1px solid #ccc;
}

2. Creating Even Spacing Between Lines

If you have multiple lines of text or inline-block elements, align-content can distribute them evenly:

<div class="container">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
</div>
.container {
  height: 300px;
  display: block;
  align-content: space-evenly;
  background: #e0f7fa;
}

When to Use align-content for Block Elements

  1. Centering and Alignment: Simplifies the process of vertically aligning text, images, or blocks within a parent container.

  2. Modern Layouts: Works seamlessly in designs that involve multiple lines of text or child elements distributed vertically.

  3. Fallback for Non-Flex/Grid Environments: Provides an alternative to flexbox or grid for basic alignment needs in block containers.


Browser Support

As of latest specifications, align-content for block elements is being progressively implemented in modern browsers. Make sure to check Can I Use for specific browser support before using it in production.


Conclusion

The new support for align-content on block elements in CSS simplifies vertical alignment challenges that have plagued web developers for years. Whether you’re building responsive layouts, centering elements, or distributing content evenly, align-content is a welcome addition to your CSS toolkit. As browser support expands, this feature will undoubtedly become a go-to solution for modern layouts.

0
Subscribe to my newsletter

Read articles from Frank Lam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Frank Lam
Frank Lam