CSS: CSS Grid Container
CSS Grid is a powerful two-dimensional layout system that lets you control rows and columns simultaneously.
1. Grid Container Properties
(1) Quick Reference
| Property | Values | Purpose |
|---|---|---|
display: grid |
— | Creates grid container |
grid-template-columns |
100px 1fr 2fr, repeat(3, 1fr) |
Column definitions |
grid-template-rows |
100px auto, repeat(3, 100px) |
Row definitions |
gap |
10px, 10px 20px |
Gap between items |
grid-template-areas |
"header header" "main aside" |
Named grid areas |
2. fr Unit
The fr unit distributes remaining space proportionally:
▶ Example
▶ Try it Yourself3. repeat()
The repeat() function reduces repetition:
▶ Example
▶ Try it Yourself4. minmax() + auto-fill/auto-fit
Creates responsive grids that automatically adjust column count:
▶ Example
▶ Try it Yourselfauto-fill vs auto-fit:
auto-fill: Creates as many columns as fit, even if emptyauto-fit: Same as auto-fill, but collapses empty columns
5. grid-template-areas
Define layout with named areas:
▶ Example
▶ Try it Yourself❓ FAQ
Q Grid or Flexbox — which should I use, and when?
A One-dimensional layouts (a single row or column) are simpler with Flexbox; two-dimensional layouts that must align rows and columns together are Grid's job. Rule of thumb: nav bars, button groups and toolbars use flex; page layouts, card walls and form alignment use grid. They nest well — a grid cell that becomes a flex container is a common pattern.
Q How do I write grid-template-columns, and what does 1fr mean?
A
grid-template-columns: 200px 1fr 2fr creates three columns: the first is a fixed 200px, and the remaining space is split 1:2 between the other two. 1fr is one share of the space left after gaps are subtracted — not a percentage. Common forms: repeat(3, 1fr) for thirds, repeat(auto-fill, minmax(200px, 1fr)) for a responsive column count, and minmax(0, 1fr) to stop content from blowing the track open.Q auto-fit vs auto-fill — what's the difference, and which one for cards?
A Both compute the column count from minmax; the difference is what happens to leftover space.
auto-fill keeps empty tracks, so you get the maximum number of columns that fit and empty space on the right when there are few cards. auto-fit collapses empty tracks so existing cards stretch to fill the row. Use auto-fill for fixed card widths aligned left, auto-fit to make cards fill the row.Q How do I center items in a grid, including a single cell?
A To center everything:
display: grid; place-items: center; on the container (shorthand for align-items plus justify-items). To center inside one cell: place-self: center on that item, or align-self: center; justify-self: center. Remember place-items affects every child and place-self only the one item.Q Content overflows and breaks my grid — how do I stop it?
A Usually because children default to
min-width: auto, so long words, URLs or images widen the track. Three fixes: write the track as minmax(0, 1fr); add min-width: 0 (horizontal) or min-height: 0 (vertical) to the child; and as a last resort use overflow: hidden or overflow-wrap: anywhere on the content. Then re-check that fr distributes as intended.Q How do I get equal-height rows and equal-width columns?
A Equal height is the default: cells in a row stretch to the tallest item; set
align-items: start if you don't want that. For equal widths use grid-auto-columns: 1fr with grid-auto-flow: column, or simply repeat(3, minmax(0, 1fr)). Plain 1fr can end up unequal when content has a minimum width — minmax(0, 1fr) is what makes them truly equal.Q Can I build a grid with a dynamic number of rows or columns?
A Variable column count:
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) lets the browser derive the count from the container width. Variable rows: define only columns and let grid-auto-rows: 200px generate rows. A true masonry layout (different heights per column, staggered) is beyond Grid — it needs JavaScript or a column-count multi-column layout.Q How do I make a grid fill the full height of the screen?
A Set
height: 100vh on the container (100dvh on mobile avoids the browser bar), then grid-template-rows: auto 1fr auto so the middle row absorbs the remaining height. If only one cell should fill, use grid-row: 1 / -1 or align-self: stretch. Percentage heights do nothing unless an ancestor has a definite height.Q justify-self vs justify-items vs justify-content — what's the difference?
A They act on different things:
justify-items aligns all children horizontally and is set on the container; justify-self aligns a single child and overrides items; justify-content distributes the children as a group inside the container (space-between, center, and so on) and only shows an effect when the total track width is smaller than the container. The vertical equivalents are align-items, align-self and align-content.📖 Summary
- Grid is a powerful two-dimensional layout system
- Define grid structure with
grid-template-columnsandgrid-template-rows - Use
frunit for proportional space distribution - Create flexible grids with
repeat(),minmax() gapsets spacing;auto-fill/auto-fitwithminmaxcreates truly responsive grids
📝 Exercises
Basic
- Create a 3×3 grid container, each cell 100px×100px, 10px gap.
- Use
1frunit for a "two-column" layout (left 1fr, right 3fr).
Intermediate
- Create an auto-adapting card grid with
repeat(auto-fill, minmax(150px, 1fr))— resize browser to see columns change. - Create a "classic three-row two-column" layout: header (spanning two columns), left menu, right content, footer (spanning two columns).
Challenge
- Create a responsive image gallery: desktop 4 columns, tablet 2 columns, mobile 1 column, using
auto-fill+minmax. - Use
grid-template-areasfor a complete blog layout (header, navigation, main content, sidebar, footer).
🧩 Related Examples
auto-fit vs auto-fillSide-by-side comparison: with an unfilled last row, auto-fill keeps empty tracks (cards stay left-aligned) w…
Fix Grid Overflow with minmax(0, 1fr)Reproduces the classic broken-grid bug: a long URL blows its 1fr track open and crushes the neighbor column.…
Team Member GridFour member cards in a two-column grid, each revealing a social row on hover — grid layout plus independent…