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.
grid-template-columns &
grid-template-rows — full syntax
grid-template-areas — visual
ASCII layoutgrid-auto-rows & grid-auto-columnsminmax() — minimum and maximum
track sizesauto-fill vs auto-fit — responsive no-media-query gridsgap, grid-auto-flow, grid shorthandAll Grid Container Properties — Quick Reference
Every property that belongs to the grid container (the parent with
display: grid):
grid-area to place themselves.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:
Lines named in brackets — reference by name
.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>:
.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";
}
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.
max:1fr
max:1fr
max:1fr
/* 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:
Empty column tracks remain — items DON'T stretch to fill
Empty tracks collapse to 0 — items fill the full width ✓
/* ── 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:
(content height)
(content height)
.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:
.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.
Fills rows left to right
Fills columns top to bottom
dense: fills any gaps — items may appear out of DOM order
.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 |
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:
Quick Check — Chapter 30
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.
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 rowsminmax(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.
grid-template-areas, what does a . (dot) represent?grid-auto-rows: minmax(100px, auto)
do?auto-fill
and auto-fit when fewer items are present than possible columns?
grid-auto-flow: dense?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 usegrid-area: nameto 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.