Grid

CSS Grid layout patterns: column templates, named areas, auto-fill/fit, dense packing, and responsive collapse.

1. Fixed Column Counts

grid-cols-N sets an explicit number of equal-width columns. Pair with gap-* for gutter control.

1
2
3
4
1
2
3
4
5
6
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
9
10
11
12

2. Named Template Areas

grid-template-areas names zones so placement is self-documenting. Not expressible via Tailwind arbitrary values — requires custom CSS (see style block).

headerHeader
sidebarSidebar
mainMain
headerHeader
navNav
mainMain
asideAside

3. auto-fill vs auto-fit

Both fill as many columns as fit the container at a minimum cell width. The difference appears when there are fewer items than columns: auto-fill keeps phantom empty tracks; auto-fit collapses them so real items expand.

1
2
3
1
2
3
1
2
3
4
5
6
7
8
9
10
11

4. Span — Column & Row

col-span-N and row-span-N let individual items bridge multiple tracks — essential for feature highlights, hero cards, and editorial layouts.

col-span-4 (full width)
col-span-2
1
1
1
col-span-3
row-span-2
A
B
C
D
Featured
Thumb 1
Thumb 2

5. Dense Packing

grid-flow-dense back-fills gaps left by spanning items. Critical for image galleries and dashboard widgets where visual order matters less than density.

1
2 wide (col-span-3)
3
4
5 wide
6
7
1
2 wide (col-span-3)
3
4
5 wide
6
7

6. Responsive Collapse

Tailwind breakpoint prefixes on grid-cols-* collapse multi-column grids to single-column on small screens without a media query in your CSS.

Card 1
Card 2
Card 3
Card 4
Card 5
Card 6
Card 7
Card 8

Uses minmax(min(100%, 10rem), 1fr) — a single rule that gracefully goes from 1 col on mobile to N cols on wide screens without breakpoints.

Main content (2fr)
Sidebar (1fr)

7. Gap Control

gap-* sets row and column gap uniformly. gap-x-* / gap-y-* set them independently — useful when vertical rhythm should differ from horizontal spacing (e.g. card grids).

1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
9

8. Interactive Playground

Adjust columns, gap, flow, and item count. The generated CSS is shown below.


            

9. Edge Cases

Scenarios that surface bugs in grid implementations. Test before shipping.

No items to display
Only item — fills row
Supercalifragilisticexpialidocious-overflow-word
Normal
Normal

minmax(0, 1fr) is critical — 1fr alone has an implicit min of auto which lets cells overflow their track.

1
2
3
4
5
6
7

7 items on a 4-col grid — last row has 3 cells and one empty track. Consider col-span on the last item or grid-flow-dense if this looks wrong in context.