LEARNING OBJECTIVES ⌵
- Construct explicit grid column and row tracks using
grid-template-columnsandgrid-template-rows. - Apply content-driven sizing keywords:
auto,min-content,max-content, andfit-content(). - Simplify repetitive track declarations using the
repeat()function and recurring patterns. - Assign explicit custom names to grid lines using square bracket syntax (
[line-name]).
📖 The Mental Model & Story (Intuitive Foundation)
Imagine an architect drafting the blueprint for a multi-tenant corporate office building. The blueprint has a vertical floor plan consisting of:
- A rigid 280-pixel utility column on the left for elevators and electrical conduit (fixed size).
- A flexible central atrium that expands to fill whatever real estate the city plot permits (flexible size).
- A right-hand reception alcove whose width is strictly determined by the exact length of the reception desk inside it (content-based sizing).
In CSS Grid, grid-template-columns and grid-template-rows are the architectural blueprints. You don't just specify uniform boxes; you define custom track recipes combining fixed pixels, dynamic percentages, content-aware boundaries, and repeating patterns.
Technical Deep Dive & Specifications
The grid-template-* Property Suite
.container {
display: grid;
/* 3 Columns: 200px fixed, flexible remaining, 150px fixed */
grid-template-columns: 200px 1fr 150px;
/* 2 Rows: 60px header, auto content */
grid-template-rows: 60px auto;
}
Track Sizing Values & Sizing Keywords
| Unit / Keyword | Type | Mechanics & Calculation |
|---|---|---|
Fixed Lengths (px, rem) |
Absolute | Creates rigid, non-collapsible tracks regardless of container or content size. |
Percentages (%) |
Relative | Sized relative to the inner content box width/height of the grid container. |
fr Unit |
Fractional | Represents a fraction of the available free space remaining in the grid container. |
auto |
Content/Flexible | Expands to fit content; if free space exists, expands to absorb it (similar to minmax(min-content, max-content)). |
min-content |
Content-Intrinsic | The smallest possible track size that prevents text or content from overflowing (e.g. longest single word or smallest image). |
max-content |
Content-Intrinsic | The largest possible track size that allows content to render without any soft line breaks or text wrapping. |
fit-content(limit) |
Clamped Content | Equivalent to min(max-content, max(min-content, limit)). Sizes to content but caps at the specified limit. |
CONTENT SIZING COMPARISON
+-------------------------------------------------------------+
| min-content: "Responsive" | Track width = width of longest |
| "Web" | single word ("Responsive") |
| "Design" | |
+-------------------------------------------------------------+
| max-content: "Responsive Web Design" | No wrapping allowed |
+-------------------------------------------------------------+
| fit-content(300px): Sizes to max-content up to 300px max |
+-------------------------------------------------------------+
The repeat() Function Notation
Instead of typing repetitive track lists manually:
/* Verbose */
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
/* Concise repeat() */
grid-template-columns: repeat(6, 1fr);
Repeating Multi-Track Patterns
repeat() can repeat complex multi-track sequences:
/* Creates 6 tracks: 100px, 1fr, 2fr, 100px, 1fr, 2fr */
grid-template-columns: repeat(2, 100px 1fr 2fr);
Custom Grid Line Naming
By default, grid lines are referenced by numbers (1, 2, 3...). You can assign semantic names using bracket notation [name]:
.layout {
display: grid;
grid-template-columns:
[site-start sidebar-start] 260px
[sidebar-end main-start] 1fr
[main-end site-end];
grid-template-rows:
[header-start] 80px
[header-end content-start] auto
[content-end footer-start] 60px
[footer-end];
}
Note: A single grid line can have multiple alias names separated by whitespace inside [...].
💻 Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 26 (
display: grid): Creates the grid formatting context. - Line 32 (
grid-template-columns: 220px max-content 2fr 1fr):220px: Locks the first column for navigation.max-content: Tight-fits the second column to the un-wrapped width of the status badge.2fr 1fr: Distributes all remaining container width in a 2:1 ratio between the primary feed and telemetry panes.
- Line 33 (
grid-template-rows: 60px 240px): Declares a 60px header row and a 240px content row. - Line 47 (
grid-column: 1 / -1): Positions the header from line 1 to the final line-1, spanning all four columns across row 1.
Expected Browser Render Output
+-----------------------------------------------------------------------------------+
| GLOBAL HEADER (Row 1: 60px, spans lines 1 to 5) |
+-------------------+----------------------+--------------------+-------------------+
| Navigation | STATUS: LIVE STREAM | Primary Feed | Telemetry |
| (220px Fixed) | (max-content snug) | (2fr Free Space) | (1fr Free Space) |
+-------------------+----------------------+--------------------+-------------------+🏋️ Hands-On Exercise
🎯 The Challenge: Build a 5-Column Media Production Grid with repeat()
Instructions:
- Create a media production console
.media-console. - Define a 5-column grid where:
- Column 1 is fixed at
200pxfor Tools. - Columns 2, 3, 4 are 3 equal
1fraudio track columns defined usingrepeat(3, 1fr). - Column 5 is
fit-content(180px)for the Master volume meter.
- Column 1 is fixed at
- Define 2 rows: Row 1 is
50px, Row 2 is200px. - Apply a
0.75remgap.
🏁 Starter Code Sandbox
⚠️ Common Pitfalls
- Using Commas Inside
grid-template-columns: Writinggrid-template-columns: 100px, 1fr, 200px;. CSS Grid track lists are space-separated, not comma-separated. The comma will cause the entire rule to be ignored as invalid syntax. - Confusing
min-contentwithmax-content:min-contentcollapses tracks to the smallest unbroken word width, causing aggressive multi-line wrapping.max-contentforces tracks wide enough so no text wraps at all. - Overusing Fixed
pxfor Responsive Layouts: Hardcoding multiple pixel columns without flexible units (fr) will cause horizontal scrollbars on smaller viewports.
💡 Pro Tips
- Line Names for Semantic Code: Use semantic line names like
[content-start]and[content-end]. When complex responsive designs reorder items across media queries, referencing named lines keeps your CSS clean and self-documenting. - Nested
repeat()Limitations: Note thatrepeat()cannot be nested inside anotherrepeat(). Keep repeating patterns flat.
📌 Key Takeaways
grid-template-columnsandgrid-template-rowsdefine the explicit track coordinate system.- Track sizing accepts lengths (
px,rem), percentages (%), fractions (fr), and content keywords (auto,min-content,max-content,fit-content()). repeat(count, track_spec)removes code duplication for multi-column grids.- Custom line names are defined in square brackets
[name]between track sizing values. - CSS Grid track lists are space-separated, not comma-separated.
- --