CSS Grid Guide
A complete reference for CSS Grid Layout with properties, values, and practical examples. Master responsive layouts without media queries.
Container Properties
Properties applied to the grid container (parent element).
display: grid
.container {
display: grid; /* or inline-grid */
}
Defines an element as a grid container and establishes a new grid formatting context.
grid-template-columns
.container {
/* Fixed width columns */
grid-template-columns: 200px 200px 200px;
/* Flexible columns */
grid-template-columns: 1fr 2fr 1fr;
/* Mixed units */
grid-template-columns: 200px 1fr auto;
/* Repeat notation */
grid-template-columns: repeat(3, 1fr);
/* Auto-fill - as many as fit */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* Auto-fit - expands items to fill space */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
grid-template-rows
.container {
/* Fixed height rows */
grid-template-rows: 100px 200px;
/* Flexible rows */
grid-template-rows: 1fr 2fr;
/* Mix with minmax */
grid-template-rows: minmax(100px, auto) 1fr;
}
grid-template-areas
.container {
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
gap (grid-gap)
.container {
/* Both row and column gap */
gap: 20px;
/* Row gap, column gap */
gap: 20px 40px;
/* Individual properties */
row-gap: 20px;
column-gap: 40px;
}
grid-auto-columns / grid-auto-rows
.container {
/* Size of implicitly created columns */
grid-auto-columns: 200px;
/* Size of implicitly created rows */
grid-auto-rows: minmax(100px, auto);
}
grid-auto-flow
.container {
grid-auto-flow: row; /* default */
grid-auto-flow: column;
grid-auto-flow: dense; /* fill holes */
grid-auto-flow: row dense;
}
Item Properties
Properties applied to grid items (child elements).
grid-column / grid-row
.item {
/* Start and end lines */
grid-column: 1 / 3; /* spans columns 1-3 */
grid-row: 1 / 3; /* spans rows 1-3 */
/* Span notation */
grid-column: span 2; /* spans 2 columns */
grid-row: span 3; /* spans 3 rows */
/* Individual properties */
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3;
/* Span to end */
grid-column: 1 / -1; /* spans all columns */
}
grid-area
.item {
/* Shorthand: row-start / col-start / row-end / col-end */
grid-area: 1 / 1 / 3 / 3;
/* Or reference a named area */
grid-area: header;
}
justify-self
.item {
justify-self: start; /* align to start */
justify-self: end; /* align to end */
justify-self: center; /* center horizontally */
justify-self: stretch; /* fill width (default) */
}
align-self
.item {
align-self: start; /* align to top */
align-self: end; /* align to bottom */
align-self: center; /* center vertically */
align-self: stretch; /* fill height (default) */
}
place-self
.item {
/* Shorthand for align-self / justify-self */
place-self: center;
place-self: start end;
}
Sizing Functions
fr (fraction unit)
Distributes available space as fractions
grid-template-columns: 1fr 2fr 1fr;
minmax(min, max)
Defines a size range for a track
grid-template-columns: minmax(200px, 1fr);
fit-content(limit)
Uses content size, capped at limit
grid-template-columns: fit-content(300px);
min-content
Minimum size without overflow
grid-template-columns: min-content 1fr;
max-content
Maximum size based on content
grid-template-columns: max-content 1fr;
auto
Behaves like minmax(min-content, max-content)
grid-template-columns: auto 1fr auto;
Alignment
Control alignment of items and tracks within the grid.
justify-items
.container {
justify-items: start; /* align to start (left) */
justify-items: end; /* align to end (right) */
justify-items: center; /* center horizontally */
justify-items: stretch; /* fill width (default) */
}
align-items
.container {
align-items: start; /* align to top */
align-items: end; /* align to bottom */
align-items: center; /* center vertically */
align-items: stretch; /* fill height (default) */
}
| Property | Applies To | Values | Description |
|---|---|---|---|
justify-items |
Container | start | end | center | stretch | Aligns items horizontally within their cells |
align-items |
Container | start | end | center | stretch | Aligns items vertically within their cells |
place-items |
Container | align-items / justify-items | Shorthand for both alignments |
justify-content |
Container | start | end | center | stretch | space-between | space-around | space-evenly | Aligns the grid horizontally within the container |
align-content |
Container | start | end | center | stretch | space-between | space-around | space-evenly | Aligns the grid vertically within the container |
place-content |
Container | align-content / justify-content | Shorthand for grid alignment |
Common Patterns
Holy Grail Layout
.container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.header { grid-column: 1 / -1; }
.footer { grid-column: 1 / -1; }
Holy Grail Layout
Responsive Card Grid (No Media Queries)
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
}
/* Cards automatically wrap and fill space */
Centered Layout
.container {
display: grid;
place-items: center;
min-height: 100vh;
}
/* Perfect centering with one line */
Centered Layout
12-Column Layout System
.container {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
}
.span-6 { grid-column: span 6; } /* Half width */
.span-4 { grid-column: span 4; } /* Third width */
.span-3 { grid-column: span 3; } /* Quarter width */
Asymmetric Layout
.container {
display: grid;
grid-template-columns: 2fr 1fr;
grid-auto-rows: minmax(200px, auto);
gap: 2rem;
}
.featured {
grid-column: 1 / -1;
grid-row: span 2;
}
Masonry-Like Layout (CSS Grid)
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-auto-rows: 10px;
gap: 1rem;
}
.item {
/* Use JavaScript to calculate span based on content height */
/* or use CSS grid-row: span X; */
}
Note: True masonry layout is coming to CSS Grid with grid-template-rows: masonry; but browser support is limited. For now, use JavaScript to calculate row spans or consider CSS Multi-column Layout.
Responsive Grids
Auto-Fit vs Auto-Fill
/* auto-fill: creates empty columns to fill space */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* auto-fit: collapses empty columns, items expand */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
auto-fill (maintains empty columns)
auto-fit (items expand to fill)
Tip: Use auto-fit for fewer items that should expand. Use auto-fill to maintain consistent column sizes.
Responsive with Media Queries
.container {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 768px) {
.container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.container {
grid-template-columns: repeat(3, 1fr);
}
}
Container Query Grids
.wrapper {
container-type: inline-size;
}
.grid {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@container (min-width: 600px) {
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
@container (min-width: 900px) {
.grid {
grid-template-columns: repeat(3, 1fr);
}
}
Container queries allow grids to respond to their container size rather than viewport size.