On this page11 sections
Lecture 052: Grid Items, Alignment & @supports
| Category | 8: Flexbox & Grid |
| Lecture | 052 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Lecture 051 drew the grid. Now you'll place items exactly where you want them using line numbers, align them inside their cells, change their order, build the classic 12-column layout, and check browser support with
@supports.
Learning Objectives
- Place items by line number with
grid-columnandgrid-row - Align the grid and its items:
justify-items,align-items,place-items,justify-self,align-self,justify-content,align-content - Reorder items with
order - Build a 12-column layout
- Test for features with
@supports
1. Grid Lines
A grid with 3 columns has 4 vertical lines, numbered 1 to 4 from the left (and −1 from the right).
1 2 3 4
| A | B | C |Example
<style>
.g { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 60px; gap: 6px; }
.g div { background: #d0ebff; border-radius: 6px; display: grid; place-items: center; font: 12px monospace; }
.a { grid-column: 1 / 3; background: #ffd43b !important; } /* from line 1 to line 3 */
.b { grid-column: 3 / -1; grid-row: 1 / 3; background: #b2f2bb !important; } /* to the last line, 2 rows */
.c { grid-column: span 2; background: #ffc9c9 !important; }
</style>
<div class="g">
<div class="a">1 / 3</div>
<div class="b">3 / -1, rows 1 / 3</div>
<div class="c">span 2</div>
<div>4</div><div>5</div><div>6</div>
</div>| You write | Means |
|---|---|
grid-column: 1 / 3 | From line 1 to line 3 (two columns) |
grid-column: 2 / -1 | From line 2 to the last line |
grid-column: span 3 | Three columns wide, wherever it lands |
grid-area: 1 / 1 / 3 / 3 | row-start / column-start / row-end / column-end |
2. Aligning Items Inside Their Cells
Example
<style>
.g { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 80px; gap: 6px; background: #f1f3f5; padding: 6px;
justify-items: center; align-items: center; } /* = place-items: center */
.g div { background: #4c6ef5; color: white; padding: 6px 10px; border-radius: 6px; font: 12px monospace; }
.s { justify-self: start; align-self: start; }
.e { justify-self: end; align-self: end; }
.st { justify-self: stretch; align-self: stretch; }
</style>
<div class="g">
<div>centre</div><div class="s">start</div><div class="e">end</div>
<div class="st">stretch</div><div>centre</div><div>centre</div>
</div>| Property | On | Aligns |
|---|---|---|
justify-items / align-items | Container | Every item, horizontally / vertically, inside its cell |
place-items | Container | Both at once |
justify-self / align-self | Item | Just that item |
3. Aligning the Whole Grid
When the grid is smaller than its container, justify-content and align-content move the
whole grid.
Example
<style>
.g { display: grid; grid-template-columns: repeat(3, 60px); grid-auto-rows: 40px; gap: 6px; height: 140px;
background: #fff9db; justify-content: space-between; align-content: center; }
.g div { background: #f08c00; border-radius: 6px; }
</style>
<div class="g"><div></div><div></div><div></div><div></div><div></div><div></div></div>Values: start, end, center, stretch, space-between, space-around, space-evenly.
4. Order
.featured { order: -1; } /* jumps to the first cell */As with Flexbox, it only changes the visual order, not the reading order.
5. The 12-Column Layout
Twelve divides evenly into halves, thirds, quarters and sixths, so many design systems use it.
Example: drag the preview corner:
<style>
.grid12 { display: grid; grid-template-columns: repeat(12, 1fr); gap: 10px; }
.grid12 > * { grid-column: span 12; padding: 12px; border-radius: 8px; color: white; font: 13px system-ui; }
@media (min-width: 600px) {
.half { grid-column: span 6; }
.third { grid-column: span 4; }
.quarter { grid-column: span 3; }
}
</style>
<div class="grid12">
<div class="half" style="background: #1971c2;">half (6)</div>
<div class="half" style="background: #1864ab;">half (6)</div>
<div class="third" style="background: #2f9e44;">third (4)</div>
<div class="third" style="background: #2b8a3e;">third (4)</div>
<div class="third" style="background: #237032;">third (4)</div>
<div class="quarter" style="background: #e8590c;">¼</div><div class="quarter" style="background: #d9480f;">¼</div>
<div class="quarter" style="background: #c2410c;">¼</div><div class="quarter" style="background: #a63b0b;">¼</div>
</div>On phones every block spans all 12 columns (stacked); from 600px they sit side by side.
6. @supports: Feature Detection
@supports applies CSS only if the browser understands a property and value.
Example
<style>
.box { padding: 12px; background: #ffe3e3; }
@supports (display: grid) {
.box { background: #d3f9d8; }
.box::after { content: " Your browser supports Grid."; }
}
@supports not (anchor-name: --a) {
.note::after { content: " (Anchor positioning isn't supported here.)"; color: #e8590c; }
}
</style>
<div class="box">Feature test:</div>
<p class="note">Second test:</p>Write a simple layout first, then add the fancy version inside @supports, so older browsers still get a working page.
Common Beginner Mistakes
1. Counting columns instead of lines. A 3-column grid has lines 1–4; 1 / 3 spans two columns.
2. Mixing up justify-* and align-*. justify = horizontal (the inline axis), align = vertical (the block axis).
3. justify-content with no spare space. It only does something when the grid is smaller than its container.
Practice Questions
- In a 4-column grid, make an item span the full width using line numbers.
- What does
grid-column: span 2do? - Centre every item in its cell with one property.
- Align only one item to the end of its cell.
- In a 12-column grid, how many columns should a "third" span?
- Apply a rule only if the browser supports
display: grid.
Practice Answers
grid-column: 1 / -1;(or1 / 5)- The item stretches across two columns from wherever it's placed.
place-items: center;justify-self: end; align-self: end;on that item.-
@supports (display: grid) { … }
Key Takeaways
- Place items by line numbers (
1 / 3,2 / -1) or withspan. *-itemsalign all items;*-selfaligns one;*-contentmoves the whole grid.- A 12-column grid makes halves, thirds and quarters easy.
@supportslets you add new features safely.