Flexbox patterns

Topic 5 of 5 in Flexbox6 min readBeginnerFree course

Overview

Flexbox is at its best for components: small, one-dimensional groups of things whose sizes come from their content. This lesson collects the patterns you’ll use again and again. Each is only a few lines once you know the pieces from the previous lessons.

Everyday flexbox patterns. A navigation bar — Logo, Courses and Pricing at the start, with margin-inline-start: auto pushing Log in and Sign up to the far end. A media object — a fixed image (flex: none) next to flexible text (flex: 1). A footer at the bottom — a flex column with header, main (flex: 1) and footer. Flexbox shines for one-dimensional rows of things whose sizes come from their content.

HTML
<header class="site-header">
  <a class="logo" href="/">Academy</a>
  <nav class="links">
    <a href="/courses">Courses</a>
    <a href="/pricing">Pricing</a>
  </nav>
  <a class="button" href="/signup">Sign up</a>
</header>
CSS
.site-header {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 1rem;
}
.links { display: flex; gap: 1rem; }
.site-header .button { margin-inline-start: auto; }
  • align-items: center lines up items of different heights (a big logo, small links).
  • The auto margin pushes the button to the far end while keeping the logo and links together.
  • flex-wrap: wrap on .site-header stops it overflowing on small screens (a full mobile menu is covered in Navigation layouts).

Media object

An image (avatar, thumbnail, icon) next to a block of text — comments, notifications, search results:

CSS
.media { display: flex; gap: 1rem; align-items: flex-start; }
.media > img { flex: none; width: 4rem; }
.media > .body { flex: 1; min-width: 0; }
  • flex: none keeps the image exactly its size — it neither grows nor gets squashed by long text.
  • align-items: flex-start stops the image stretching to the height of the text.
  • min-width: 0 lets the text shrink and wrap properly, even with long words or nested flex rows.

Swap the image to the other side with flex-direction: row-reverse only if reading order doesn’t matter; otherwise move it in the HTML.

Input group

A text field with attached buttons, like a search box or a “copy link” field:

CSS
.input-group { display: flex; }
.input-group input { flex: 1; min-width: 0; }
.input-group button { flex: none; }

/* join the borders */
.input-group > :not(:first-child) { margin-inline-start: -1px; }
.input-group > :first-child { border-start-end-radius: 0; border-end-end-radius: 0; }
.input-group > :last-child  { border-start-start-radius: 0; border-end-start-radius: 0; }

Button groups and toolbars

CSS
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;   /* buttons at the end, like a dialog's footer */
}

Wrapping lets the group survive narrow screens and longer translations.

On short pages, a footer should sit at the bottom of the window, not halfway up. Make the page a column and let the main content grow:

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

When content is short, main grows to fill the space and pushes the footer down. When content is long, the page simply scrolls. (Grid offers an equally short version — see Page layouts.)

Equal-height cards with bottom-aligned actions

Flex items in a row stretch to equal height by default. Make each card a column and push its footer down:

CSS
.cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.card {
  flex: 1 1 16rem;
  display: flex;
  flex-direction: column;
}
.card .actions { margin-block-start: auto; }

All the buttons line up along the bottom, whatever the length of each card’s text. (For card titles that line up too, you’ll want grid with subgrid — see Subgrid.)

Icon and text

Buttons, links and labels with icons:

CSS
.button {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
}
.button svg { flex: none; width: 1.25em; height: 1.25em; }

Using em for the gap and icon size keeps them proportional when the button’s font size changes.

Split header

A heading with an action at the other end, where the action wraps under the heading on narrow screens:

CSS
.section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem 1rem;
}

align-items: baseline lines up a large heading and a small “View all” link by their text, which looks far better than centring them.

Pattern spotting: when you see a row (or a column) of things that should share space, align, or wrap, think flexbox. When you see a grid of things that should line up in both directions, think grid — next chapter.

Exercise

A comment component

Build a .comment media object: a 3rem round avatar on the left that never shrinks, and a body that fills the remaining space. The body has a header row with the author name and a timestamp pushed to the right (both aligned on their text baseline), the comment text, and an .actions row of buttons with 0.5rem gaps. Long unbroken text (like a URL) must wrap instead of overflowing the comment.

HTML
<article class="comment">
  <img class="avatar" src="https://i.pravatar.cc/96?img=12" alt="" width="48" height="48" />
  <div class="body">
    <header class="meta">
      <strong class="author">Asha Rao</strong>
      <time class="time">2 hours ago</time>
    </header>
    <p>Grid finally clicked for me — this demo helped: https://example.com/a-really-long-link-that-keeps-going-and-going</p>
    <div class="actions"><button>Reply</button><button>Like</button><button>Share</button></div>
  </div>
</article>

<style>
  * { box-sizing: border-box; }
  .comment { max-width: 32rem; }
  .time { font-size: 0.8rem; color: #5b606b; }
  /* your code here */
</style>

Summary

  • Navigation bars: flex row, align-items: center, gap, and an auto margin to split groups.
  • Media object: a flex: none image beside a flex: 1; min-width: 0 body.
  • Input groups: a flexible input with fixed-size buttons attached.
  • Sticky footer and card footers: a column flex container with flex: 1 or margin-top: auto.
  • Wrap button rows and tag lists with flex-wrap: wrap and gap.