Positioning grid items

By default, grid items fill cells in row order (left to right, top to bottom). But you can tell any item exactly where to go using grid-column and grid-row.

Grid lines are numbered starting at 1. A 3-column grid has lines 1, 2, 3, and 4 (the fourth line is the right edge).

grid-positioning.css Positioning items on the grid

The syntax is grid-column: start / end. For example, grid-column: 1 / 4 means "start at line 1, end at line 4" — spanning 3 columns.

Key point:
The end line is exclusive. grid-column: 1 / 3 covers columns 1 and 2, not column 3.

Spanning with the span keyword

Instead of specifying both lines, use span to say "cover N columns/rows from where I am."

spanning.css Using the span keyword

span 2 means "occupy 2 columns/rows." It's shorter and often clearer than counting lines.

The grid-area shorthand

You can combine row and column positioning into one property: grid-area. The order is: row-start / column-start / row-end / column-end.

grid-area.css Using the grid-area shorthand

grid-area is concise but less readable than separate grid-column and grid-row properties.

Try it yourself: Dashboard layout

This is where CSS Grid shines! grid-template-areas lets you visualize your layout as ASCII art. Define named regions, then place items by name.

✏️ Try It Yourself — Dashboard with Named Areas
Preview

grid-template-areas is visual and intuitive. Each row in the areas string represents a row of the grid. Items with the same name span multiple cells. A dot . represents an empty cell.

Pro tip:
This layout approach makes responsive design easy — just redefine grid-template-areas for mobile without changing any HTML.

Try it yourself: Magazine layout

Here's a magazine-style layout with a large featured article and smaller items using grid-column and grid-row.

✏️ Try It Yourself — Magazine Layout
Preview

Named grid lines

You can name grid lines instead of (or in addition to) numbered lines. This makes code more readable for complex layouts.

named-lines.css Naming grid lines

Implicit vs explicit grids

An explicit grid is what you define with grid-template-columns and grid-template-rows. An implicit grid is created automatically when items don't fit or are placed outside the explicit grid.

implicit-grid.css Controlling implicit grid sizing

Use grid-auto-rows and grid-auto-columns to control the size of automatically created rows/columns.

Auto-fitting with implicit grids

Implicit grids are powerful for dynamic layouts where you don't know how many items you'll have.

auto-grid.css Let the grid create rows automatically
Challenge: Build a Dashboard

Task: Create a dashboard layout using grid-template-areas with:

  • A full-width header area at the top
  • A left sidebar (1 column wide, 2+ rows tall)
  • Two stat cards (stat1 and stat2) in the top right
  • A large chart area below the stat cards (spans 2 columns)
  • A full-width footer at the bottom

Use descriptive colors and name all areas. Use grid-auto-rows to set a minimum row height of 100px.

Hint: Your grid might look like:

grid-template-areas:
"header header header"
"sidebar stat1 stat2"
"sidebar chart chart"
"footer footer footer";

Adjust grid-template-columns and grid-template-rows to control sizing.

Key takeaways

  • grid-column: start / end and grid-row: start / end position items on the grid.
  • Use span N to occupy N columns or rows without counting lines.
  • grid-area combines row and column positioning into one shorthand.
  • grid-template-areas is visual and intuitive — define named regions as ASCII art.
  • Implicit grids are created automatically when items exceed the explicit grid. Control their size with grid-auto-rows and grid-auto-columns.
  • Named grid lines improve readability for complex layouts.

Test your knowledge

Question 1 of 5
How do you make an item span 2 columns in a grid?
Question 2 of 5
What does grid-template-areas do?
Question 3 of 5
In grid-template-areas, what character represents an empty cell?
Question 4 of 5
What is an implicit grid?
Question 5 of 5
What does grid-area: header mean?
0
/5
Excellent! You understand CSS Grid positioning.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered grid item positioning.
You're building layouts like a pro.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →