CSS Academy · Lesson

The fr Unit and Repeat

Use the fractional fr unit and the repeat() function to create flexible grid tracks.

Lesson 2 of 413 steps

The fr Unit and Repeat is a free CSS Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

What is the fr Unit?

The fr (fraction) unit is exclusive to CSS Grid. It represents a fraction of the remaining free space in the grid container after all non-flexible tracks are sized.

Simple fr Example

Two equal columns each getting half the available space:

<div class="grid">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">Column 1 (1fr)</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">Column 2 (1fr)</div>
</div>

Mixed Tracks

fr space is calculated after fixed tracks are subtracted:

<div class="layout">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">200px sidebar</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">1fr column</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">1fr column</div>
</div>

Unequal Fractions

Different fr values create proportional columns:

<div class="thirds" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">25% (1fr)</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">50% (2fr)</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">25% (1fr)</div>
</div>

The repeat() Function

repeat() avoids repeating the same track size. The first argument is the count; the second is the track size.

<div class="grid" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">1</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">2</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">3</div>
  <div style="background:#ff6b6b22;border:1px solid #ff6b6b;padding:12px;border-radius:4px;">4</div>
</div>

repeat() with Mixed Tracks

You can repeat a pattern of tracks:

<div class="grid" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">1fr</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">2fr</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">1fr</div>
  <div style="background:#ff6b6b22;border:1px solid #ff6b6b;padding:12px;border-radius:4px;">2fr</div>
  <div style="background:#b57bff22;border:1px solid #b57bff;padding:12px;border-radius:4px;">1fr</div>
  <div style="background:#3fd0c922;border:1px solid #3fd0c9;padding:12px;border-radius:4px;">2fr</div>
</div>

auto-fill

repeat(auto-fill, ...) creates as many columns as fit the container at the given size. Empty tracks are preserved at the end.

<div class="grid" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">200px</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">200px</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">200px</div>
</div>

auto-fit

repeat(auto-fit, ...) like auto-fill but collapses empty tracks, allowing existing items to stretch to fill the container.

<div class="grid" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">min 200px, grows</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">min 200px, grows</div>
</div>

minmax() in repeat

minmax(min, max) defines a track size range. Combined with auto-fit it creates a responsive grid with no media queries:

<div class="responsive-grid">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:12px;border-radius:4px;">Card A</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:12px;border-radius:4px;">Card B</div>
  <div style="background:#7ee78722;border:1px solid #7ee787;padding:12px;border-radius:4px;">Card C</div>
</div>

fr vs Percentage

Unlike percentages, fr automatically excludes gap space. 1fr 1fr with a 24px gap gives two equal columns without any math. Equivalent percentage math would be calc(50% - 12px).

fr with min-content max-content

Combine fr with content-based sizing for mixed layouts:

<div class="grid" style="display:grid;">
  <div style="background:#4f8cff22;border:1px solid #4f8cff;padding:8px;">Label</div>
  <div style="background:#ffb84d22;border:1px solid #ffb84d;padding:8px;">Flexible content area</div>
  <div style="background:#7ee78722;border:1px solid #34c759;padding:8px;">Action</div>
</div>

Quick Check

What does repeat(auto-fit, minmax(200px, 1fr)) create?

Recap

The fr unit distributes remaining grid space proportionally. repeat() avoids repetitive track declarations. The powerful repeat(auto-fit, minmax(250px, 1fr)) pattern creates fully responsive card grids with no media queries needed.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “The fr Unit and Repeat” lesson free?

Yes — the full text of “The fr Unit and Repeat” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.

What will I learn in “The fr Unit and Repeat”?

Use the fractional fr unit and the repeat() function to create flexible grid tracks. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The fr Unit and Repeat” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Display Grid and Template Columns Rows
  2. The fr Unit and Repeat
  3. Gap and Grid Placement
  4. Grid Template Areas
← Back to CSS Academy