Skip to content
All references

CSS reference

CSS Layout Patterns

Small Grid and Flexbox patterns for responsive interfaces, with overflow and accessibility considerations.

Choose a layout model from the content's behavior

Use Flexbox when arranging items along one primary direction, such as a toolbar that wraps. Use Grid when rows and columns need to form a coordinated structure, such as a card collection. Neither model removes the need to test long content, translated labels and narrow viewports. Begin with the behavior you want rather than copying the shortest CSS snippet.

A useful layout should adapt without hiding essential controls or forcing a whole-page horizontal scrollbar. Give flexible children room to shrink, keep readable text sizes and test the actual longest labels. Fixed widths can make a tidy screenshot while failing as soon as a user increases zoom or a description wraps onto another line.

  1. Identify the main axis or the repeated column structure. Select Flexbox for a one-dimensional arrangement and Grid for coordinated rows and columns.
  2. Choose a gap and a minimum useful item size. Let the layout wrap or reduce its column count when that size no longer fits.
  3. Check children containing code, long URLs or tables. Use local overflow handling rather than allowing them to widen the entire page.
  4. Test keyboard focus, zoom, translated labels and both sparse and full content before considering the layout finished.

Let cards form as many readable columns as will fit

Example input

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  gap: 1rem;
}

Expected result

1 → 2 → 3

The column count can grow as the container gets wider. Each card aims for at least sixteen rem, but the nested min permits a single card to fit a narrower container. The output indicates possible column counts, not fixed breakpoint values. Test the actual parent width and content rather than assuming the browser window and the card container have the same dimensions.

Check before you continue

  • An unbreakable URL or a child's minimum content width can still create overflow. Inspect the offending element instead of hiding all horizontal overflow globally.
  • Visual reordering can differ from keyboard and reading order. Keep the source order meaningful for assistive technology and keyboard users.
  • A generated layout snippet is a starting point. Integrate it with the surrounding component's sizing, focus and responsive rules.

6 of 6 entries

Grid

Responsive card grid

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: 1rem; }

Cards grow into available columns while the inner min() allows narrow screens.

Grid

Sidebar and content

.layout { display: grid; grid-template-columns: 16rem minmax(0, 1fr); gap: 1.5rem; }

Switch to one column at a suitable breakpoint. minmax(0, 1fr) lets content shrink.

Grid

Centered content

.center { display: grid; place-items: center; }

Centers items on both axes within the available container area.

Flexbox

Wrapping toolbar

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; }

Wrapping avoids squeezing actions off-screen.

Flexbox

Growing text column

.row { display: flex; gap: 1rem; } .row__content { flex: 1; min-width: 0; }

min-width: 0 allows long text or nested content to fit the row.

Accessibility

Visual ordering

/* Keep a meaningful DOM order. */
.actions { display: flex; gap: .5rem; }

Changing visual order does not necessarily change keyboard or screen-reader order.