On this page11 sections
  1. Learning Objectives
  2. 1. Grid Lines
  3. 2. Aligning Items Inside Their Cells
  4. 3. Aligning the Whole Grid
  5. 4. Order
  6. 5. The 12-Column Layout
  7. 6. @supports: Feature Detection
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 052: Grid Items, Alignment & @supports

Category8: Flexbox & Grid
Lecture052 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

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-column and grid-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).

output
 1      2      3      4
 |  A   |  B   |  C   |

Example

html
<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 writeMeans
grid-column: 1 / 3From line 1 to line 3 (two columns)
grid-column: 2 / -1From line 2 to the last line
grid-column: span 3Three columns wide, wherever it lands
grid-area: 1 / 1 / 3 / 3row-start / column-start / row-end / column-end

2. Aligning Items Inside Their Cells

Example

html
<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>
PropertyOnAligns
justify-items / align-itemsContainerEvery item, horizontally / vertically, inside its cell
place-itemsContainerBoth at once
justify-self / align-selfItemJust 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

html
<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

css
.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:

html
<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

html
<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

  1. In a 4-column grid, make an item span the full width using line numbers.
  2. What does grid-column: span 2 do?
  3. Centre every item in its cell with one property.
  4. Align only one item to the end of its cell.
  5. In a 12-column grid, how many columns should a "third" span?
  6. Apply a rule only if the browser supports display: grid.

Practice Answers

  1. grid-column: 1 / -1; (or 1 / 5)
  2. The item stretches across two columns from wherever it's placed.
  3. place-items: center;
  4. justify-self: end; align-self: end; on that item.
  5. @supports (display: grid) { … }

Key Takeaways

  • Place items by line numbers (1 / 3, 2 / -1) or with span.
  • *-items align all items; *-self aligns one; *-content moves the whole grid.
  • A 12-column grid makes halves, thirds and quarters easy.
  • @supports lets you add new features safely.