Web Analytics

Grid Advanced

Intermediate ~10 min read

Grid Column & Row

Use grid-column and grid-row to position items on specific grid lines. Lines are numbered starting from 1.

HTML
CSS
JS

Spanning Cells

Use span keyword to make items span multiple columns or rows: grid-column: span 2.

HTML
CSS
JS

Justify Items (Horizontal)

justify-items aligns grid items horizontally within their cells: start, center, end, stretch (default).

HTML
CSS
JS

Align Items (Vertical)

align-items aligns grid items vertically within their cells.

HTML
CSS
JS

Justify Self & Align Self

Override container alignment for individual items with justify-self and align-self.

HTML
CSS
JS

Complex Layout Example

Combining all techniques to create a magazine-style layout with different sized items.

HTML
CSS
JS
Pro Tip: Grid lines can be negative! grid-column: 1 / -1 spans from the first to the last column.

Summary

  • grid-column / grid-row - Position items on grid lines
  • span - Make items span multiple cells
  • justify-items - Horizontal alignment of all items
  • align-items - Vertical alignment of all items
  • justify-self / align-self - Individual item alignment
  • Negative line numbers count from the end

Quick Quiz

Which property makes a grid item span 3 columns?

A
grid-span: 3
B
grid-column: span 3
C
column-span: 3
D
grid-width: 3