Tutorials CSS Grid Container Properties
Ch 30 / 60
Chapter 30 Beginner 10 min read CSS

CSS Grid Container Properties

Chapter 29 introduced Grid and the basics of defining tracks. This chapter goes deep on every property that belongs to the grid container — the parent with display: grid. These are the properties that define the grid structure: track sizes, named areas, implicit tracks, auto-fill magic, gaps and the shorthand.

What you'll learn
grid-template-columns & grid-template-rows — full syntax
grid-template-areas — visual ASCII layout
grid-auto-rows & grid-auto-columns
minmax() — minimum and maximum track sizes
auto-fill vs auto-fit — responsive no-media-query grids
gap, grid-auto-flow, grid shorthand

All Grid Container Properties — Quick Reference

Every property that belongs to the grid container (the parent with display: grid):

grid-template-columns
default: none
Defines explicit column track sizes. Accepts px, fr, %, auto, minmax(), repeat().
grid-template-rows
default: none
Defines explicit row track sizes. Without this, rows are created implicitly.
grid-template-areas
default: none
Names grid areas with ASCII layout strings. Items use grid-area to place themselves.
grid-template
shorthand
Shorthand for grid-template-rows, grid-template-columns, and grid-template-areas.
grid-auto-rows
default: auto
Size of implicitly-created row tracks. Applied when items wrap beyond defined rows.
grid-auto-columns
default: auto
Size of implicitly-created column tracks.
grid-auto-flow
default: row
Controls auto-placement direction: row, column, dense.
gap / row-gap / column-gap
default: 0
Spacing between tracks. Works identically to Flexbox gap.
grid
shorthand
Master shorthand for all grid container properties.

grid-template-columns & grid-template-rows

These two properties define the size of explicit tracks. A wide variety of value types can be mixed freely. Named line syntax also lets you reference tracks by name instead of number:

repeat(3,1fr) — 3 equal columns
1
2
3
4
5
6
80px 1fr 2fr — mixed units
fixed
1fr
2fr
fixed
1fr
2fr
explicit rows: 40px 80px 40px
40px
40px
80px
80px
40px
40px
named lines: [start] 1fr [mid] 1fr [end]
col-start→mid
col-mid→end
col-start→mid
col-mid→end

Lines named in brackets — reference by name

CSS — grid-template-columns & rows
.container {
  display: grid;

  /* ── Column definitions ── */
  grid-template-columns: repeat(3, 1fr);         /* 3 equal */
  grid-template-columns: 260px 1fr;              /* sidebar + main */
  grid-template-columns: 1fr 2fr 1fr;            /* proportional */
  grid-template-columns: 200px 1fr 200px;        /* fixed sides */
  grid-template-columns: auto 1fr auto;          /* content-sized sides */

  /* ── Named lines ── */
  grid-template-columns: [start] 1fr [center] 1fr [end];
  /* Place items: grid-column: start / center */

  /* ── Row definitions ── */
  grid-template-rows: 64px 1fr 48px;    /* header+content+footer */
  grid-template-rows: repeat(3, minmax(100px, auto));
}

grid-template-areas — Visual ASCII Layout

grid-template-areas is one of CSS Grid's most expressive features. You define the layout using quoted strings of area names — one string per row, spaces between columns. Items then self-place using grid-area: <name>:

header
sidebar
main
CSS — grid-template-areas
.page {
  display:              grid;
  grid-template-columns:260px 1fr;
  grid-template-rows:   64px 1fr 48px;
  grid-template-areas:
    "header  header"    /* row 1: header spans both cols */
    "sidebar main"      /* row 2: sidebar | main */
    "footer  footer";   /* row 3: footer spans both cols */
  min-height:         100vh;
}

/* Items reference area names */
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

/* ── Rules for grid-template-areas ──
   • Each area must be rectangular
   • Same name in multiple cells = item spans those cells
   • Use . (dot) for an empty cell
   • All rows must have the same number of cells */

/* Empty cell example */
.grid {
  grid-template-areas:
    "logo  .      nav"    /* . = empty middle cell */
    "hero  hero   hero";
}
Responsive areas with media queries
One of the most elegant uses of grid-template-areas is redefining the layout for different screen sizes. On mobile: stack everything in a single column. On desktop: switch to a sidebar layout — all by redefining the template strings in a media query. The HTML order is irrelevant.

minmax() — Minimum and Maximum Track Size

minmax(min, max) is a CSS Grid function that sets a minimum and maximum size for a track. The track can shrink to min but never below it, and grow up to max but never above it. It's the key to responsive grids without media queries.

minmax(100px, 1fr) — grows from 100px to fill available space
min:100px
max:1fr
min:100px
max:1fr
min:100px
max:1fr
minmax(200px, auto) — min 200px, grows with content
min:200px max:auto
min:200px max:auto
(taller content)
minmax(0, 1fr) — prevents track from overflowing with long content
min:0 max:1fr
min:0 max:1fr
min:0 max:1fr
CSS — minmax()
/* minmax(min, max) — track is between min and max */

.container {
  grid-template-columns: repeat(3, minmax(100px, 1fr));
  /* Each column: at least 100px, fills 1fr of space */
}

/* Most important use: responsive card grid */
.cards {
  display:              grid;
  grid-template-columns:repeat(auto-fill, minmax(250px, 1fr));
  gap:                  20px;
}
/* Cards are at least 250px; as many fit per row as possible */

/* Common minmax patterns */
.a { grid-template-columns: minmax(200px, 300px) 1fr; }  /* bounded sidebar */
.b { grid-template-rows:    minmax(100px, auto); }        /* min row height */
.c { grid-template-columns: repeat(4, minmax(0, 1fr)); }  /* no overflow */

/* minmax values:
   min: px, em, rem, % — NOT fr
   max: px, em, rem, %, fr, auto, max-content, min-content */

auto-fill vs auto-fit — Responsive Without Media Queries

The most powerful CSS Grid trick: repeat(auto-fill, minmax(200px, 1fr)) creates as many columns as fit in the container — all without any media queries. The grid automatically reflows as the viewport changes.

The only difference between auto-fill and auto-fit appears when there are fewer items than can fill a row:

auto-fill — keeps empty tracks
Item 1
Item 2
Item 3

Empty column tracks remain — items DON'T stretch to fill

auto-fit — collapses empty tracks ✓
Item 1
Item 2
Item 3

Empty tracks collapse to 0 — items fill the full width ✓

Live: repeat(auto-fill, minmax(clamp(100px,20vw,140px), 1fr)) — resize window to see it reflow
Card 1
Card 2
Card 3
Card 4
Card 5
Card 6
CSS — auto-fill, auto-fit and responsive card grid
/* ── The magic responsive grid — no media queries needed ── */
.card-grid {
  display:              grid;
  grid-template-columns:repeat(auto-fill, minmax(280px, 1fr));
  gap:                  20px;
}
/* Cards are at least 280px. As many as fit per row.
   Remaining space fills proportionally with 1fr. */

/* ── auto-fill vs auto-fit ── */
.fill { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
/* fewer items → empty tracks remain → items don't stretch */

.fit  { grid-template-columns: repeat(auto-fit,  minmax(200px, 1fr)); }
/* fewer items → empty tracks collapse → items fill full width */

/* Rule of thumb:
   Use auto-fill when you want consistent column count
   Use auto-fit  when you want items to fill available space */

grid-auto-rows & grid-auto-columns

When items overflow beyond your explicit row/column definitions, the browser creates implicit tracks. grid-auto-rows and grid-auto-columns control the size of these implicit tracks:

No grid-auto-rows — implicit rows auto-sized
Explicit 60px
Explicit 60px
Implicit: auto
(content height)
Implicit: auto
(content height)
grid-auto-rows: 80px — consistent height ✓
Explicit 60px
Explicit 60px
Implicit: 80px ✓
Implicit: 80px ✓
Implicit: 80px ✓
Implicit: 80px ✓
CSS — grid-auto-rows & grid-auto-columns
.container {
  display:              grid;
  grid-template-columns:repeat(3, 1fr);

  /* Fixed implicit row height */
  grid-auto-rows: 200px;

  /* Flexible — at least 100px, grows with content */
  grid-auto-rows: minmax(100px, auto);   /* ← best practice */

  /* Multiple auto-row values — alternating pattern */
  grid-auto-rows: 100px 200px;   /* short, tall, short, tall… */
}

/* grid-auto-columns: controls implicit column tracks
   (created by grid-auto-flow:column or out-of-range placement) */
.container { grid-auto-columns: minmax(100px, auto); }

gap, row-gap & column-gap

CSS Grid and Flexbox share the same gap syntax. gap adds space between grid tracks — never at the outer edges. Works for both rows and columns simultaneously:

gap: 0
1
2
3
4
5
6
gap: 8px
1
2
3
4
5
6
gap: 4px 12px (row col)
1
2
3
4
5
6
CSS — gap in Grid
.grid {
  display: grid;

  gap:        20px;         /* row AND column gap = 20px */
  row-gap:    12px;         /* between rows only */
  column-gap: 24px;         /* between columns only */
  gap:        12px 24px;    /* shorthand: row-gap col-gap */

  /* Responsive gap with clamp */
  gap: clamp(12px, 2vw, 24px);
}

/* gap does NOT add space at the outer edges —
   only between tracks */

grid-auto-flow — Auto-Placement Direction

grid-auto-flow controls how items that are not explicitly placed are automatically placed into the grid. The default is row — items fill left to right, then wrap to the next row.

grid-auto-flow: row (default)
1 →
2 →
3 ↓
4 →
5 →
6 ↓

Fills rows left to right

grid-auto-flow: column
1 ↓
2 →
3 ↓
4 →
5 ↓
6

Fills columns top to bottom

grid-auto-flow: dense — fills holes left by spanning items
Span 2
2
3
4
5
Span 2
6

dense: fills any gaps — items may appear out of DOM order

CSS — grid-auto-flow
.grid {
  display: grid;

  grid-auto-flow: row;         /* default — left to right, wrap down */
  grid-auto-flow: column;      /* top to bottom, wrap right */
  grid-auto-flow: row dense;   /* fills holes with later items */
  grid-auto-flow: column dense; /* column + fill holes */
}

/* dense: useful for masonry-like layouts where items
   have different spans. Fills any gaps left by large items.
   ⚠ Items may appear out of DOM/tab order — avoid for
   interactive content (links, buttons) */

All Container Properties — Complete Reference

Property Default Key Values Controls
grid-template-columns none fr, px, %, auto, minmax(), repeat() Explicit column track sizes
grid-template-rows none fr, px, %, auto, minmax(), repeat() Explicit row track sizes
grid-template-areas none quoted strings of area names, . = empty Named area layout
grid-template none / none / none Shorthand rows / cols + areas All three template props
grid-auto-rows auto px, fr, minmax(), auto Implicit row track size
grid-auto-columns auto px, fr, minmax(), auto Implicit column track size
grid-auto-flow row row, column, row dense, column dense Auto-placement direction
gap / row-gap / column-gap 0 px, em, rem, % Space between tracks
grid — Master shorthand (complex) All grid container props
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all CSS Grid container and item properties with visual examples.
Download PDF

Try It Yourself

Experiment with all grid container properties. Try the responsive auto-fill card grid, the areas-based page layout, and different gap values:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 30

Questions & Answers
grid-template-areas lets you define the layout visually using quoted strings of area names. Each string represents one row, and space-separated names within a string represent columns.

Items self-place by adding grid-area: <name> matching a name in the template.

Rules:
— Each area must be rectangular — L-shapes are not allowed
— Repeating the same name in adjacent cells makes the item span those cells
— Use . (dot) for an intentionally empty cell
— All rows must have the same number of columns
— Area names must be valid CSS identifiers (no spaces)

The biggest advantage: you can completely redesign the layout for mobile vs desktop by simply redefining the template area strings in a media query. The HTML order doesn't need to change.
Both auto-fill and auto-fit create as many column tracks as will fit in the container width, given a minimum track size. The difference only appears when there are fewer items than the maximum possible columns:

auto-fill — creates tracks for all possible columns whether or not items fill them. Empty tracks remain at their minimum width. Items don't stretch into empty tracks.

auto-fit — collapses any empty tracks to zero width. The remaining items expand to fill the full container width. Result: items always fill the row.

Example: container is 600px, minmax(200px, 1fr) → 3 columns possible. With only 2 items:
— auto-fill: 3 columns, 2 filled, 1 empty (items don't fill the third column)
— auto-fit: 2 columns, both filled (empty column collapses, items split the space)

Use auto-fit when you want items to always fill the full width. Use auto-fill when you want a consistent column count visible.
minmax(200px, 1fr) creates a track that:

— Is never smaller than 200px — even if the container is very narrow
— Can grow up to 1fr — takes a proportional share of available space

This is the cornerstone of the responsive card grid pattern:
repeat(auto-fill, minmax(200px, 1fr))

How it works: The browser calculates how many 200px columns fit in the container. If the container is 650px: ⌊650 ÷ 200⌋ = 3 columns. Each column then grows proportionally to fill the remaining space (the 1fr part). If the container shrinks below 600px, items drop to 2 columns, then 1 column — all automatically without media queries.

Important: in minmax(), the min cannot be fr, but the max can be.
grid-auto-rows sets the size of implicitly-created row tracks — rows that the browser generates automatically when items overflow the explicitly defined rows.

When you need it: When you define grid-template-columns but not rows. Items auto-flow into the grid creating new rows as needed. Without grid-auto-rows, these rows are auto height — sized to their content. With grid-auto-rows, you control that height.

Common patterns:
— grid-auto-rows: 200px — all cards same height
— grid-auto-rows: minmax(100px, auto) — at least 100px, grows with content
— grid-auto-rows: 1fr — equal-height rows

minmax(minHeight, auto) is generally the best practice because it prevents content from overflowing while still allowing rows to grow.
grid-auto-flow: dense modifies the auto-placement algorithm to backfill holes that would otherwise be left by items that span multiple columns or rows. Instead of placing items strictly in DOM order, the algorithm looks for earlier gaps and places later items there.

Use case: A photo gallery or card grid where some items span 2 columns. Without dense, large items leave gaps below them. With dense, smaller items fill those gaps creating a tight, masonry-like layout.

When to be careful:
— dense places items out of DOM order. An item that appears third in the HTML might be rendered first visually.
— This breaks keyboard tab order and screen reader reading order — which follows the DOM, not the visual order.
— Only use dense for purely visual content (images, decorative cards) — never for interactive elements like buttons, links or form fields where reading/tab order matters.
Multiple Choice Questions
Question 1 of 5
Which CSS Grid pattern creates a responsive card grid without media queries?
Question 2 of 5
In grid-template-areas, what does a . (dot) represent?
Question 3 of 5
What does grid-auto-rows: minmax(100px, auto) do?
Question 4 of 5
What is the key difference between auto-fill and auto-fit when fewer items are present than possible columns?
Question 5 of 5
What is the main accessibility concern with grid-auto-flow: dense?
Chapter Summary
  • grid-template-columns/rows — define explicit track sizes using fr, px, %, auto, minmax(), repeat() and named lines.
  • grid-template-areas — visual ASCII layout strings. Each row is a quoted string of area names. Items use grid-area: name to self-place. Dot = empty cell. Each area must be rectangular.
  • minmax(min, max) — track is at least min, at most max. Min cannot be fr. Key for responsive grids. Best practice: minmax(200px, 1fr) for cards.
  • Responsive magic: repeat(auto-fill, minmax(200px, 1fr)) — no media queries needed. Columns auto-reflow as width changes.
  • auto-fill vs auto-fit: auto-fill keeps empty column tracks. auto-fit collapses them, making items fill the full row width.
  • grid-auto-rows — size of rows created implicitly. Best practice: minmax(minPx, auto). Without it: auto (content height).
  • grid-auto-flow — row (default), column, row dense. Dense fills gaps — use only for visual content (accessibility risk).
  • gap — same as Flexbox. Between tracks only, not at outer edges. Shorthand: gap: row col.
~10 min read
Beginner level
Chapter 30 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel