CSS Grid
Like Flexbox, but two-dimensional. Allows to create complex layouts. Somehow adds additional syntax, which doesn’t have any logic:
- Positioning items adds
/
(not division) and has two different meanings! - Template areas add markdown-like syntax for naming areas
- Naming lines adds
[]
fr
unit. Wat. Flexbox grow/shrink is basically the same thing, but it doesn’t usefr
.span
keyword. Has nothing in to do with HTML<span>
.span 2
means “take up 2 cells”.
display: grid
and there you go.
# Terminology1
Grid lines - lines that divide columns and rows.
Grid cell - a single unit of a grid
Grid area - rectangular space surrounded by four grid lines
Grid track - space between two grid lines
Grid row - horizontal track of a grid
Grid column - vertical track of a grid
# Properties
grid-template-rows
, grid-template-columns
Set sizes of grid rows and columns. Could be in px
, %
, etc, or in special unit fr
(fraction). You can mix those:
|
|
grid-auto-rows
, grid-auto-columns
Set sizes for rows/columns that overflow the template layout.
grid-row
, grid-column
2
For positioning grid items, like this:
|
|
Yes, the second way syntactically looks like division.
# Functions
repeat()
- a special Grid function to represent a recurring pattern of rows or columns. E.g. repeat(4, 10rem)
- 10rem 10rem 10rem 10rem
.
minmax()
- the name says it all. Useful for setting minimum and maximum row/col size, like minmax(10rem, auto)
.
# Named template areas
|
|
# Auto-filling and auto-fitting tracks
|
|
# Links
- Learn CSS Grid
- CSS Grid PlayGround | Mozilla
- Layout Land - YouTube
- Grid by Example
- A simple visual cheatsheet