Flex and grid items stretch to fill the cross axis by default because align-items starts as stretch. Learn how to stop it, or use it to build equal-height cards.
The short answer
#Flexbox and grid stretch their items along the cross axis by default. In a row, that means every item becomes as tall as the tallest one. Set align-items on the container, or align-self on one item, to something other than stretch.
_10.row {_10 display: flex;_10 align-items: flex-start; /* items keep their own height */_10}_10_10.grid {_10 display: grid;_10 grid-template-columns: repeat(3, 1fr);_10 align-items: start;_10}
Why it happens
#In a flex container, the initial value of align-items is stretch. Any item whose cross size is auto (its height, in a row) grows to fill the line. The line is as tall as its tallest item, so every item matches it.
Grid works the same way inside each grid area. The initial value is normal, which behaves as stretch for most items. Each row is as tall as its tallest item, and every item in the row stretches to fill its cell. Items with an intrinsic aspect ratio, such as images, are the exception in grid: they keep their ratio instead of stretching.
This default is deliberate. It is the reason equal-height columns, which used to need table hacks, take one line of flexbox. It only becomes a problem when you did not want it: a short button next to tall text, a sidebar that fills the whole page height, or an image stretched out of shape in a flex row.
How to fix it
#Change align-items on the container
Pick the alignment you want for all items.
_10.toolbar {_10 display: flex;_10 align-items: center; /* centre each item vertically */_10}
In flexbox, use flex-start, center, flex-end or baseline. In grid, use start, center, end or baseline. Modern browsers also accept start and end in flexbox.
Override one item with align-self
If most items should stretch but one should not, set align-self on that item.
_10.media {_10 display: flex;_10 gap: 1rem;_10}_10_10.media img {_10 align-self: flex-start; /* keep the image's natural height */_10}
Give the item an explicit height
Stretch only affects items with an auto cross size. Setting height (or width in a column layout) on an item stops it stretching. This is useful when the size is fixed by design, but alignment properties are usually cleaner.
Use the stretch on purpose for card layouts
When you do want equal heights, keep the default and make each card a flex column, so its footer sits at the bottom whatever the content length.
_14.cards {_14 display: grid;_14 grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));_14 gap: 1.5rem;_14}_14_14.card {_14 display: flex;_14 flex-direction: column;_14}_14_14.card__footer {_14 margin-top: auto; /* pushes the footer down */_14}
Common pitfalls
#- Looking at the item for the cause. The stretching comes from the container's
align-items, so inspect the parent. - Mixing up axes. In a column flex layout, the cross axis is horizontal, so items stretch to full width, not height.
justify-contentcontrols the main axis;align-itemscontrols the cross axis. - Wrapped flex rows. With
flex-wrap: wrap, each line stretches its items to that line's height only, so different rows can have different heights. - Images in a flex row. An
imgwithautoheight is stretched to the row height and distorted. Usealign-self: flex-startorobject-fit: cover. - A page-level sidebar that fills the full height of a long main column. That is stretch working as intended; set
align-self: startand, if needed,position: sticky.
Related reading
#Was this resource helpful?