Flexbox lines things up in one direction. Grid works in two at once: rows and columns. It's the easiest way to build galleries, card grids and whole page layouts.
flag
What you'll learn
How display: grid places the children of a box in rows and columns.
How to set columns and rows with px, fr and repeat(), and add gaps.
How to plan a whole page with named areas, line items up inside their cells, and move the whole grid.
waving_handLet's draw a grid
You add display: grid; to the parent, called the grid container. Then you describe its columns and rows, and its children, the grid items, fill the cells one by one, from left to right and top to bottom.
calendar_month
Think of a calendar page. It has seven columns, one for each day of the week, all the same width. The dates fill the boxes in order, and a new row starts every week.
There is a small gap between every box. A CSS grid works the same way: you choose the columns, and the items fill them in order.
The month starts on a Thursday, so the first four boxes stay empty, and the dates fill the rest of the boxes in order. One line of CSS sets all seven columns.
searchA closer look
Columns and the fr unit
grid-template-columns lists one size for each column. 90px 90px 90px makes three fixed columns.
fr means "a fraction of the free space": 1fr 1fr 1fr makes three equal columns that fill the container, and 1fr 2fr makes the second column twice as wide as the first.
repeat(3, 1fr) is a short way to write 1fr 1fr 1fr.
Rows and gaps
grid-template-rows works the same way for rows. Rows you don't list are added as needed, and they fit their content. gap adds space between all rows and columns, just like in flexbox.
Planning a page with grid-template-areas
grid-template-areas lets you draw the page with words. Each string is one row, and each word is one column. Repeat a word to make an area wider or taller. Then put each item in its area with grid-area (more about it in CSS Lesson 10).
"top top" "menu main" "foot foot"
topmenumainfoot
"menu top" "menu main" "menu foot"
menutopmainfoot
"top top" "main side"
topmainside
Lining items up inside their cells
By default, every item stretches to fill its cell. justify-items moves all items left and right inside their cells, and align-items moves them up and down. Here, both use the same value.
stretch
ABCD
start
ABCD
center
ABCD
Moving the whole grid: justify-content and align-content
Sometimes the columns are narrower than the container, for example three 40px columns in a wide box. Then there is leftover space, and justify-content decides where the whole grid goes: left, center, right, or spread out between the columns.
align-content does the same up and down, when the container is taller than its rows.
justify-content: start
ABC
justify-content: center
ABC
justify-content: space-between
ABC
align-content: center
ABC
constructionLet's build it
Let's place six Front Page Bytes category cards in a grid, then plan a whole page. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
display: grid; turns .grid into a grid container, and grid-template-columns makes three columns, each 90 pixels wide. The six cards fill them in order: three per row.
The columns are now a fixed 80px each, so the grid is narrower than the page, and there is space left over on the right. justify-content: center; moves the whole grid to the middle, leaving equal space on both sides.
A new page: a top bar, a menu, the main stories and a footer. grid-template-areas draws the plan as three rows of words, and each item is put in its place with grid-area. "top top" makes the top bar cover both columns.
sticky_note_2Quick notes
display: gridTurn on grid
.grid { display: grid; }
How to use
display: grid;Goes on the parent. Its children are placed in rows and columns.
The parent is the grid container, and its children are grid items.
Without columns set, there is one column, so nothing seems to change yet.
Examples
Turn a box of cards into a grid.
.cards { display: grid;}
grid-template-columnsThe columns
.grid { grid-template-columns: repeat(3, 1fr); }
How to use
90px 90px 90pxThree columns, each 90px wide.
1fr 2frTwo columns. The second gets twice the free space.
repeat(4, 1fr)Four equal columns, written the short way.
200px 1frA fixed column, then one that takes the rest.
"main" now covers an L shape: one cell in the second row and one in the third. Areas have to be rectangles, so the browser skips the whole plan. The area names no longer point anywhere, so all four items land in the same extra cell, piled on top of each other. Only the last one, the footer, can be seen.
tips_and_updatesTip: Make each area a rectangle, like "foot foot" on the last row.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Turn .gallery into a grid container.not done yet
Give .gallery three columns with grid-template-columns.not done yet
Add a gap between the photos.not done yet
Center the photos inside their cells with justify-items.not done yet