🎨 CSS: Fix an aspect-ratio Box Collapsing When Content Overflows
aspect-ratio sets a preferred size relationship between width and height, but it isn’t a hard clamp on the box — other layout rules can still win. Inside a flex container, a flex item’s default min-height: auto lets its content push the box taller than the ratio alone would produce, and if the content also overflows without any overflow handling, the box can end up looking like it never respected the ratio at all, when really the ratio was respected right up until something else overrode it.
🔎 The Problem
.thumb {
aspect-ratio: 16 / 9;
display: flex;
/* default min-height: auto on a flex item lets content
stretch the box past the ratio's intended height */
}
.thumb p {
margin: 0; /* long caption text still forces extra height */
}
✅ Fix: Give the Content Somewhere to Go Besides Growing the Box
- Add
min-height: 0(ormin-width: 0for row layouts) on the flex item so its content stops being able to override the ratio-derived size through the default auto minimum. - Set
overflow: hidden(or a scrollable overflow value, if that’s the intent) on the ratio box so content that genuinely doesn’t fit is contained or scrollable instead of silently stretching the box. - If the box sits inside a grid or flex parent using stretch alignment, set
align-self: start(or the equivalent) on it so the parent’s stretch behavior doesn’t compete with the ratio you’ve already declared.
🚨 Why This Is Dangerous
- A ratio box that silently grows past its intended height on long content can break a card grid’s alignment across an entire row, since every sibling box keeps its correct height while just the one with overflowing content stretches.
aspect-ratio tells the box what shape it would like to be, not what shape it’s guaranteed to be — content, min-height, and the parent’s layout mode all still get a vote.
