Skip to content
Tutorials Center
My learning

Topics

Lesson 72 of 139 ยท Flexbox

Flexbox Patterns

Build five real-world Flexbox patterns: a navbar, a media object, equal-height cards with aligned footers, a sticky footer and wrapping tags.

You now know every Flexbox property. The fastest way to make them stick is to see how a handful of them combine into the components you build again and again. This lesson is a pattern book: five layouts that show up on almost every website, each one explained line by line.

Every pattern here is short. That's the point. Most real Flexbox layouts use only three or four properties; the skill lies in knowing which three or four. Edit each example freely and resize the preview to see how it behaves at different widths.

Pattern 1: the navigation bar

A typical navbar has a logo on one side, links in the middle or on the right, and maybe a button at the far end. You could use justify-content: space-between, but that only works neatly with exactly two or three groups. A more flexible tool is the auto margin.

In a flex container, an item with margin-left: auto (or margin-inline-start: auto) absorbs all the free space on its left side. It and everything after it get pushed to the end of the line.

Navbar with an auto margin

align-items: center lines up the bigger brand text with the small links. The push class on "Sign in" splits the bar into a left group and a right group with no extra wrapper elements. flex-wrap: wrap plus a two-value gap keeps things tidy on narrow screens: items drop to a second line with sensible spacing instead of overflowing. For a full mobile menu with a toggle, see Project: Responsive Navigation Bar.

Pattern 2: the media object

The media object is an image (an avatar, an icon, a thumbnail) on one side and a block of text on the other. Comments, notifications, search results and chat messages all use it.

Media object

Three details make this robust:

  • flex: none on the image means it never grows or shrinks, so the avatar stays a perfect 48px circle.
  • flex: 1 and min-width: 0 on the text let it take the remaining space and shrink below the length of that long URL. Without min-width: 0, the link would push the card wider than its container (the automatic minimum size from Flex Item Properties).
  • align-items: flex-start keeps the avatar at the top instead of stretching it to the full height of the text.

To put the image on the right instead, set flex-direction: row-reverse or simply move the <img> after the text in the HTML, which is better if the order also matters for reading.

Pattern 3: card rows with aligned footers

Cards in a row usually have different amounts of text, but you want them the same height with their buttons lined up along the bottom. Flexbox does both:

  1. The row is a flex container. Its default align-items: stretch makes every card as tall as the tallest one.
  2. Each card is a flex container too, with flex-direction: column.
  3. The card's body gets flex: 1 (or the footer gets margin-top: auto), which pushes the footer to the bottom.
Equal-height cards

flex: 1 1 160px gives each card a 160px starting size, lets it grow to share the row, and lets the row wrap when cards would get narrower than that. margin-top: auto on the link swallows the spare height in each card. align-self: flex-start stops the button from stretching across the full card width, which a column's default stretch would otherwise do.

Narrow the preview until a card wraps. The lone card on the second row stretches to full width, because each line is flexed separately. If you'd rather every card keep the same width, that's a sign you want a grid; see Grid Patterns.

On a page with little content, the footer should sit at the bottom of the window rather than floating halfway up. When the content is long, it should come after the content as usual. This is the sticky footer (not to be confused with position: sticky).

The Flexbox solution makes the page a column at least as tall as the viewport, and lets the main area grow:

CSS
body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  margin: 0;
}
main {
  flex: 1;
}

The second min-height uses the dvh unit, which accounts for mobile browser toolbars; older browsers ignore it and keep the vh line. Both units are explained in Viewport Units.

Sticky footer

Click the button a few times. While the content is short, main grows to fill the gap. Once it is taller than the viewport, the page simply scrolls and the footer follows the content. You can build the same layout with Grid using grid-template-rows: auto 1fr auto, as shown in Grid Patterns.

Pattern 5: wrapping tags and button groups

Tags, chips, filters and groups of buttons are lists of small, unequal items that should flow onto as many lines as needed. Wrapping plus gap handles it in two lines of CSS:

Wrapping tags

The tags are a real <ul> so screen readers announce "list, 7 items". list-style: none and padding: 0 remove the bullets and indent; see Styling Lists for more on removing bullets accessibly. The action buttons use justify-content: flex-end to sit at the right, and when the preview is very narrow they wrap rather than overflow.

Choosing the right tool

All five patterns have something in common: the content's size matters, and items arrange themselves along one direction. That is Flexbox territory. When you notice you need items in different rows to line up as columns, or you want to place things into a fixed two-dimensional structure, Grid is the better fit. You start learning it in Grid Basics.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a flex navbar, how do you push the last two links to the right side while the rest stay on the left?

    An auto margin absorbs all the free space before that item, so it and every item after it move to the end of the line.
  2. 2. In a media object, why do we add min-width: 0 to the text column?

    Flex items default to a minimum size equal to their content. min-width: 0 lets the text column shrink, so overflow-wrap can break the long URL.
  3. 3. Which combination pushes each card's button to the bottom of equal-height cards?

    The row stretches the cards to equal height; inside each column card, the auto top margin soaks up the spare space above the button.
  4. 4. In the sticky footer pattern, which element gets flex: 1?

    main grows to fill any spare height in the column, pushing the footer to the bottom of the viewport when the content is short.

Summary

  • Navbar: display: flex, align-items: center and an auto margin to split items into groups; add flex-wrap and gap for narrow screens.
  • Media object: a fixed flex: none image beside a flex: 1; min-width: 0 text block, aligned to the top.
  • Card row: the row stretches cards to equal height; each card is a column flex container whose button uses margin-top: auto.
  • Sticky footer: a column body with min-height: 100vh (or 100dvh) and flex: 1 on main.
  • Tags and button groups: flex-wrap: wrap plus gap, with semantic list markup where it fits.
  • When items need to line up in two dimensions, move on to Grid.

Next: Grid Basics, where layouts start working in rows and columns at the same time.