In this lesson, you tried flow, flexbox and grid on the same seven event cards and watched what each one does as the window narrows. Two questions decide which one a parent should use: does it arrange one direction or two, and who sets the sizes, the content (each card’s own text) or the layout (sizes you set on the parent)?
Key Points
- Where you start. With
display: flexon the cards’ wrapper,.event-list, all seven cards sit in one row, squeezed so tight that their words break apart. Rosa wants them in rows that fit the column. - Follow along or watch. Every change in the video is a live edit in DevTools’ Styles pane, and nothing is saved. Select the event list in the Elements panel and click the flex badge next to it, and DevTools draws its flex overlay around the container and between its items.
- Flow is for a document. Change
flextoblock, and the cards stack in normal flow. Each card takes the column’s full width, 752 pixels, and the next card starts below it. Flow arranges one direction, down the page, at full width. That suits a document of headings and paragraphs. For seven short cards, it’s a long scroll. - Flexbox sizes from the content out. Without wrapping, flexbox has to fit all seven cards on one line, so it squeezes them. Add
flex-wrap: wrap, and the row can break when the next card doesn’t fit, so each card gets the width its content asks for: its text on one line. The seed swap card is the widest, at 541 pixels, and the Plant Out the Tomatoes card the narrowest, at 514. No two fit side by side, so each card gets a line of its own. - The right edges don’t line up. The lines stack, but flexbox arranges along each line only, in one direction. Flow stretched every card to 752 pixels. Here each card stops where its text ends, and the rest of each line is left over. Narrow the window in device mode from 800 to 600 pixels, and the cards keep their widths. Only the leftover space at the end of each line shrinks.
- Grid sizes from the layout in. With a template for three equal columns and
display: grid, every card is 251 pixels wide, whatever its content, and the cards line up in columns and in rows. The badge now reads grid, and clicking it turns on the grid overlay, which draws the lines between the columns and rows. - The grid keeps its columns. At 500 pixels wide, the grid still has three columns, each 151 pixels wide, and the text wraps to fit. The template sets the column widths, and the content fits into them. And the cards line up in two directions at once.
- Floats are something you’ll meet in older code, once used for whole page layouts. Today, they’re still right for one job, letting text wrap around a photo.
The Rule
- Flow is for a document, one box after another.
- Flexbox is for one row or column, sized from the content out.
- Grid is for rows and columns, sized from the layout in.
Rosa’s cards should line up in rows and columns, so for them, that’s grid. You’ll come back to this choice once flexbox and grid have had their own lessons.
Final Code
Nothing was saved, so events/layout.css still has its flex rule from the last lesson:
events/layout.css
/* Layouts to try on the events pages */
.event-list {
display: flex;
}
For reference, here are the two rules the video tried on the event list. The first is flexbox with wrapping:
.event-list {
display: flex;
flex-wrap: wrap;
}
The second is grid with three equal columns:
.event-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Next is the lesson Pin a Badge to a Card, where you’ll take a box out of the flow altogether.