The problem with uneven columns
A balanced layout makes a website look polished and professional. A common challenge is getting a row of columns, such as feature boxes, pricing cards or product listings, to be the same height, however much content each one contains.
By default, each box is only as tall as its own content, so a column with more text ends up taller than its neighbours:
In this tutorial we'll look at three ways to make columns the same height with CSS, then at how to line up the content inside them too.
The flexbox approach
Flexbox (the Flexible Box Layout) is a CSS layout system for arranging items in a row or a column. It's a perfect fit for equal-height columns, because it makes them the same height automatically:
.columns {
display: flex;
gap: 16px;
}
.column {
flex: 1;
}
How it works
display: flexplaces the columns side by side in a row.- Flex items stretch to fill the height of the row by default (because
align-itemsdefaults tostretch). This is what makes them equal in height. flex: 1makes each column take an equal share of the width.gapadds space between the columns, and only between them, unlike margins, which would also add space at the outer edges.
Correction: the original version of this post said flex: 1 creates the same-height effect. It actually makes the columns equal in width; the equal height comes from flexbox's default stretching. The original also used margin for spacing, and gap is now the simpler choice.
The grid approach
CSS Grid is a layout system for arranging items in rows and columns. Items in the same grid row also stretch to the same height by default:
.columns {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 16px;
}
How it works
display: gridturns the container into a grid.repeat(auto-fit, minmax(160px, 1fr))creates as many columns as will fit, each at least 160px wide, sharing any extra space equally. On a narrow screen, the columns automatically wrap onto new rows, with no media queries needed.- Every item in a row stretches to match the tallest one in that same row.
auto-fit or auto-fill? The original post used auto-fill. Both create as many columns as fit, but they differ when there are fewer items than spaces: auto-fill leaves empty column slots, while auto-fit stretches the items to fill the row. For a row of cards, auto-fit is usually what you want.
The table approach
Before flexbox and grid existed, a common trick was to make elements behave like table cells, which always share the same height in a row:
.columns {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 16px 0; /* space between the cells */
}
.column {
display: table-cell;
}
display: table-cellmakes each column behave like a cell in a table row, so they all match the tallest one.table-layout: fixedgives the cells equal widths, rather than sizing them by their content.border-spacingadds space between cells, because table cells ignore margins.
This still works, but it's harder to make responsive: table cells won't wrap onto new rows on small screens. Today, flexbox or grid is almost always the better choice.
Good to know: using display: table on <div> elements only changes how they look. It doesn't turn them into a real data table for screen readers (software that reads pages aloud), so it's fine for layout. For actual tabular data, use a real <table>.
Lining up content inside cards
Equal-height boxes are a great start, but the content inside them can still look uneven. A classic example is a row of cards with a button at the bottom: the buttons end up at different heights.
Push buttons to the bottom
Make each card a flex column, then give the button margin-top: auto. The auto margin takes up all the spare space above the button, pushing it to the bottom:
.card {
display: flex;
flex-direction: column;
gap: 8px;
}
.card .button {
margin-top: auto;
}
Before, with the buttons wherever the text ends:
After, with margin-top: auto lining every button up along the bottom:
Align every part with subgrid
What if each card has a title of a different length? The titles, descriptions and other parts won't line up across the cards. Subgrid fixes this: each card shares the rows of the parent grid, so matching parts line up across every card.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 16px;
}
.card {
display: grid;
grid-row: span 3; /* each card covers 3 rows: label, title, text */
grid-template-rows: subgrid; /* ...and uses the parent's rows for them */
}
Without subgrid, each card lays out its parts independently. The titles (blue) and descriptions (green) start at different heights:
Everything you need to get started.
More storage and priority support.
For growing teams.
With subgrid, every title row is as tall as the tallest title, so the descriptions all line up:
Everything you need to get started.
More storage and priority support.
For growing teams.
Subgrid works in all current major browsers. In older ones, the cards simply lay out as normal, without the cross-card alignment.
Common gotchas
- Setting
align-itemsstops the stretching. Values likealign-items: centerorflex-starton the container switch off the equal heights, because items no longer stretch. - A fixed
heighton a column stops it stretching. Usemin-heightinstead if you need a minimum size. - Only the direct children stretch. If your visible card is wrapped in another element, the wrapper stretches but the card inside doesn't. Add
height: 100%to the inner card, or make the wrapper the card. - Grid rows match within a row only. When cards wrap onto a second row, each row has its own height.
Which approach should you use?
| Approach | Best for |
|---|---|
| Flexbox | A single row of columns, especially when they should share space flexibly |
| Grid | Rows of cards that should wrap neatly on smaller screens |
| Grid with subgrid | Cards whose inner parts (titles, prices, buttons) should line up across the row |
| Table display | Rarely needed today; mainly found in older code |
Wrapping up
Equal-height columns used to need clever hacks, but modern CSS makes them the default: flex and grid items stretch to match their tallest neighbour automatically. Use flexbox for a simple row, grid for wrapping cards, margin-top: auto to line up buttons, and subgrid when every part of every card should align.