Skip to content
BENIBIX Learn

CSS Grid

Development🌐 Web DevelopmentBrowser Code

Also known as Grid Layout, CSS Grid Layout, display: grid

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

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.

Example

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

Why it matters

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

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 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.
Grid, on the right, places items in rows and columns at once.

Common mistake

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

Key takeaways

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

Compare

Learn it properly