# Grid: Columns, Rows & Placement

> Master the 2D layout engine. Define precise architectural grids and explicitly place items exactly where you want them using line numbers.

Subject: CSS | Lesson 23 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/css/grid-fundamentals
Previous: [Flex Items: Sizing & Order](https://codewithanimation.in/css/flex-item-sizing.md) | Next: [Grid Areas & Dynamic Functions](https://codewithanimation.in/css/grid-areas-and-functions.md)

## What it is

CSS Grid is a powerful **2-dimensional** layout system. While Flexbox is designed to align items along a single axis (either a row or a column), Grid is designed to handle both rows and columns simultaneously.

You define a grid on a parent container by specifying the size of its `grid-template-columns` and `grid-template-rows`.

## Why it matters

Before Grid, building complex webpage architectures (like a Header spanning the top, a Sidebar on the left, a Main Content area, and a Footer at the bottom) required nesting dozens of flex containers or relying on fragile floats.

With CSS Grid, you can define that entire architecture on a single parent element, and simply tell the child items which grid cells they should occupy.

## How it works

1. **The fr unit**: Grid introduces the `fr` (fractional) unit. `grid-template-columns: 1fr 2fr 1fr;` divides the space into 4 equal fractions, giving the middle column exactly twice the width of the outer columns.

2. **Grid Lines**: This is the most crucial concept. A grid is made of lines, not just tracks. A 3-column grid has **4 vertical lines** (start, between 1 and 2, between 2 and 3, and end).

3. **Item Placement**: You can explicitly place a child item using `grid-column` and `grid-row`. For example, `grid-column: 2 / 4;` tells the item to start at vertical line 2, and stretch until vertical line 4.

## Common mistake: The 'Number of Columns' Confusion

The biggest hurdle for beginners is confusing the number of columns with the number of grid lines. If you want an item to span across two columns starting from the first column, you don't write `grid-column: 1 / 2;` (that only spans one column).

You must write `grid-column: 1 / 3;` (start at line 1, end at line 3).

## Tip: Negative Line Numbers

If you want an item to stretch all the way to the right edge of the grid, but you do not know how many columns there are, you can use negative line numbers! `grid-column: 1 / -1;` means 'start at the first line, and end at the very last line'.

## Try it: Try it

Define the parent grid tracks, then select the Hero item and adjust its line placements to watch it span across multiple columns and rows.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/grid-fundamentals

## Check yourself

### Question 1

What is the primary difference between CSS Grid and Flexbox?

- A. Grid is built for 2-dimensional layouts, Flexbox is for 1-dimensional layouts
- B. Grid only works on text, Flexbox works on boxes
- C. Flexbox is for desktop layouts, Grid is for mobile

Correct answer: A

Flexbox aligns items in a single row or column. Grid lets you define a rigid structure of both rows and columns at the same time.

- Why not B: Both operate on block-level boxes.
- Why not C: Both are entirely responsive and used across all devices.

### Question 2

If a grid has exactly 4 columns, how many vertical grid lines does it have?

- A. 5
- B. 4
- C. 3

Correct answer: A

A grid always has one more line than the number of tracks. For 4 columns, you have lines 1 (start), 2, 3, 4, and 5 (end).

- Why not B: There is an extra line to close off the final column.
- Why not C: That would be the number of gaps, not lines.

### Question 3

What does `grid-column: 2 / 4;` mean?

- A. The item starts at grid line 2 and ends at grid line 4
- B. The item starts at column 2 and spans 4 columns wide
- C. The item takes up 2/4 (half) of the container width

Correct answer: A

The syntax `[start] / [end]` refers to the numbered grid lines. Starting at line 2 and ending at line 4 means it will span across exactly two columns.

- Why not B: The second number is the ending line, not the span amount (unless you use the 'span' keyword).
- Why not C: CSS Grid uses line numbers, not mathematical fractions, for placement.

## Remember this

- Grid handles 2D layouts (rows and columns) simultaneously.
- The `fr` unit distributes leftover space proportionally.
- Grid placement is based on numbered lines, not just columns.
- `grid-column: 1 / -1` spans the entire width of the grid.
