CSS Grid Layout Guide _ CSS-Tricks
CSS Grid Layout Guide _ CSS-Tricks
HOME / GUIDES /
Our comprehensive guide to CSS grid, focusing on all the settings both
for the grid parent container and the grid child elements.
The ability to pass grid parameters down through nested elements (aka Values:
Hey! subgrids) has been moved to level 2 of the CSS Grid specification. Here’s a
quick explanation. <line> – can be a number to refer to a numbered grid line,
or a name to refer to a named grid line
span <number> – the item will span across the provided
number of grid tracks
span <name> – the item will span across until it hits the
grid-template-columns next line with the provided name
grid-template-rows auto – indicates auto-placement, an automatic span, or a
default span of one
Defines the columns and rows of the grid with a space- CSS
.item {
separated list of values. The values represent the track size, and grid-column-start: <number> | <name> | span <number> | s
the space between them represents the grid line. grid-column-end: <number> | <name> | span <number> | spa
grid-row-start: <number> | <name> | span <number> | span
Values: grid-row-end: <number> | <name> | span <number> | span <
}
<track-size> – can be a length, a percentage, or a fraction
of the free space in the grid using the fr unit (more on this
Examples:
unit over at DigitalOcean)
<line-name> – an arbitrary name of your choosing CSS
.item-a {
CSS grid-column-start: 2;
.container { grid-column-end: five;
grid-template-columns: ... ...; grid-row-start: row1-start;
/* e.g. grid-row-end: 3;
1fr 1fr }
minmax(10px, 1fr) 3fr
repeat(5, 1fr)
50px auto 100px 1fr
*/
grid-template-rows: ... ...;
/* e.g.
min-content 1fr min-content
100px 1fr max-content
*/
}
.item-b {
grid-column-start: 1;
grid-column-end: span col4-start;
grid-row-start: 2;
grid-row-end: span 2;
}
But you can choose to explicitly name the lines. Note the
bracket syntax for the line names:
CSS
.container {
grid-template-columns: [first] 40px [line2] 50px [line3]
grid-template-rows: [row1-start] 25% [row1-end] 100px [t
If no grid-column-end/grid-row-end is declared, the item will
}
span 1 track by default.
Items can overlap each other. You can use z-index to control
their stacking order.
grid-column
Note that a line can have more than one name. For example, grid-row
here the second line will have two names: row1-end and row2-
start:
Shorthand for grid-column-start + grid-column-end,
and grid-row-start + grid-row-end, respectively.
CSS
.container {
grid-template-rows: [row1-start] 25% [row1-end row2-star
} Values:
.item {
CSS
grid-column: <start-line> / <end-line> | <start-line> /
.container {
grid-row: <start-line> / <end-line> | <start-line> / spa
grid-template-columns: repeat(3, 20px [col-start]);
}
}
CSS
CSS
.item-c {
.container {
grid-column: 3 / span 2;
grid-template-columns: 20px [col-start] 20px [col-start]
grid-row: third-line / 4;
}
}
CSS
.item {
grid-column-start: col-start 2;
}
The free space is calculated a ter any non-flexible items. In this grid-area
example the total amount of free space available to the fr units
doesn’t include the 50px:
Gives an item a name so that it can be referenced by a template
created with the grid-template-areas property. Alternatively,
CSS
.container {
grid-template-columns: 1fr 50px 1fr 1fr; this property can be used as an even shorter shorthand
} for grid-row-start + grid-column-start + grid-row-
end + grid-column-end.
Values:
.item {
areas which are specified with the grid-area property.
grid-area: <name> | <row-start> / <column-start> / <row-
Repeating the name of a grid area causes the content to span }
those cells. A period signifies an empty cell. The syntax itself
provides a visualization of the structure of the grid.
Examples:
Values:
As a way to assign a name to the item:
<grid-area-name> – the name of a grid area specified
CSS
CSS
.item-d {
}
grid-area: 1 / col4-start / last-line / 6;
}
Example:
CSS
.item-a {
grid-area: header;
}
.item-b {
grid-area: main;
justify-self
}
.item-c {
grid-area: sidebar;
Aligns a grid item inside a cell along the inline (row) axis (as
} opposed to align-self which aligns along the block
.item-d { (column) axis). This value applies to a grid item inside a single
grid-area: footer;
cell.
}
Values:
.container {
display: grid;
start – aligns the grid item to be flush with the start edge of
grid-template-columns: 50px 50px 50px 50px;
grid-template-rows: auto;
the cell
grid-template-areas: end – aligns the grid item to be flush with the end edge of
"header header header header" the cell
"main main . sidebar"
center – aligns the grid item in the center of the cell
"footer footer footer footer";
} stretch – fills the whole width of the cell (this is the
default)
That’ll create a grid that’s four columns wide by three rows tall.
CSS
.item {
The entire top row will be composed of the header area. The justify-self: start | end | center | stretch;
middle row will be composed of two main areas, one empty }
cell, and one sidebar area. The last row is all footer.
Examples:
Each row in your declaration needs to have the same number of
CSS
cells. .item-a {
justify-self: start;
You can use any number of adjacent periods to declare a single }
.item-a {
Notice that you’re not naming lines with this syntax, just areas.
justify-self: end;
When you use this syntax the lines on either end of the areas
}
are actually getting named automatically. If the name of your
grid area is foo, the name of the area’s starting row line and
starting column line will be foo-start, and the name of its last
CSS
row line and last column line will be foo-end. This means that .item-a {
justify-self: center;
some lines might have multiple names, such as the far le t line
}
in the above example, which will have three names: header-
start, main-start, and footer-start.
CSS
.item-a {
justify-self: stretch;
}
grid-template
A shorthand for setting grid-template-rows, grid-template- To set alignment for all the items in a grid, this behavior can
columns, and grid-template-areas in a single declaration. also be set on the grid container via the justify-
items property.
Values:
Values:
It also accepts a more complex but quite handy syntax for
specifying all three. Here’s an example: start – aligns the grid item to be flush with the start edge of
the cell
CSS
.container { end – aligns the grid item to be flush with the end edge of
grid-template: the cell
[row1-start] "header header header" 25px [row1-end]
center – aligns the grid item in the center of the cell
[row2-start] "footer footer footer" 25px [row2-end]
/ auto 50px auto;
stretch – fills the whole height of the cell (this is the
} default)
CSS
.item {
That’s equivalent to this: align-self: start | end | center | stretch;
}
CSS
.container {
grid-template-rows: [row1-start] 25px [row1-end row2-sta
grid-template-columns: auto 50px auto; Examples:
grid-template-areas:
CSS
"header header header" .item-a {
"footer footer footer"; align-self: start;
} }
.item-a {
which is probably what you want to do in most cases, it’s align-self: end;
recommended to use the grid property instead of grid- }
template.
CSS
.item-a {
align-self: center;
}
column-gap
row-gap
grid-column-gap .item-a {
CSS
Specifies the size of the grid lines. You can think of it like
setting the width of the gutters between the columns/rows.
To align all the items in a grid, this behavior can also be set on
the grid container via the align-items property.
Values:
CSS
.container {
place-self
/* standard */
column-gap: <line-size>;
row-gap: <line-size>;
place-self sets both the align-self and justify-
} .item-a {
place-self: center;
}
.item-a {
Note: The grid- prefix will be removed and grid-column- place-self: center stretch;
}
gap and grid-row-gap renamed to column-gap and row-gap.
The unprefixed properties are already supported in Chrome
68+, Safari 11.2 Release 50+, and Opera 54+.
All major browsers except Edge support the place-
self shorthand property.
gap
grid-gap Special Units & Functions
Values:
You’ll likely end up using a lot of fractional units in CSS Grid,
<grid-row-gap> <grid-column-gap> – length values like 1fr. They essentially mean “portion of the remaining
space”. So a declaration like:
CSS
.container {
CSS
/* standard */ grid-template-columns: 1fr 3fr;
gap: <grid-row-gap> <grid-column-gap>;
/* old */ Means, loosely, 25% 75%. Except that those percentage values
grid-gap: <grid-row-gap> <grid-column-gap>;
are much more firm than fractional units are. For example, if
}
you added padding to those percentage-based columns, now
you’ve broken 100% width (assuming a content-box box
Example: model). Fractional units also much more friendly in
combination with other units, as you can imagine:
CSS
.container {
CSS
grid-template-columns: 100px 50px 100px;
grid-template-columns: 50px min-content 1fr;
grid-template-rows: 80px auto 80px;
gap: 15px 10px;
}
Values:
start – aligns items to be flush with the start edge of their Sizing Functions
cell
end – aligns items to be flush with the end edge of their cell
The fit-content() function uses the space available, but
center – aligns items in the center of their cell
never less than min-content and never more than max-
stretch – fills the whole width of the cell (this is the content.
default)
The minmax() function does exactly what it seems like: it sets
CSS a minimum and maximum value for what the length is able
.container {
to be. This is useful for in combination with relative units.
justify-items: start | end | center | stretch;
Like you may want a column to be only able to shrink so far.
}
This is extremely useful and probably what you want:
CSS
CSS
.container {
The min() function.
justify-items: start;
} The max() function.
CSS
.container {
justify-items: end; The repeat() Function and Keywords
}
/* easier: */
grid-template-columns:
CSS repeat(8, 1fr);
.container {
justify-items: stretch; /* especially when: */
} grid-template-columns:
repeat(8, minmax(10px, 1fr));
This behavior can also be set on individual grid items via But repeat() can get extra fancy when combined with
the justify-self property. keywords:
Aligns grid items along the block (column) axis (as opposed
This bears the most famous snippet in all of CSS Grid and one
to justify-items which aligns along the inline (row) axis). This
of the all-time great CSS tricks:
value applies to all grid items inside the container.
CSS
grid-template-columns:
Values:
repeat(auto-fit, minmax(250px, 1fr));
The spec has an official way now, and this is behind a flag in
Examples: Firefox:
CSS CSS
.container { .container {
align-items: start; display: grid;
} grid-template-columns: repeat(4, 1fr);
grid-template-rows: masonry;
}
CSS
.container {
See Rachel’s article for a deep dive.
align-items: end;
}
CSS
.container { Subgrid
align-items: center;
}
Subgrid is an extremely useful feature of grids that allows grid
items to have a grid of their own that inherits grid lines from
CSS the parent grid.
.container {
align-items: stretch; CSS
} .parent-grid {
display: grid;
grid-template-columns: repeat(9, 1fr);
}
This behavior can also be set on individual grid items via .grid-item {
grid-column: 2 / 7;
the align-self property.
display: grid;
There are also modifier keywords safe and unsafe (usage is like
grid-template-columns: subgrid;
align-items: safe end). The safe keyword means “try to
}
align like this, but not if it means aligning an item such that it .child-of-grid-item {
moves into inaccessible overflow area”, while unsafe will allow /* gets to participate on parent grid! */
.center {
display: grid;
place-items: center;
}
justify-content
Sometimes the total size of your grid might be less than the size
of its grid container. This could happen if all of your grid items
are sized with non-flexible units like px. In this case you can set
the alignment of the grid within the grid container. This
property aligns the grid along the inline (row) axis (as opposed
to align-content which aligns the grid along the block
(column) axis).
Values:
start – aligns the grid to be flush with the start edge of the
grid container
end – aligns the grid to be flush with the end edge of the grid
container
center – aligns the grid in the center of the grid container
stretch – resizes the grid items to allow the grid to fill the
full width of the grid container
space-around – places an even amount of space between
each grid item, with half-sized spaces on the far ends
space-between – places an even amount of space between
each grid item, with no space at the far ends
space-evenly – places an even amount of space between
each grid item, including the far ends
CSS
.container {
justify-content: start | end | center | stretch | space-
}
Examples:
CSS
.container {
justify-content: start;
}
CSS
.container {
justify-content: end;
}
CSS
.container {
justify-content: center;
}
CSS
.container {
justify-content: stretch;
}
CSS
.container {
justify-content: space-around;
}
CSS
.container {
justify-content: space-between;
}
CSS
.container {
justify-content: space-evenly;
}
align-content
Sometimes the total size of your grid might be less than the size
of its grid container. This could happen if all of your grid items
are sized with non-flexible units like px. In this case you can set
the alignment of the grid within the grid container. This
property aligns the grid along the block (column) axis (as
opposed to justify-content which aligns the grid along
the inline (row) axis).
Values:
start – aligns the grid to be flush with the start edge of the
grid container
end – aligns the grid to be flush with the end edge of the grid
container
center – aligns the grid in the center of the grid container
stretch – resizes the grid items to allow the grid to fill the
full height of the grid container
space-around – places an even amount of space between
each grid item, with half-sized spaces on the far ends
space-between – places an even amount of space between
each grid item, with no space at the far ends
space-evenly – places an even amount of space between
each grid item, including the far ends
CSS
.container {
align-content: start | end | center | stretch | space-ar
}
Examples:
CSS
.container {
align-content: start;
}
CSS
.container {
align-content: end;
}
CSS
.container {
align-content: center;
}
CSS
.container {
align-content: stretch;
}
CSS
.container {
align-content: space-around;
}
CSS
.container {
align-content: space-between;
}
CSS
.container {
align-content: space-evenly;
}
place-content
Values:
grid-auto-columns
grid-auto-rows
Values:
CSS
.container {
grid-auto-columns: <track-size> ...;
grid-auto-rows: <track-size> ...;
}
CSS
.container {
grid-template-columns: 60px 60px;
grid-template-rows: 90px 90px;
}
CSS
.item-a {
grid-column: 1 / 2;
grid-row: 2 / 3;
}
.item-b {
grid-column: 5 / 6;
grid-row: 2 / 3;
}
CSS
.container {
grid-auto-columns: 60px;
}
grid-auto-flow
If you have grid items that you don’t explicitly place on the grid,
the auto-placement algorithm kicks in to automatically place the
items. This property controls how the auto-placement
algorithm works.
Values:
CSS
.container {
grid-auto-flow: row | column | row dense | column dense;
}
Note that dense only changes the visual order of your items
and might cause them to appear out of order, which is bad for
accessibility.
Examples:
HTML
<section class="container">
<div class="item-a">item-a</div>
<div class="item-b">item-b</div>
<div class="item-c">item-c</div>
<div class="item-d">item-d</div>
<div class="item-e">item-e</div>
</section>
You define a grid with five columns and two rows, and set grid-
auto-flow to row (which is also the default):
CSS
.container {
display: grid;
grid-template-columns: 60px 60px 60px 60px 60px;
grid-template-rows: 30px 30px;
grid-auto-flow: row;
}
When placing the items on the grid, you only specify spots for
two of them:
CSS
.item-a {
grid-column: 1;
grid-row: 1 / 3;
}
.item-e {
grid-column: 5;
grid-row: 1 / 3;
}
CSS
.container {
display: grid;
grid-template-columns: 60px 60px 60px 60px 60px;
grid-template-rows: 30px 30px;
grid-auto-flow: column;
}
grid
Values:
Examples:
CSS
.container {
grid: 100px 300px / 3fr 1fr;
}
.container {
grid-template-rows: 100px 300px;
grid-template-columns: 3fr 1fr;
}
CSS
.container {
grid: auto-flow / 200px 1fr;
}
.container {
grid-auto-flow: row;
grid-template-columns: 200px 1fr;
}
CSS
.container {
grid: auto-flow dense 100px / 1fr 2fr;
}
.container {
grid-auto-flow: row dense;
grid-auto-rows: 100px;
grid-template-columns: 1fr 2fr;
}
CSS
.container {
grid: 100px 300px / auto-flow 200px;
}
.container {
grid-template-rows: 100px 300px;
grid-auto-flow: column;
grid-auto-columns: 200px;
}
CSS
.container {
grid: [row1-start] "header header header" 1fr [row1-end]
[row2-start] "footer footer footer" 25px [row2-end
/ auto 50px auto;
}
CSS
.container {
grid-template-areas:
"header header header"
"footer footer footer";
grid-template-rows: [row1-start] 1fr [row1-end row2-star
grid-template-columns: auto 50px auto;
}
This comment thread is closed. If you have important information to share, please contact us.
SUBSCRIBE
D I G I TA LO C E A N CSS -T R I C KS SO C I A L
About DO Contact RSS Feeds
Cloudways Write for CSS-Tricks! CodePen
Legal stuff Advertise with us Mastodon
Get free credit! Bluesky