CSS Grid

Create two-dimensional layouts with responsive columns, rows, and alignment.

CSS Grid is a two-dimensional layout system that lets you arrange elements in rows and columns. Unlike Flexbox (which is mainly one-dimensional—either a row or a column), Grid is designed for creating complex page layouts.

Why use CSS Grid?

Grid makes it easy to:

  • Create responsive layouts
  • Align items precisely
  • Build dashboards, galleries, and web page layouts
  • Control both horizontal and vertical spacing

1. Making a Grid Container

A grid container with four grid items1234display: grid

To use Grid, set the parent element's `display` property to `grid`.

HTML
<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>
CSS
.container {
  display: grid;
}

Now the parent becomes a grid container, and its children become grid items.


2. Creating Columns

Three columns with widths in the ratio one to two to one1fr2fr1fr1fr · 2fr · 1fr

Use `grid-template-columns`.

CSS
.container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
}

This creates 3 columns, each 200px wide.

You can also use fractions (`fr`):

CSS
.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
}

This divides the available space into 4 equal parts:

  • First column = 1 part
  • Second column = 2 parts
  • Third column = 1 part

Example:

devbrainbox.local/output
|----|--------|----|
 1fr    2fr    1fr

3. Creating Rows

A 100 pixel row above a taller 150 pixel row100px150pxgrid-template-rows

Use `grid-template-rows`.

CSS
.container {
  display: grid;
  grid-template-rows: 100px 150px;
}

This creates:

  • First row = 100px
  • Second row = 150px

4. Gap Between Items

Six grid items separated by equal gaps123456gap: 20px

Use `gap`.

CSS
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

Example:

devbrainbox.local/output
+-----+  +-----+  +-----+
|  1  |  |  2  |  |  3  |
+-----+  +-----+  +-----+

5. The repeat() Function

Three equal columns repeated across two rows1fr1fr1fr1fr1fr1frrepeat(3, 1fr)

Instead of writing:

CSS
grid-template-columns: 1fr 1fr 1fr;

Write:

CSS
grid-template-columns: repeat(3, 1fr);

This means:

  • Create 3 columns
  • Each is 1 fraction wide

6. Placing Items

The first item stretches from column line one to column line threeItem 12456grid-column: 1 / 3

You can control where an item appears.

CSS
.item1 {
  grid-column: 1 / 3;
}

This means:

  • Start at column line 1
  • End at column line 3

Example:

devbrainbox.local/output
+-----------+-------+
|   Item1   |       |
|           |       |
+-----------+-------+

For rows:

CSS
.item1 {
  grid-row: 1 / 3;
}

The item spans two rows.


7. Spanning Multiple Columns

An item spans two of the three columnsspan 21grid-column: span 2
CSS
.item {
  grid-column: span 2;
}

This item takes up 2 columns.


8. Auto Rows and Columns

Extra items create a new row of the same height123newnewnewgrid-auto-rows: 100px

If more items are added than you've explicitly defined, Grid creates new rows automatically.

CSS
grid-auto-rows: 100px;

Every new row will be 100px tall.


9. Aligning Items

Items centered horizontally and vertically within their grid cells123456justify-items + align-items: center

Center items inside their grid cells:

CSS
.container {
  justify-items: center;
  align-items: center;
}
  • `justify-items` → horizontal alignment
  • `align-items` → vertical alignment

Center the entire grid within its container:

CSS
.container {
  justify-content: center;
  align-content: center;
}

10. Responsive Grid

A responsive grid changes from three columns to two on a narrower screen123456123456auto-fit + minmax()

A common responsive pattern:

CSS
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

How it works:

  • `auto-fit` automatically creates as many columns as will fit.
  • `minmax(200px, 1fr)` makes each column at least 200px wide and allows it to grow to fill available space.

Grid vs. Flexbox

CSS GridFlexbox
Two-dimensional (rows and columns)One-dimensional (row or column)
Best for page layoutsBest for arranging items in a single direction
Can place items in exact cellsItems flow naturally along one axis
More powerful for complex layoutsSimpler for navigation bars, buttons, menus, and small UI components