日本語

Left-aligning the last row of cards with CSS space-between

​​CSS​CSS Grid,Flexbox,HTML,Playwright

When you lay out a card list, such as products or blog posts, with display: flex, flex-wrap: wrap, and justify-content: space-between, the cards in the last row can spread to the left and right edges and leave a large gap between them. If you want the cards to fill from the left column, switch to display: grid and combine grid-template-columns: repeat(auto-fill, 200px) with justify-content: space-between. The column positions then stay the same in every row, so the cards in the last row line up with the cards above them.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page containing only HTML and CSS with Playwright, changed the parent width and the number of cards, and measured the left edge of each card with getBoundingClientRect(). I did not test Firefox or Safari.

Example where the last-row cards split to the left and right

I placed 200px-wide cards inside a 680px-wide parent. Three cards fit in one row, and the rows are separated by 20px using row-gap.

html
<div class="cards">
  <div class="card">カード 1</div>
  <div class="card">カード 2</div>
  <!-- …up to Card 5 -->
</div>
css
.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  row-gap: 20px;
  width: 680px;
}

.card {
  width: 200px;
  height: 70px;
  box-sizing: border-box;
  border: 2px solid #00c8e8;
}

With five cards, the left edges of the three cards in the first row were at 0px, 240px, and 480px. Card 4, the first card in the second row, was at 0px, but Card 5 was placed at 480px instead of 240px, which left the middle column empty.

Top: with flex-wrap and space-between, Card 4 in the second row sits at the left edge and Card 5 at the right edge, and a pink dotted line marks the empty gap in the middle. Bottom: with grid auto-fill and space-between, Card 4 and Card 5 sit directly below Card 1 and Card 2 in the first row. The labels in this image are in Japanese.
The same five cards laid out with flex (top) and grid (bottom). The gray dotted line marks the parent’s bounds, and the pink dotted line was added to the image for illustration.

The way the layout broke changed with the number of cards.

Parent width Number of cards Left edges of last-row cards Appearance
680px (3 per row) 5 0px, 480px Gap in the middle
680px (3 per row) 6 0px, 240px, 480px Aligned
680px (3 per row) 7 0px The single card on the left is aligned
900px (4 per row) 6 0px, 700px Gap across the middle two columns

The layout does not break when the last row is exactly full or contains only one card. This is why the problem is easy to miss while the card count happens to fit, and it appears when the count changes.

flex distributes free space row by row

The cause is that justify-content works on each line separately. The Flexbox specification says that after items are divided into lines, each line is laid out independently, and justify-content looks only at the items on that line.

As described in MDN's justify-content reference, space-between places the first item at the start (left edge), places the last item at the end (right edge), and spaces the remaining items evenly. If the second row has only two cards, the first goes to the left edge and the second goes to the right edge. How many cards are in the rows above does not matter.

The same page also says that when a line contains only one item, that item is placed at the start. This is why the seventh card was placed on the left when there were seven cards.

With grid, the column positions stay the same in every row

When the parent is a grid, you define columns (tracks) with repeat(auto-fill, 200px) and distribute those columns with justify-content: space-between. The card CSS stays the same.

css
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, 200px);
  justify-content: space-between;
  row-gap: 20px;
}

With five cards, the left edges of Card 4 and Card 5 in the second row were 0px and 240px. They lined up directly below Card 1 and Card 2 in the first row. When I changed the parent width or the number of cards, every row started filling from the left column.

Parent width Number of cards Left edges in the first row Left edges in the last row
680px 5 0px, 240px, 480px 0px, 240px
760px 5 0px, 280px, 560px 0px, 280px
900px 6 0px, 233.3px, 466.7px, 700px 0px, 233.3px

The difference from flex is what justify-content distributes. In the CSS Box Alignment specification, the justify-content of a grid container distributes grid tracks (columns), not the items. The MDN guide to grid item alignment also says that when the total track size is smaller than the container, justify-content can move the tracks themselves along the inline axis. The extra space goes between columns, and each card sits in a column, so the cards in the last row land in the same positions as the cards above.

auto-fill creates columns even when there are few cards. With only two cards, the second card was placed at 240px, not at the right edge (480px) as it would be with flex's space-between. If you want to spread a partly filled first row to both edges, this approach does not fit.

If you do not need fixed card widths and can let the cards stretch to fill their columns, you only need to set a number of columns and divide them with 1fr.

css
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

With a 680px parent, each card is 213.3px wide. Card 4 and Card 5 in the second row are at 0px and 233.3px, aligned with the columns in the first row. With this layout, do not set width on the cards (in my test I reverted them to width: auto).

Adding an invisible card with ::after breaks when the column count changes

As a way to fix this while keeping flex, you can add an ::after pseudo-element to the parent that acts as an invisible card of the same width and is placed last.

css
.cards::after {
  content: "";
  width: 200px;
}

With three cards per row, this fixed the layout. With five cards, the second row contained Card 4, Card 5, and the ::after, so the spacing was distributed among those three, and Card 5 was placed at 240px. With four cards, Card 4 was still at 0px.

However, in my tests the approach broke in two cases.

  • With a 900px parent so that four cards fit per row and six cards total, the second row contained only Card 5, Card 6, and the ::after, so the spacing was distributed among those three. Card 6 was placed at 350px, which does not line up with the second column of the first row (233.3px).
  • With three cards per row and six cards total, the ::after moved to a third row, and the parent height increased from 160px to 180px. That is 70px + 20px + 70px for the two card rows, plus 20px from the row-gap above a row containing only the zero-height ::after.

For a list where the number of columns changes with the screen width, this approach needs an adjustment for each column count. Replacing it with grid keeps the columns aligned regardless of the column count.

Left-aligned with gap for spacing

If the space between cards does not need to stretch to fill the parent's width, you can omit justify-content (keeping the default start alignment) and use gap to space the cards.

css
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px 40px;
}

With a 680px parent, three 200px cards plus two 40px gaps fill the row exactly, and Card 5 in the second row was also placed at 240px. However, when I changed the parent width to 760px, the cards stayed at 0px, 240px, and 480px, and an 80px empty space remained on the right. If you want both edges aligned, choose the grid approach.

​Posted by 86