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
Grid FundamentalsGrid TracksGrid Placement and Layout DecisionsImplicit GridsFlexible GridsGrid AlignmentGrid or Flexbox Placing items on a grid
CSS Grid

Grid or Flexbox

Choose a layout method for the problem at hand.

Ask what needs to line up. Flexbox works along a row or column, with each wrapped line arranged separately. Grid keeps rows and columns aligned together. You can use either for components or page layouts.

Compare the layout needs

NeedUseful starting pointReason
A row of navigation links with different text lengths.Flexbox.Items can keep natural widths and wrap independently.
A vertical stack with a final action pushed down.Flexbox.A column and an auto margin handle one main axis.
Cards whose columns must align across rows.Grid.Rows share the same column tracks.
A header spanning a sidebar and main region.Grid.Placement can describe both dimensions together.

These are starting points rather than exclusive rules. A component's size does not determine which system it should use.

Compare wrapping with shared columns

Assume five short articles are direct children of either example container:

.flex-collection {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.flex-collection > article {
  flex: 1 1 10rem;
  min-width: 0;
}

At a 640-pixel content width with a 16-pixel root font size, three cards fit on the first line and two on the second. Each line distributes its own remaining width, so the two cards on the second line grow wider than the first three.

.grid-collection {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
}

Here, all rows use the same three column tracks. The two cards on the final row keep those column widths and leave the third cell unoccupied. This numeric template always keeps three columns; use automatic repetition when the count should adapt.

At the same container width A, B, and C fill the first row of both collections. Flex items D and E grow to fill the second row. Grid items D and E stay aligned under A and B, leaving the cell under C empty.

The question is whether each line should distribute its own space or whether several rows must share column boundaries.

Decide from the constraint

A row of buttons should wrap, and each line should distribute its own remaining space. Which layout is a useful starting point?

Check your reasoning

Flexbox fits this constraint: each flex line distributes space independently. Grid can also wrap items, but shared columns are not required here.

Now change the requirement: cards on every row must share column boundaries. Which layout would you start with, and why?

Check your reasoning

Grid provides shared column tracks across rows. The key evidence is the alignment requirement, not the fact that the items are cards.

Combine them at different levels

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

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

The dashboard controls the card tracks. The nested actions container arranges its own buttons. Each layout controls its own direct children, so Grid and Flexbox can work together here.

Key Takeaways

  • Flexbox: Lets each line manage its own item sizing and space.
  • Grid: Provides shared tracks across rows and columns.
  • Combination: A grid item can contain a separate flex layout.
  • Choice: Decide what needs to line up, then test with real content.

Try it yourself

Try it yourself

Lay out a dashboard and its actions

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

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

Take the quiz · Placing items on a grid

How is this lesson?

Previous lessonGrid AlignmentNext lesson Placing items on a grid
Edit on GitHubReport on GitHub
On this page
%

On this page

Compare the layout needsCompare wrapping with shared columnsDecide from the constraintCombine them at different levelsKey TakeawaysTry it yourself