grid
styles,grid-template-columns
grid-template-rows
grid-template-areas
grid-template
grid-auto-columns
grid-auto-rows
grid
gap
justify-content
justify-items
align-content
align-items
fr
min-content
max-content
fit-content()
repeat()
minmax()
auto-fit
auto-fill
span
grid-column-start
grid-column-end
grid-column
grid-row-start
grid-row-end
grid-row
grid-area
order
align-self
justify-self
grid-template-columns
grid-template-rows
grid-template-areas
gap
fr
repeat()
grid-area
.site {
display: grid;
grid-template-columns: 1fr 40em 20em 1fr;
grid-template-areas:
"header header header header"
" . content sidebar . "
"footer footer footer footer";
gap: 12px 16px;
}
.site__header {
grid-area: header;
}
.site__main {
grid-area: content;
}
.site__aside {
grid-area: sidebar;
}
.site__footer {
grid-area: footer;
}
grid-template-columns
Set the proportions for tracks along the inline axis of the grid container
Value: a series of valid length values
grid-template-rows
Set the proportions for tracks along the block axis of the grid container
Value: a series of valid length values
Fixed heights => overflow
Flexible row height values: auto
, min-content
, max-content
Inline: start to end of a line of text in the block
Block: start to end of the block of text
dir=ltr
dir=rtl
writing-mode: vertical-lr
grid-template-areas
.
creates a “blank” areagrid-template-areas
.grid {
grid-template-areas:
"header header header header"
". body sidebar . "
"footer footer footer footer";
}
grid-template-areas
.grid {
grid-template-areas:
"👱♀️ 👱♀️ 👱♀️ 👱♀️"
". 📄 📶 . "
"🦶 🦶 🦶 🦶";
}
grid-template-areas
One rectangle
Adjacent cells
Strings, not integers
grid-template
Assign rows, columns, and areas
.grid {
grid-template:
"header header header header" auto
". body sidebar . " auto
"footer footer footer footer" auto /
1fr 40em 20em 1fr;
}
gap
grid-gap
row-gap column-gap
gap: 20px 30px
fr
One “fraction” of the grid’s free space
Calculated after non-fr
space is used
Based on total number of fr
in the row or column
repeat()
Repeats a certain value a specified number of times
repeat(<count>, <length>)
repeat(12, 1fr)
grid-area
Values:
grid-template-areas
namerow-start / column-start / row-end / column-end
display: grid;
justify-content: center;
align-items: center;
.row {
display: grid;
grid-template:
"title date" auto
"actions status" auto /
1fr max-content;
gap: .5rem;
}
.employee {
display: grid;
grid-template:
"photo header" auto
"photo awards" auto
"bio awards" auto /
30% 1fr;
gap: 1rem 3rem;
}
.employee {
grid-template:
"photo header" auto
"bio bio " auto
"awards awards" auto /
150px 1fr;
gap: 1rem;
}
.employee {
grid-template:
"photo" auto
"header" auto /
1fr;
}
.employee__awards,
.employee__bio {
display: none;
}
.image-grid {
grid-template:
" i1 i1 i2 i3" 1fr
" i1 i1 i4 i3" 1fr
" i6 i7 i4 i5" 1fr
" i6 i8 i8 i9" 1fr /
1fr 1fr 1fr 1fr;
gap: 10px;
}
img
assigned to a grid-area
obeys the grid cell size,
not its intrinsic aspect ratio
Wrap images in a container element
Some browsers animate
grid-template-columns
grid-template-rows
gap
Can’t animate elements between areas
Fallback layout CSS (if possible)
Autoprefixer can help (if necessary)
No gap
support
jdsteinbach