Loading video player…

Choose Flow, Flexbox or Grid

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: flex on 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 flex to block, 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:

Language: CSS Filename: 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:

Language: CSS
.event-list {
  display: flex;
  flex-wrap: wrap;
}

The second is grid with three equal columns:

Language: CSS
.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.

00:00 In the last lesson, you put display: flex on the cards’ wrapper, the event list. Now all seven cards sit in one row, squeezed so tight that their words break apart.

00:11 Rosa wants the cards in rows that fit the column.

00:15 Follow along in DevTools, or watch. Nothing here gets saved. Select the event list in the Elements panel. Next to it, click the flex badge. DevTools draws its flex overlay around the container and between its items.

00:30 Three inside layouts could arrange the cards: flow, flexbox and grid. You’ll try each one on these cards as the window narrows. Two questions decide the choice.

00:41 Does it arrange one direction or two? And who sets the sizes: each card’s content, or the layout on the parent?

00:51 In the Styles pane, layout.css sets display: flex on the event list. Change flex to block, and the cards stack again. That’s normal flow. Each card takes the column’s full width, 752 pixels, and the next card starts below it.

01:08 So 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.

01:18 Change it back to flex, and the row returns.

01:23 Now add flex-wrap: wrap to the rule. It lets the row break when the next card doesn’t fit. Without it, flexbox had to fit all seven cards on one line, so it squeezed them.

01:35 With room to wrap, each card gets the width its content asks for: its text on one line. The seed swap card is the widest, at 541 pixels. The tomato card is the narrowest, at 514.

01:48 No two fit side by side, so each card gets a line of its own. Notice that the right edges don’t line up. The lines stack, but flexbox arranges along each line only, in one direction.

02:02 Flow stretched every card to 752. Here each card stops where its text ends, and the rest of the line is left over. That’s sizing from the content out.

02:13 Now turn on device mode, type 800 as the width, and drag the handle down to 600. The cards keep their widths. Only the leftover space shrinks.

02:26 Type 800 as the width again. In the Styles pane, delete the wrap line. Add a template for three equal columns, then change flex to grid. Its syntax comes later.

02:38 The badge now reads grid. Click it, and the grid overlay draws the lines between the columns and rows. Every card is now 251 pixels wide, whatever its content.

02:50 The cards line up in columns and in rows. In flexbox, the cards kept their own widths. Your turn: pause the video and predict how many columns this grid will have at 500 pixels wide.

03:12 Three. The columns narrow together, to 151 pixels each, and the text wraps to fit. The template sets the column widths, and the content fits into them. That’s sizing from the layout in.

03:26 And the cards line up in two directions at once.

03:31 So choose with those two questions. 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.

03:45 Rosa’s cards should line up in rows and columns, so for them, that’s grid. You’ll also meet floats in older code, once used for whole page layouts. Today, they’re still right for one job, letting text wrap around a photo.

04:01 That’s all you need about flexbox and grid for now. You’ll come back to this choice once both have had their lessons. Nothing was saved, so layout.css still has its flex rule.

04:13 Next is the lesson Pin a Badge to a Card, where you’ll take a box out of the flow altogether.

Become a Member to join the conversation.