Web Analytics

CSS Grid Basics

Beginner ~11 min read

What is CSS Grid?

CSS Grid is a powerful 2D layout system for creating complex, responsive layouts. Unlike Flexbox (1D), Grid works with both rows and columns simultaneously.

Creating a Grid Container

Apply display: grid to create a grid container. Direct children become grid items.

HTML
CSS
JS

Grid Template Columns

grid-template-columns defines column widths. Use px, %, fr (fraction), auto, or mix them!

HTML
CSS
JS

Grid Template Rows

grid-template-rows defines row heights. Works the same as columns.

HTML
CSS
JS

Gap (Grid Spacing)

The gap property creates spacing between grid items. Use row-gap and column-gap for individual control.

HTML
CSS
JS

Repeat Function

Use repeat() to avoid repetition: repeat(3, 1fr) = 1fr 1fr 1fr.

HTML
CSS
JS

Auto-Fit & Minmax

Combine repeat(auto-fit, minmax(min, max)) for responsive grids that automatically adjust columns!

HTML
CSS
JS
Pro Tip: The fr unit (fraction) divides available space proportionally. 1fr 2fr means the second column gets twice the space of the first!

Summary

  • display: grid - Creates grid container
  • grid-template-columns - Define column widths
  • grid-template-rows - Define row heights
  • gap - Spacing between grid items
  • repeat(n, size) - Repeat pattern n times
  • fr unit - Fraction of available space
  • minmax(min, max) - Flexible sizing with limits

Quick Quiz

What does the 'fr' unit stand for in CSS Grid?

A
Frame
B
Fraction
C
Free
D
Fixed ratio