CSS Grid: Complete Guide & Interactive Examples

Master two-dimensional layouts with CSS Grid

What is CSS Grid?

CSS Grid Layout is a modern, two-dimensional layout system for the web. It enables you to create complex, responsive layouts by dividing a container into rows and columns, and placing child elements (grid items) into these defined areas. Unlike Flexbox (which is one-dimensional), Grid lets you control both rows and columns simultaneously, making it ideal for web page layouts, dashboards, galleries, and more.

CSS Grid Template Visual
Explicit Grid: Defined by grid-template-rows and grid-template-columns.
Implicit Grid: Created automatically when items are placed outside the defined grid, using grid-auto-rows and grid-auto-columns.
Example:
.container {
  grid-template-columns: 100px 100px;
  grid-auto-rows: 40px;
}

Key CSS Grid Properties

Basic Grid Container & Items

Set display: grid on a container to enable grid layout. Direct children become grid items.

1
2
3
4
.basic-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

Defining Columns & Rows

Use grid-template-columns and grid-template-rows to define the structure.

A
B
C
D
.columns-rows-grid {
  display: grid;
  grid-template-columns: 120px 1fr 2fr;
  grid-template-rows: 60px 60px;
  gap: 10px;
}

Grid Gap (Spacing)

gap (or grid-gap) sets spacing between rows and columns.

One
Two
Three
Four
.gap-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px 8px;
}

Grid Lines, Start/End, and Spanning

Use grid-column and grid-row to control where items start/end and how many tracks they span.

Span 2 cols
2
3
Row Span
.lines-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 50px 50px;
  gap: 8px;
}
.lines-grid .span2 {
  grid-column: 1 / span 2;
}
.lines-grid .row-span {
  grid-row: 1 / span 2;
}

Named Grid Areas

Use grid-template-areas and grid-area for semantic layouts.

CSS Grid Template Visual
Header
Main
.area-grid {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 120px 1fr;
  grid-template-rows: 50px 120px 40px;
  gap: 8px;
}
.area-grid .header { grid-area: header; }
.area-grid .sidebar { grid-area: sidebar; }
.area-grid .main { grid-area: main; }
.area-grid .footer { grid-area: footer; }

Flexible Sizing: fr, minmax(), auto-fit, auto-fill

Use fr for flexible tracks, minmax() for min/max sizes, and auto-fit/auto-fill for responsive grids.

1
2
3
4
5
6
.flexible-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 12px;
}

Alignment: justify-items, align-items, justify-content, align-content

Control alignment of items and tracks within the grid container.

Item 1
Item 2
Item 3
Item 4
.align-grid {
  display: grid;
  grid-template-columns: repeat(2, 120px);
  grid-template-rows: repeat(2, 60px);
  justify-items: end;
  align-items: center;
  justify-content: space-between;
  align-content: space-around;
  gap: 10px;
}

Intrinsic Sizing: min-content & max-content

Use min-content and max-content to size tracks based on their content.

Short
Some much longer content
.intrinsic-grid {
  display: grid;
  grid-template-columns: min-content max-content;
  gap: 10px;
}

Explicit vs. Implicit Grids

Explicit grid is defined by grid-template-rows and grid-template-columns.
Implicit grid is created when you place items outside the defined grid, and tracks are auto-generated using grid-auto-rows and grid-auto-columns.

Explicit Grid

1
2
3
4
.explicit-grid {
  display: grid;
  grid-template-columns: 100px 100px;
  gap: 8px;
}

Implicit Grid

1
2
3
4
5
.implicit-grid {
  display: grid;
  grid-template-columns: 100px 100px;
  grid-auto-rows: 40px;
  gap: 8px;
}

Notice: The explicit grid only defines two columns and two rows, so items 5+ are placed in new, auto-generated (implicit) rows.

Responsive Grid Example

Here's an example of a responsive grid using minmax() and repeat().

1
2
3
4
5
6
7
8
.responsive-demo-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(60px, 1fr));
  grid-template-rows: 40px 40px;
}

Best Practices & Resources