Home >Web Front-end >CSS Tutorial >How Can I Ensure Even Spacing in the Last Row of a Flexbox Grid with `justify-content: space-between`?

How Can I Ensure Even Spacing in the Last Row of a Flexbox Grid with `justify-content: space-between`?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-25 08:53:08379browse

How Can I Ensure Even Spacing in the Last Row of a Flexbox Grid with `justify-content: space-between`?

Flex-box: Aligning the Last Row of a Grid

In a flex-box container with flex-flow: row wrap; and justify-content: space-between;, the last row items may not be aligned correctly. This article explores a simple solution to align the last row with the other rows, maintaining the desired spacing.

The solution utilizes the ::after pseudo-element to autofill the remaining space in the container. By assigning flex: auto; to ::after, it fills the vacant space without any extra content in the HTML.

.grid {<br>  display: flex;<br>  flex-flow: row wrap;<br>  justify-content: space-between;<br>}</p><p>.grid::after {<br>  content: "";<br>  flex: auto;<br>}<br>

This approach provides a clean and straightforward solution, avoiding the need to add unnecessary elements to the HTML code. Check out the live demo at http://codepen.io/DanAndreasson/pen/ZQXLXj to witness its effectiveness.

The above is the detailed content of How Can I Ensure Even Spacing in the Last Row of a Flexbox Grid with `justify-content: space-between`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn