🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Grid-template-columns

AI & DATA SCIENCE // grid-template-columns

The grid-template-columns property defines the number and size of a grid container's columns, accepting fixed lengths, percentages, and the flexible fr unit representing a fraction of available space.

Syntax

grid-template-columns: value value value...;

Deep Dive Course

Each length value listed defines one column's width, so grid-template-columns: 200px 1fr 1fr creates exactly three columns, a fixed 200px first column followed by two equally-sized flexible columns sharing the remaining space evenly. The fr unit represents a fraction of the grid container's remaining available space after any fixed-size tracks have been subtracted, making it especially useful for flexible, responsive column sizing, and the repeat() function, like repeat(3, 1fr), provides a more concise shorthand for defining several identically-sized columns without listing 1fr three separate times.

1Understanding Grid-template-columns

Each length value listed defines one column's width, so grid-template-columns: 200px 1fr 1fr creates exactly three columns, a fixed 200px first column followed by two equally-sized flexible columns sharing the remaining space evenly. The fr unit represents a fraction of the grid container's remaining available space after any fixed-size tracks have been subtracted, making it especially useful for flexible, responsive column sizing, and the repeat() function, like repeat(3, 1fr), provides a more concise shorthand for defining several identically-sized columns without listing 1fr three separate times.

💡

Use repeat(auto-fit, minmax(200px, 1fr)) for a common, genuinely responsive grid pattern that automatically fits as many 200px-minimum-width columns as will comfortably fit the container, without needing a separate media query breakpoint for each screen size.

editor.html
.grid {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
}
localhost:3000

2Practical Example

Here is a real-world application of Grid-template-columns showing how it is used in production CSS code.

editor.html
.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Grid-template-columns:

1. Use the fr unit for flexible columns that should share the container's available space proportionally, rather than fixed pixel widths that don't adapt to different container sizes

2. Use repeat() for concisely defining several columns of the same size, rather than manually listing that same size value multiple times

3. Combine repeat(auto-fit, minmax(...)) for a responsive grid that automatically adjusts its number of columns based on available space, without needing explicit media query breakpoints

⚠️

Tip: Use repeat(auto-fit, minmax(200px, 1fr)) for a common, genuinely responsive grid pattern that automatically fits as many 200px-minimum-width columns as will comfortably fit the container, without needing a separate media query breakpoint for each screen size.

editor.html
.grid {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
}
localhost:3000

Examples

Example 01Basic Usage
.grid {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
}
Example 02Advanced Example
.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

Best Practices

  • Use the fr unit for flexible columns that should share the container's available space proportionally, rather than fixed pixel widths that don't adapt to different container sizes
  • Use repeat() for concisely defining several columns of the same size, rather than manually listing that same size value multiple times
  • Combine repeat(auto-fit, minmax(...)) for a responsive grid that automatically adjusts its number of columns based on available space, without needing explicit media query breakpoints

Interview Question

Why does grid-template-columns: 200px 1fr 1fr allocate different amounts of space to the fixed and flexible columns, and how exactly is the space divided between the two 1fr columns?

Hint: Think about the order in which fixed-size tracks and flexible fr tracks are actually accounted for when the browser calculates the final column widths.

The browser first allocates space to every fixed-size track, here the 200px first column, subtracting that fixed amount directly from the container's total available width, and only afterward distributes whatever space genuinely remains among the flexible fr tracks, proportionally according to each one's fr value. Since both remaining columns are specified as 1fr, an equal fraction of that leftover, post-fixed-column space, they receive exactly half of whatever remains between them each — for a 1000px-wide container, that's 1000px minus the fixed 200px, leaving 800px to split evenly, giving each 1fr column exactly 400px. This two-step calculation, fixed tracks subtracted first, then the fr unit dividing up whatever's genuinely left over, is precisely why fr represents a proportional fraction of remaining space specifically, not a fraction of the container's total original width, which is exactly the distinction that makes fr so useful for building layouts that combine both fixed and flexible columns together predictably.

Exercises

MediumPractice using Grid-template-columns in a real scenario.
View Solution
.grid {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
}

Frequently Asked Questions

Why does grid-template-columns: 200px 1fr 1fr allocate different amounts of space to the fixed and flexible columns, and how exactly is the space divided between the two 1fr columns?

The browser first allocates space to every fixed-size track, here the 200px first column, subtracting that fixed amount directly from the container's total available width, and only afterward distributes whatever space genuinely remains among the flexible fr tracks, proportionally according to each one's fr value. Since both remaining columns are specified as 1fr, an equal fraction of that leftover, post-fixed-column space, they receive exactly half of whatever remains between them each — for a 1000px-wide container, that's 1000px minus the fixed 200px, leaving 800px to split evenly, giving each 1fr column exactly 400px. This two-step calculation, fixed tracks subtracted first, then the fr unit dividing up whatever's genuinely left over, is precisely why fr represents a proportional fraction of remaining space specifically, not a fraction of the container's total original width, which is exactly the distinction that makes fr so useful for building layouts that combine both fixed and flexible columns together predictably.

Related Functions

Display-gridGrid-template-rowsGrid-gap