SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
FlexboxDirection and WrappingFlex Item SizingMain-Axis AlignmentCross-Axis AlignmentFlexbox PatternsDebugging Flexbox Sizing and aligning a flex layout
Flexbox Layouts

Flexbox Patterns

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.

A wrapping navigation bar

.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 with flexible text

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.

A card row with actions at the bottom

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.

Center a single item

.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.

Choose a pattern

A toolbar contains links of different lengths. Which example lets them move to a new line without assigning every link a fixed width?

Check your reasoning

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?

Check your reasoning

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.

Key Takeaways

  • Navigation: Wrap natural-width links when the available space decreases.
  • Media objects: Keep one part stable while another fills the remaining space.
  • Nested containers: Use a separate flex layout inside each card.
  • Centering: Align on both axes and provide enough space for the effect.

Try it yourself

Try it yourself

Center an empty state

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

How is this lesson?

Previous lessonCross-Axis AlignmentNext lessonDebugging Flexbox
Edit on GitHubReport on GitHub
On this page
%

On this page

A wrapping navigation barA media object with flexible textA card row with actions at the bottomCenter a single itemChoose a patternKey TakeawaysTry it yourself