AZ.dev

Stop a flex or grid child from overflowing its container

| browsers: all modern

Set min-width: 0 on the child that refuses to shrink. Flex and grid items default to min-width: auto, which means “never smaller than my content”, so a long URL, a wide <pre> or a table pushes the item past its track.

.cell {
  min-width: 0;
}

For a column layout that overflows vertically, the same rule is min-height: 0.

How to recognise it

In every case the container is the right width; the item inside it is wider than its share.

Why auto is the default

The spec sets min-width: auto on flex and grid items so that content is never clipped by accident. It’s the right default for a button label, and the wrong one for anything holding user or code content. Set it to zero on the layout cell, then handle the content inside with overflow: auto, overflow-wrap: anywhere or truncation, whichever the content wants.

.main {
  min-width: 0;
}
.main pre {
  overflow-x: auto;
}

Gotchas

flexbox grid overflow min-width truncation

This entry is one card on a printable cheat sheet:

CSS & Layout Essentials Cheat Sheet

Was this helpful?

Related Entries

Responsive grid columns with auto-fit or auto-fillWhy position sticky isn't sticking