CSS: CSS Grid Items
Grid item properties let you precisely control how individual children are placed within a grid container.
1. Grid Item Properties
(1) Quick Reference
| Property | Values | Purpose |
|---|---|---|
grid-column |
1 / 3, span 2 |
Column placement |
grid-row |
1 / 3, span 2 |
Row placement |
grid-area |
header, 1 / 2 / 3 / 4 |
Named area or line placement |
justify-self |
start, end, center, stretch |
Horizontal alignment within cell |
align-self |
start, end, center, stretch |
Vertical alignment within cell |
place-self |
center, start end |
Shorthand for justify + align |
2. grid-column and grid-row
Control where items start and end on the grid:
▶ Example
▶ Try it Yourself(1) Full-Width Item
Use grid-column: 1 / -1 to span all columns:
▶ Example
▶ Try it Yourself3. justify-self / align-self
Control alignment within individual cells:
▶ Example
▶ Try it Yourself4. Grid Overlap
Grid items can overlap — later items appear on top:
▶ Example
▶ Try it Yourself❓ FAQ
Q How do grid-column and grid-row work? How are grid lines numbered?
A Grid lines start at 1 — a 3-column grid has 4 column lines.
grid-column: 1 / 3 places an item from line 1 to line 3 (spanning 2 columns), grid-row: 1 / 2 takes one row. The value before the slash is the start line, after it the end line; you're counting lines, not cells — the classic beginner slip.Q How do I make a grid item span multiple rows or columns?
A span is the intuitive way:
grid-column: span 2; grid-row: span 3; covers 2 columns and 3 rows without naming a start point — the browser finds room. For precise control, grid-column: 1 / 3; grid-row: 1 / 3; gives you a 2×2 block from line 1.Q What does grid-column: 1 / -1 mean?
A From grid line 1 to the last grid line (-1 is the last line's number) — the full row. Great for full-width headers or divider rows, and it keeps working when you add columns later.
Q How do I use grid-template-areas?
A Draw a map on the container, then let items claim land:
grid-template-areas: "header header" "sidebar main" "footer footer"; — each string is one row, repeated names merge into one area. An item just declares header { grid-area: header; }. Far more readable than line numbers, the first choice for page layouts; each row string must list as many areas as there are columns.Q Can Grid items overlap?
A Yes. Two items assigned the same grid area stack up, the later one on top, with z-index controlling the order. Text over an image, badges over a card — same trick.
Q What's the order of the 4 values in the grid-area shorthand?
A row-start / column-start / row-end / column-end:
grid-area: 1 / 1 / 3 / 3 runs from row 1, column 1 to row 3, column 3 (a 2×2 block). Row then column, start then end — chant "row column row column" until it sticks.Q How do I align a grid item inside its cell? justify-self vs align-self?
A justify-self is horizontal, align-self vertical; values are stretch (default, fills the cell) / start / end / center. To center an image horizontally only:
justify-self: center;. The -self versions affect one item without touching siblings.Q What's the difference between justify-items and justify-content?
A -items sets how each item sits inside its own cell; -content moves the whole grid inside the container — the difference only shows when the grid is smaller than the container. Item looks misaligned? First decide the layer: inside-cell alignment uses -items/-self, whole-grid offsets use -content.
Q Can I change the order of grid items? How does the order property work?
A Yes. Items follow DOM order by default;
order: -1 promotes one to the front, lower numbers come first. Handy for promoting a featured card — but don't overuse it: screen readers and Tab focus still follow the DOM, so a big visual/DOM mismatch hurts accessibility.📖 Summary
- Grid item placement is very flexible — precisely specify start/end grid lines, or use
spankeyword for multi-row/column spanning - Within cells,
justify-items/align-itemscontrol overall alignment,justify-self/align-selfcontrol individual elements - If line numbers are hard to remember, try
grid-template-areasfor named regions — more intuitive
📝 Exercises
- Create a 4×4 grid, place an item spanning 2 columns and 2 rows starting from column 2, row 2.
- Use
grid-template-areasfor a complete page layout: header (spanning 2 columns), sidebar, content area, footer (spanning 2 columns). - Center all items horizontally and vertically in a grid container (using
place-items: center). - Make the third grid item align to the bottom-right corner of its cell (using
justify-selfandalign-self).