{ ILoveJS }

CSS Grid Cheatsheet

css

Complete CSS Grid reference with examples.

7 sections · 42 items

Container Properties - Display & Template

display
display: grid | inline-grid;

Defines the element as a grid container

css
.container { display: grid; }
grid-template-columns
grid-template-columns: <track-size> ... | <line-name> <track-size> ...;

Defines the columns of the grid with a space-separated list of values

css
grid-template-columns: 200px 1fr 2fr;
grid-template-rows
grid-template-rows: <track-size> ... | <line-name> <track-size> ...;

Defines the rows of the grid with a space-separated list of values

css
grid-template-rows: 100px auto 200px;
grid-template-areas
grid-template-areas: "<area-name> ..." ...;

Defines named grid areas using a visual ASCII-art style syntax

css
grid-template-areas:
  "header header"
  "sidebar main"
  "footer footer";
grid-template
grid-template: <rows> / <columns>;

Shorthand for grid-template-rows, grid-template-columns, and grid-template-areas

css
grid-template: 100px 1fr / 200px 1fr;

Container Properties - Gap & Spacing

gap
gap: <row-gap> <column-gap>;

Sets the gap between rows and columns

css
gap: 20px 10px;
row-gap
row-gap: <length> | <percentage>;

Sets the gap between grid rows

css
row-gap: 15px;
column-gap
column-gap: <length> | <percentage>;

Sets the gap between grid columns

css
column-gap: 20px;

Container Properties - Alignment

justify-items
justify-items: start | end | center | stretch;

Aligns grid items along the inline (row) axis within their cell

css
justify-items: center;
align-items
align-items: start | end | center | stretch | baseline;

Aligns grid items along the block (column) axis within their cell

css
align-items: start;
place-items
place-items: <align-items> <justify-items>;

Shorthand for align-items and justify-items

css
place-items: center start;
justify-content
justify-content: start | end | center | stretch | space-around | space-between | space-evenly;

Aligns the entire grid along the inline (row) axis within the container

css
justify-content: space-between;
align-content
align-content: start | end | center | stretch | space-around | space-between | space-evenly;

Aligns the entire grid along the block (column) axis within the container

css
align-content: center;
place-content
place-content: <align-content> <justify-content>;

Shorthand for align-content and justify-content

css
place-content: center space-evenly;

Container Properties - Auto Placement

grid-auto-columns
grid-auto-columns: <track-size> ...;

Specifies the size of implicitly created column tracks

css
grid-auto-columns: 100px;
grid-auto-rows
grid-auto-rows: <track-size> ...;

Specifies the size of implicitly created row tracks

css
grid-auto-rows: minmax(100px, auto);
grid-auto-flow
grid-auto-flow: row | column | row dense | column dense;

Controls how auto-placed items are inserted in the grid

css
grid-auto-flow: row dense;
grid
grid: <grid-template> | <grid-auto-flow> [<grid-auto-rows> / <grid-auto-columns>];

Shorthand for all grid container properties

css
grid: auto-flow dense / 1fr 1fr;

Item Properties

grid-column-start
grid-column-start: <line> | span <number> | auto;

Determines where a grid item starts on the column axis

css
grid-column-start: 2;
grid-column-end
grid-column-end: <line> | span <number> | auto;

Determines where a grid item ends on the column axis

css
grid-column-end: span 2;
grid-column
grid-column: <start> / <end>;

Shorthand for grid-column-start and grid-column-end

css
grid-column: 1 / 3;
grid-row-start
grid-row-start: <line> | span <number> | auto;

Determines where a grid item starts on the row axis

css
grid-row-start: 1;
grid-row-end
grid-row-end: <line> | span <number> | auto;

Determines where a grid item ends on the row axis

css
grid-row-end: span 3;
grid-row
grid-row: <start> / <end>;

Shorthand for grid-row-start and grid-row-end

css
grid-row: 2 / 4;
grid-area
grid-area: <name> | <row-start> / <col-start> / <row-end> / <col-end>;

Assigns an item to a named area or specifies its position

css
grid-area: header;
justify-self
justify-self: start | end | center | stretch;

Aligns a single grid item along the inline (row) axis

css
justify-self: end;
align-self
align-self: start | end | center | stretch | baseline;

Aligns a single grid item along the block (column) axis

css
align-self: center;
place-self
place-self: <align-self> <justify-self>;

Shorthand for align-self and justify-self

css
place-self: center end;

Sizing Functions & Units

fr unit
<number>fr

Represents a fraction of the available space in the grid container

css
grid-template-columns: 1fr 2fr 1fr;
repeat()
repeat(<count>, <track-list>)

Repeats a track pattern a specified number of times

css
grid-template-columns: repeat(3, 1fr);
minmax()
minmax(<min>, <max>)

Defines a size range between min and max values

css
grid-template-columns: minmax(200px, 1fr);
auto-fill
repeat(auto-fill, <track-size>)

Creates as many tracks as fit in the container, including empty tracks

css
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
auto-fit
repeat(auto-fit, <track-size>)

Creates as many tracks as fit, collapsing empty tracks to zero

css
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
fit-content()
fit-content(<length-percentage>)

Clamps size to content up to a maximum value

css
grid-template-columns: fit-content(300px) 1fr;
min-content
min-content

Size based on the minimum content size of the track items

css
grid-template-columns: min-content 1fr;
max-content
max-content

Size based on the maximum content size of the track items

css
grid-template-columns: max-content 1fr;

Common Patterns

Basic 3-column layout
display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;

Creates an equal three-column grid with consistent gaps

css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
Responsive auto-fit grid
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

Creates a responsive grid that adjusts column count based on container width

css
.responsive {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
Holy Grail layout
grid-template: auto 1fr auto / auto 1fr auto;

Classic header-sidebar-main-sidebar-footer layout using named areas

css
.layout {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav main aside"
    "footer footer footer";
}
Sidebar fixed width
grid-template-columns: 250px 1fr;

Fixed sidebar with flexible main content area

css
.sidebar-layout {
  display: grid;
  grid-template-columns: 250px 1fr;
}
Centered single item
display: grid; place-items: center;

Centers a single item both horizontally and vertically

css
.centered {
  display: grid;
  place-items: center;
  height: 100vh;
}
Spanning full width
grid-column: 1 / -1;

Makes an item span from first to last column line

css
.full-width {
  grid-column: 1 / -1;
}

Related Content