Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › grid

CSS Reference

grid

The two-dimensional layout system. Arrange items in rows and columns simultaneously.

Last updated: October 2026

Definition: CSS Grid is a two-dimensional layout system. Set display: grid on a parent, define columns and rows, and place items anywhere in the grid. It is the most powerful layout system in CSS and the standard for page-level layouts and card grids.

🎬 Live Examples

Four Grid patterns that cover most real-world use cases.

3-column grid
1
2
3
4
5
6
Responsive auto-fit grid
A
B
C
D
E
Item spanning 2 columns
1
Spans 2 columns
3
4
Full page layout
Header
Sidebar
Main Content

💡 Every layout above is built with the same core Grid properties.

📝

Basic Syntax

.grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;  /* 3 equal columns */
    gap: 1rem;
}

The parent becomes a grid container. Children become grid items placed automatically in the defined columns.

🎯 Container Properties (Parent)

Property Common Values Purpose
displaygrid / inline-gridEnable Grid.
grid-template-columnsrepeat(3, 1fr), 200px 1fr, etc.Define column tracks.
grid-template-rowsauto 1fr auto, etc.Define row tracks.
gapany lengthSpace between tracks.
grid-auto-flowrow / column / denseAuto-placement direction.
justify-itemsstretch / start / center / endAlign items within their cells (horizontal).
align-itemsstretch / start / center / endAlign items within their cells (vertical).

🎯 Item Properties (Children)

Property Purpose
grid-columnspan 2, 1 / 3. Position/span across columns.
grid-rowspan 2, 1 / 3. Position/span across rows.
grid-areaShorthand for row-start / column-start / row-end / column-end.
justify-selfOverride justify-items for this item.
align-selfOverride align-items for this item.
1

Equal Columns with repeat()

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

repeat(3, 1fr) is shorthand for 1fr 1fr 1fr. Three equal columns.

Change the number for different counts:

grid-template-columns: repeat(2, 1fr);  /* 2 columns */
grid-template-columns: repeat(4, 1fr);  /* 4 columns */
grid-template-columns: repeat(6, 1fr);  /* 6 columns */
2

The Responsive Auto-fit Grid

One line of CSS that replaces dozens of media queries.

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1rem;
}

How it works:

  • auto-fit — create as many columns as will fit.
  • minmax(250px, 1fr) — each column is at least 250px, growing to fill space.
  • No media queries required. The grid responds to the container width automatically.
3

Spanning Items Across Tracks

/* Span 2 columns */
.featured {
    grid-column: span 2;
}

/* Span 2 rows */
.tall {
    grid-row: span 2;
}

/* Explicit positioning: from column line 1 to column line 3 */
.hero {
    grid-column: 1 / 3;
}

/* Full-width: from the first to the last column line */
.full-width {
    grid-column: 1 / -1;
}

grid-column: 1 / -1 is the "full-width" pattern — the item stretches across every column.

4

Full Page Layout with Grid

The classic header / sidebar / main / footer layout.

.layout {
    display: grid;
    grid-template-columns: 250px 1fr;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
        "header  header"
        "sidebar main"
        "footer  footer";
    min-height: 100vh;
    gap: 1rem;
}

.header  { grid-area: header;  }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main;    }
.footer  { grid-area: footer;  }

Named grid areas make complex layouts readable. Change the ASCII diagram to change the layout.

5

Responsive Stack → Grid

.layout {
    display: grid;
    grid-template-columns: 1fr;  /* Mobile: stack */
    gap: 1rem;
}

@media (min-width: 1024px) {
    .layout {
        grid-template-columns: 250px 1fr;  /* Desktop: sidebar + main */
    }
}

🎯 Grid vs Flex — Decision Guide

Scenario Use
Full page layout (header, sidebar, main)Grid
Card grid with equal rows and columnsGrid
Photo gallery with alignmentGrid
Overlapping contentGrid
Navbar layoutFlex
Button with icon + textFlex
Centering contentFlex

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is CSS Grid?

CSS Grid is a two-dimensional layout system for arranging items in rows and columns simultaneously. Set display: grid on a parent, then define columns and rows with grid-template-columns and grid-template-rows.

What is the difference between grid and flex?

Grid is two-dimensional (rows AND columns) and lets you place items anywhere in the grid. Flex is one-dimensional (row OR column) and arranges items in sequence. Use Grid for page layouts and card grids, Flex for components like navbars and buttons.

What does grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) do?

It creates a responsive grid that automatically fits as many 250px-minimum columns as possible into the available width. No media queries required. Each column stretches evenly with 1fr.

What is the difference between auto-fit and auto-fill?

auto-fit collapses empty columns and stretches items to fill the row. auto-fill keeps empty columns, leaving whitespace. Use auto-fit for card grids where you want items to fill the space.

How do I span an item across multiple columns?

Use grid-column: span 2 (or the number of columns you want to span). In Tailwind, use col-span-2. The item will extend across that many grid tracks.

🎯 What's Next?

← All CSS Reference