Build common navigation, card, and media layouts.
These examples use Flexbox for common layouts. For each one, choose a direction, decide which items can grow, and allow wrapping where needed.
.nav {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.75rem 1.5rem;
}The links keep their natural sizes and move to another line when needed. In this row layout, the first gap value separates lines and the second separates items on each line. Keep the links' source order meaningful when the bar wraps.
A media object places a small visual beside a larger body of text:
.media-object {
display: flex;
align-items: flex-start;
gap: 1rem;
}
.media-object-icon {
flex: 0 0 3rem;
}
.media-object-copy {
flex: 1 1 0;
min-width: 0;
overflow-wrap: anywhere;
}The icon stays at 3rem, while the text fills the remaining width. min-width: 0 lets the text box shrink, and overflow-wrap breaks long strings when needed.
For a container whose direct children are articles containing a heading, paragraph, and final link:
.card-row {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card-row > article {
display: flex;
flex-direction: column;
flex: 1 1 14rem;
min-width: 0;
overflow-wrap: anywhere;
}
.card-row > article > a {
margin-block-start: auto;
}The outer row shares space among the cards. Each card is also a flex container, with its children in a column. Cards stretch to the same height on each line, and the link’s auto margin pushes it to the bottom.
Heights and widths can differ between separate wrapped lines. Use Grid when cards need shared columns across rows.
.empty-state {
display: flex;
min-height: 16rem;
justify-content: center;
align-items: center;
}With a single child, both axes are centered. The minimum height provides vertical room while allowing the container to grow with larger content.
A toolbar contains links of different lengths. Which example lets them move to a new line without assigning every link a fixed width?
The wrapping navigation uses flex-wrap: wrap; the links retain their natural sizes and form another line when needed.
In the card example, why does the action need an auto margin inside each card rather than on the outer card row?
The action is a direct item of the card's column. Its auto margin consumes spare space in that column; the outer row arranges the cards themselves.
Try it yourself
HTML + CSS