CSS: CSS Grid Container

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Gray🏷 Category: CSS Tutorial

CSS Grid is a powerful two-dimensional layout system that lets you control rows and columns simultaneously.

Properties in this section: grid-template-columns grid-template-rows gap grid-template-areas place-items place-self column-count justify-items justify-self justify-content

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 Yourself

3. repeat()

The repeat() function reduces repetition:

▶ Example

▶ Try it Yourself

4. minmax() + auto-fill/auto-fit

Creates responsive grids that automatically adjust column count:

▶ Example

▶ Try it Yourself

auto-fill vs auto-fit:


5. grid-template-areas

Define layout with named areas:

▶ Example

▶ Try it Yourself

Properties in this section: grid-template-columns grid-template-rows gap grid-template-areas place-items place-self column-count justify-items justify-self justify-content

❓ 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

📝 Exercises

Basic

  1. Create a 3×3 grid container, each cell 100px×100px, 10px gap.
  2. Use 1fr unit for a "two-column" layout (left 1fr, right 3fr).

Intermediate

  1. Create an auto-adapting card grid with repeat(auto-fill, minmax(150px, 1fr)) — resize browser to see columns change.
  2. Create a "classic three-row two-column" layout: header (spanning two columns), left menu, right content, footer (spanning two columns).

Challenge

  1. Create a responsive image gallery: desktop 4 columns, tablet 2 columns, mobile 1 column, using auto-fill + minmax.
  2. Use grid-template-areas for a complete blog layout (header, navigation, main content, sidebar, footer).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏