# CSS Grid

> A CSS layout that puts things in rows and columns at the same time.

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** Grid Layout, CSS Grid Layout, display: grid

## Think of it like

It is like a muffin tray. Each cup has a row and a column. A big muffin can fill two cups.

## Definition

CSS Grid is a layout that places items in rows and columns at once. You turn it on with display: grid and say how many columns you want. Each item then drops into the next cell. One item can span more than one cell, like a wide header. It works in two directions, which suits whole page layouts and galleries.

## Example

Jack shows twelve photos with grid-template-columns: repeat(3, 1fr). They fall into four neat rows of three.

## Example code

```css
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
```

Three equal columns with a gap. The photos fill them row by row.

## Why it matters

Whole page layouts used to need tricks. Grid does them in a few lines.

## Common mistake

Thinking grid replaces flexbox. Grid suits two directions, flexbox suits one, and pages use both.

## Three principles

- **Two directions.** Rows and columns together.
- **Cells to fill.** Items drop into the next free cell.
- **Span cells.** One item can cover several cells.

## Related terms

- [Flexbox](https://learn.benibix.com/term/development/flexbox)
- [Cascading Style Sheets (CSS)](https://learn.benibix.com/term/development/css)
- [Media Query](https://learn.benibix.com/term/development/media-query)
- [CSS Box Model](https://learn.benibix.com/term/development/css-box-model)
- [Grid System](https://learn.benibix.com/term/design/grid-system)

## Told apart from

- [Flexbox vs CSS Grid](https://learn.benibix.com/compare/flexbox-vs-css-grid): People think flexbox and grid do the same job, and they do not. Flexbox lines things up in one row or one column. Grid puts things in rows and columns at once. Most pages use both: grid for the page, flexbox for the menu inside it.

## Taught in

- [Flexbox, Grid and Position](https://learn.benibix.com/learn/development-web/flexbox-grid-and-position): HTML, CSS and JS, 9/14

![Two halves. On the left, flexbox: three boxes of different widths sit side by side in one row, with an arrow along the row, and the line display: flex. On the right, grid: five boxes sit in two rows and three columns, the first box spanning two columns, with the line display: grid.](https://learn.benibix.com/diagrams/dev-css-layout.svg)

---

Source: https://learn.benibix.com/term/development/css-grid
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
