Grid can line up boxes in a row too
A recent discussion about flexbox and gap prompted a reminder: one of the everyday reasons people reach for flexbox is simply to put some boxes in a row and space them out a little.
That instinct isn't going away, and with gap available it probably shouldn't. Still, grid can accomplish the same layout in its own way.
.grid {
display: grid;
gap: 1rem;
grid-auto-flow: column;
}
The boxes may appear to be equal width in that example, but only because they hold no content. Once content is present, the boxes push against one another according to the natural width of that content. To rein that in, you can set width, min-width or max-width on the elements sitting in those columns — or define them through grid-template-columns without specifying the actual number of columns and let min-content dictate the width.
.grid {
display: grid;
gap: 1rem;
grid-auto-flow: column;
grid-template-columns: repeat(auto-fit, minmax(min-content, 1fr));
}
Flexible grids, as it happens, are the coolest.
One more thing worth keeping in mind: if you want the grid itself to be only as wide as its content — narrower than 100% or auto, when needed — display: inline-grid; exists for exactly that.



