CSS logoCSS INTERMEDIATE

CSS Grid

Master CSS Grid with visual examples, from basic layouts to advanced responsive designs

5 min read
cssgridlayoutresponsive

Grid Container

Create Grid Container

Transform any element into a grid layout container that creates a two-dimensional layout system

css
📄 HTMLhtml
🎨 Display Grid
1
2
3
4

Define Columns

Set the number and size of columns in your grid layout using various sizing units

css
📄 HTMLhtml
🎨 Fixed Width
100px
200px
100px
🎨 Fractional Units
1fr
2fr
1fr
🎨 Repeat & Minmax
Auto
Auto
Auto
Auto

Define Rows

Set the number and size of rows in your grid layout with flexible sizing options

css
📄 HTMLhtml
🎨 Fixed Heights
100px
200px
100px

Grid Gaps

Add consistent spacing between grid cells without using margins

css
📄 HTMLhtml
🎨 Gap Demo
1
2
3
4

Alignment

Justify Items (Horizontal)

Align grid items horizontally within their cells

css
📄 HTMLhtml
🎨 Justify Items
Start
Center
End

Align Items (Vertical)

Align grid items vertically within their cells

css
📄 HTMLhtml
🎨 Align Items
Start
Center
End

Place Items (Both)

Shorthand to align items both horizontally and vertically in one declaration

css
📄 HTMLhtml
🎨 Place Items
1
2
3
4

Justify Content (Grid)

Align the entire grid within its container when the grid is smaller than the container

css
📄 HTMLhtml
🎨 Justify Content
1
2
3

Grid Items

Position Columns

Control which columns a grid item spans using line numbers or named lines

css
📄 HTMLhtml
🎨 Column Span
Span 2
1
2
3

Position Rows

Control which rows a grid item spans using line numbers or span keyword

css
📄 HTMLhtml
🎨 Row Span
Span 2 Rows
1
2
3

Grid Area

Place items using grid area names or shorthand for row and column positioning

css
📄 HTMLhtml
🎨 Named Areas
Header
Main

Advanced Grid

Auto-Fit vs Auto-Fill

Create responsive grids that automatically adjust the number of columns

css
📄 HTMLhtml
🎨 Auto-Fit
1
2
3
🎨 Auto-Fill
1
2
3

Grid Auto Flow

Control how items are automatically placed in the grid

css
📄 HTMLhtml
🎨 Dense Packing
1
2 (wide)
3
4 (tall)
5
6

Subgrid

Allow nested grid items to inherit the parent grid's tracks

css
📄 HTMLhtml

Grid Functions

Powerful CSS functions for flexible and responsive grid sizing

css
📄 HTMLhtml

Common Layouts

Holy Grail Layout

Classic web layout with header, footer, main content, and sidebars using grid

css
📄 HTMLhtml
🎨 Holy Grail
Header
Main Content
Footer

Responsive Card Grid

css
🎨 Card Grid
Card 1
Card 2
Card 3
Card 4

Masonry-Style Grid

css