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

Flexible Grids

Create flexible tracks with fr, repeat, minmax, and auto sizing.

A flexible grid adjusts its column widths and column count to the available space. Use fr to share space, minmax() to set limits, and repeat() to create columns.

Share leftover space with fr

.ratio {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
  gap: 1rem;
}

At a 616-pixel content width with a 16-pixel gap, 600 pixels remain for the tracks. They receive 200 and 400 pixels: one share and two shares.

minmax(0, 1fr) sets a zero track minimum instead of the automatic minimum of a bare 1fr. This allows the track to shrink, but the content inside it still needs to wrap or handle overflow.

Repeat equal tracks

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

This always defines three columns. They become narrower together as the container narrows; repetition with a numeric count does not change that count.

minmax() takes a minimum followed by a maximum. A flexible fr value is valid in the maximum position, but not the minimum position.

Let the number of columns adapt

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

.catalog > article {
  min-width: 0;
  overflow-wrap: anywhere;
}

The browser creates as many 14rem minimum columns as fit, accounting for gaps, then distributes extra width through the 1fr maximum. Empty repeated tracks collapse with auto-fit.

With a 16-pixel root font size and enough items:

Container widthColumns that fit
1000pxFour: the minimum widths plus three gaps take 944px.
700pxTwo: three columns and their gaps would take 704px.
300pxOne.

The count depends on container space, not the number of items on just the final row.

Predict a flexible grid

At 616px with a 16px gap, the earlier 1fr 2fr example has 600px to share. What happens if the second track changes to 1fr while both minimums remain zero?

Check your reasoning

Each track gets one of two equal shares: 300px. The gap is subtracted before the shares are divided.

Now consider repeat(3, minmax(0, 1fr)) at a narrower width. Does it change the number of columns?

Check your reasoning

No. The numeric repeat count stays at three; the tracks shrink. Automatic repetition is needed for the count to change.

Compare auto-fit and auto-fill

.reserved {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr));
  gap: 1rem;
}

Both initially create tracks that fit. The difference appears when whole repeated tracks are empty:

  • auto-fill: Keeps those empty tracks, reserving space for them.
  • auto-fit: Collapses them so occupied flexible tracks can expand.

At 1000px there is room for four tracks. With two items, auto-fill keeps two empty tracks, while auto-fit collapses them so the two occupied tracks expand.

auto-fit collapses a column only if the whole column is empty. An empty cell in the last row is not enough.

Allow a smaller container

A strict 14rem minimum overflows a container narrower than that value. To allow one track to become as narrow as its container, replace the catalog's template with:

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

The min() function lowers the minimum when the container is narrower than 14rem. Keep the content wrapping rules as well.

Key Takeaways

  • fr: Shares leftover track space after gaps.
  • minmax(): Sets track limits; fr belongs in the maximum position.
  • Automatic repetition: Changes the number of columns that fit.
  • Empty tracks: auto-fit collapses them; auto-fill retains them.

Try it yourself

Try it yourself

Fit as many cards as each shelf holds

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 lessonImplicit GridsNext lessonGrid Alignment
Edit on GitHubReport on GitHub
On this page
%

On this page

Share leftover space with frRepeat equal tracksLet the number of columns adaptPredict a flexible gridCompare auto-fit and auto-fillAllow a smaller containerKey TakeawaysTry it yourself