Flexbox Foreman — CSS

Foreman-level Flexbox: 8 advanced levels of flex-basis, flex-grow vs flex-shrink and gap. An advanced Flexbox game for Flexbox Ducks graduates.

What you learn in this game

flex-grow controls how much a flex item grows to fill empty space. A value of 1 = share the space equally. A value of 2 = take a 2x larger portion.

flex-shrink controls the shrinking when there isn't enough space. A value of 0 = never shrink. flex-basis sets the initial size before grow/shrink is applied.

The flex shorthand combines flex-grow, flex-shrink and flex-basis. flex: 1 = flex: 1 1 0%. flex: 0 0 200px = a fixed 200px.

gap adds spacing between flex/grid items without margin. Cleaner because there's no double margin and no need for :first-child/:last-child hacks.

What the levels ask

  1. The three .col stalls are only as wide as their goods. Share the leftover road evenly: give every .col flex-grow: 1.
  2. Bu Lastri has three times the fish. Give #sidebar flex-grow: 1 and #main flex-grow: 3: the leftover road is split in four, one part for #sidebar, three for #main.
  3. The #sidebar bread table is 200px long and cannot fold, and the road is narrow here. Give #sidebar flex-basis: 200px and flex-shrink: 0.
  4. On the wide road, write the rules short: flex: 0 0 200px on #sidebar (grow 0, shrink 0, basis 200px) and flex: 1 on #main.
  5. The stalls in #container are pressed together and buyers cannot get through. Give #container gap: 16px so there is an aisle between stalls.
  6. The bakery terrace fits only two stalls in a row. Give #grid flex-wrap: wrap and every .card flex: 0 0 calc(50% - 8px), so the stalls move down to the next row, two by two.
  7. The market's entrance row: the #logo name board and the #actions ticket booth must not shrink (flex-shrink: 0), the #nav signpost fills the leftover road (flex-grow: 1).
  8. The full market: the #left table and the #right rack stay 200px (flex: 0 0 200px), the #center stall takes the rest (flex: 1). The container is already display: flex.