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).
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.
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."
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 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.
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.
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.
Named grid lines
You can name grid lines instead of (or in addition to) numbered lines. This makes code more readable for complex layouts.
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.
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.
Task: Create a dashboard layout using grid-template-areas with:
- A full-width
headerarea at the top - A left
sidebar(1 column wide, 2+ rows tall) - Two stat cards (
stat1andstat2) in the top right - A large
chartarea below the stat cards (spans 2 columns) - A full-width
footerat 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 / endandgrid-row: start / endposition items on the grid.- Use
span Nto occupy N columns or rows without counting lines. grid-areacombines row and column positioning into one shorthand.grid-template-areasis 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-rowsandgrid-auto-columns. - Named grid lines improve readability for complex layouts.
Test your knowledge
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 →