Like a photo gallery or product cards, you want to make a grid neatly lined up across and down. What shows its strength there is Grid. Where Flexbox is good at “lining things up in one row,” Grid is good at laying things out in a grid pattern.
Decide how many columns
Just like Flexbox, you wrap the things you want to line up in a parent and put display: grid; on that parent. The difference is where you write the column design.
.parent {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 16px;
}grid-template-columns: 1fr 1fr 1fr; means “make three equal columns.” fr is a unit of “proportion,” so 1fr 1fr 1fr splits evenly into three. The parts inside fill into that grid neatly, top to bottom in order.
Place six boxes and they wrap at three columns into a two-row grid. Just decide the number of columns, and the rest lines up like tiles automatically.
1fr 1fr 1fr makes three columns’ worth of “slots,” and A → B → C → D… fill in from the top in order.You can distribute column widths freely
Change the fr numbers and you can also change the distribution of column widths.
.parent {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 16px;
}This is “two columns, wider on the left (2), narrower on the right (1).” Since fr divides the remaining space by ratio, it grows and shrinks while keeping the proportion, even when the screen size changes.
Change the fr numbers and the distribution of column widths changes right along with them. Let’s compare.
2fr 1fr, the left box comes out exactly twice as wide as the right.fr ratios can be decimals too
Besides whole-number ratios like 2fr 1fr, you can also specify decimals, like 1.05fr 0.95fr.
.hero {
display: grid;
grid-template-columns: 1.05fr 0.95fr;
}When an exact half of 1fr 1fr looks a little mechanical, making the left just a touch wider — decimal fr is handy for that kind of subtle adjustment. The meaning of the numbers themselves is the same as with whole numbers: they just divide up by ratio.
Aligning parts within a cell of the grid: justify-items / justify-self
Grid also has tools for deciding how to align a part within a cell of the grid.
.parent {
display: grid;
justify-items: start;
}justify-items goes on the parent and aligns all the children by “where to place them within the cell” (start = left, center = middle, stretch = fill the whole cell, the default). When you want to align just one child on its own, put justify-self on that child itself.
.btn {
justify-self: start;
}justify-items is everyone all together, justify-self is that child alone — the difference is aligning at the parent, or changing just the one child.
With the default stretch, a part spreads to fill the cell; with start, it shrinks down to the width of its content and moves to the left. Only the one with justify-self can be aligned differently from the rest.
stretch fills the cell with the box, start pulls it to the left at the width of its content. Set just one to justify-self: center and only that box comes to the middle of its cell.Summary of this lesson
- Decide the number of columns with
display: grid;plusgrid-template-columnson the parent fris a unit of proportion (1fr 1frsplits evenly into two; you can use decimals too)- Choose by role: one row means Flexbox, a grid means Grid
justify-items(parent, everyone) /justify-self(child, just one) decide the alignment within a cell
