← Back to Learning Path Level 1 • Lesson 3 of 3
Level 1 progress 0% complete

Lesson 3: Understanding FR Units & Repeat

Master Grid's most powerful features: flexible fractions and the repeat() function.

What Are FR Units?

The fr unit stands for fraction and is Grid's secret weapon for creating flexible layouts. It represents a fraction of the available space in the grid container.

Think of fr units like slicing a pie. If you have 1fr 1fr 1fr, you are dividing the space into three equal slices. Each column gets one slice.

How FR Units Work

Example 1: Equal Columns

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

This creates three columns that each take up one fraction of space. Since all fractions are equal (1:1:1), you get three equal columns.

Example 2: Unequal Columns

.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
}

Now we have a ratio of 1:2:1. The middle column gets 2 fractions while the side columns get 1 each. If the container is 800px wide, the columns would be 200px, 400px, and 200px.

The Math Total fractions: 1 + 2 + 1 = 4
Column 1: (1/4) × 800px = 200px
Column 2: (2/4) × 800px = 400px
Column 3: (1/4) × 800px = 200px

Mixing FR with Other Units

One of the most powerful features of fr units is that you can mix them with fixed units such as pixels or percentages.

Fixed Sidebar + Flexible Content

.container {
  display: grid;
  grid-template-columns: 250px 1fr;
  gap: 20px;
}

This creates a fixed 250px sidebar and a flexible content area. The content area takes up whatever space is left after the sidebar and gap are accounted for.

Holy Grail Layout

.container {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  gap: 20px;
}

Two fixed sidebars, 200px each, with a flexible content area in the middle. This is a classic three-column layout that works on any screen size.

The Repeat() Function

Writing 1fr 1fr 1fr 1fr 1fr gets tedious. The repeat() function is a shorthand for repeating patterns.

Basic Repeat

/* Instead of this: */
grid-template-columns: 1fr 1fr 1fr 1fr;

/* Write this: */
grid-template-columns: repeat(4, 1fr);

repeat(4, 1fr) means "repeat 1fr four times." Much cleaner.

Repeat with Patterns

You can repeat more complex patterns too:

/* Creates: 1fr 2fr 1fr 2fr 1fr 2fr */
grid-template-columns: repeat(3, 1fr 2fr);

Mixing Repeat with Other Values

/* Creates: 200px 1fr 1fr 1fr 300px */
grid-template-columns: 200px repeat(3, 1fr) 300px;

This creates a fixed left sidebar (200px), three equal flexible columns, and a fixed right sidebar (300px).

Pro Tip Use repeat() whenever you have more than two or three identical columns. It makes your code much easier to read and maintain.

Practical Examples

Card Grid (4 Columns)

.card-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
}

Perfect for product listings, image galleries, or blog post grids.

Dashboard Layout

.dashboard {
  display: grid;
  grid-template-columns: 200px repeat(3, 1fr);
  grid-template-rows: 60px 1fr;
  gap: 16px;
}

Sidebar navigation (200px), header (60px), and three flexible content columns. Good for admin panels or dashboards.

Blog Layout with Sidebar

.blog-layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 40px;
}

Main content area gets 2 fractions, two thirds of the width, and the sidebar gets 1 fraction, one third of the width. This maintains a 2:1 ratio on any screen size.

FR Units vs Percentages

You might wonder why not just use percentages. Here is why fr units are better:

With Percentages (Problems)

/* With gap, this breaks. */
grid-template-columns: 33.33% 33.33% 33.33%;
gap: 20px;

The columns add up to 100%, but the gap takes up additional space. Your columns will wrap or overflow.

With FR Units (Works Correctly)

/* Gap is automatically accounted for */
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;

Grid automatically subtracts the gap space before calculating column widths, so everything just works.

Common Mistake Do not mix fr units with auto unless you understand how they interact. Auto sizes based on content, while fr divides remaining space. This can lead to unexpected results.

Advanced Pattern: Responsive Grid

Here is a powerful pattern that creates a responsive grid without media queries:

.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

This creates as many columns as will fit, with each column at least 250px wide but able to grow to fill available space. This is covered in detail in the Responsive Grids lesson.

Quick Reference

What You've Learned

You now understand Grid's most powerful features. With fr units and repeat(), you can create flexible, maintainable layouts that work on any screen size. In the next level, we will use these tools to build real-world layouts.

Practice Time

Try these challenges in the generator: create a grid with 1fr 2fr 1fr and see how the middle column is wider, use repeat(5, 1fr) to create 5 equal columns, then try 200px 1fr 300px and notice how the center is flexible.

Open Generator

Quick Check

Test your knowledge before completing Level 1. All 3 questions must be correct to continue.

1. In grid-template-columns: 1fr 3fr 1fr, what is the ratio of column widths?

2. What does repeat(4, 1fr) create?

3. Why are FR units better than percentages for Grid?

← Previous: Lesson 2