CSS Grid Cheatsheet - Grid Layout Reference
All essential CSS Grid commands organized by use case, with 42+ entries you can copy and run directly. Find the right command fast when you need it.
Back to Web ServicesContainer Definition 8
display: gridEnable grid layout (block-level)
display: inline-gridEnable grid layout (inline-level)
grid-template-columns: 1fr 2fr 1frDefine column tracks & ratio
grid-template-rows: 100px auto 100pxDefine row tracks & sizes
grid-template-columns: repeat(3, 1fr)Repeat 3 equal columns
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr))Auto-fill responsive columns
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr))Auto-fit responsive columns
grid-template-areas: "header header" "sidebar main" "footer footer"Visual named-area layout
Gap & Alignment 10
gap: 16pxRow & column gap
row-gap: 16pxRow gap only
column-gap: 16pxColumn gap only
justify-items: stretchStretch horizontally in cell (default)
justify-items: centerCenter horizontally in cell
align-items: centerCenter vertically in cell
place-items: centerSet both horizontal & vertical
justify-content: centerAlign grid horizontally
align-content: centerAlign grid vertically
place-content: centerSet both grid alignments
Cells & Spans 7
grid-area: headerPlace item into named area
grid-area: 1 / 1 / 3 / 3Line-based: row-start/col-start/row-end/col-end
grid-column: 1 / 3Span columns: line 1 to 3
grid-row: 1 / span 2Span 2 rows from line 1
grid-column: 1 / -1Span full row (line 1 to end)
grid-column: span 2Span 2 columns
grid-row: span 3Span 3 rows
Explicit & Implicit Tracks 6
grid-auto-columns: 100pxImplicit column track size
grid-auto-rows: 100pxImplicit row track size
grid-auto-flow: rowAuto-placement: by row (default)
grid-auto-flow: columnAuto-placement: by column
grid-auto-flow: denseDense packing, fill gaps
grid-auto-flow: row denseRow-wise dense packing
Common Patterns 6
grid-template-columns: 1fr 1fr 1frThree equal columns
grid-template-columns: 200px 1frSidebar + main content
grid-template-columns: repeat(12, 1fr)12-column grid system
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr)Prevent overflow break
grid-template-areas: "header" "main" "footer"Single-column stacked layout
grid-column: 1 / -1Item spans full row
Responsive Tips 5
repeat(auto-fit, minmax(200px, 1fr))Auto-fit, empty tracks collapse
repeat(auto-fill, minmax(150px, 1fr))Auto-fill, keeps empty tracks
repeat(auto-fit, minmax(min(100%, 250px), 1fr))min() prevents small-screen overflow
@media (max-width: 768px) { grid-template-columns: 1fr }Single column on mobile
grid-auto-rows: minmax(150px, auto)Responsive row height
Tips
- fr 是弹性单位,1fr 2fr 表示第二列是第一列的两倍宽。
- minmax(200px, 1fr) 表示最小 200px,最大弹性填充,配合 auto-fill 实现响应式。
- grid-template-areas 用可视化方式定义布局,代码可读性更好。
- gap 替代了旧的 grid-gap,是最新的标准写法。
- auto-fit 会折叠空轨道拉伸填满,auto-fill 保留空轨道,响应式布局常用 auto-fit。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us