HTML and CSS · Chapter 9 of 12

Flexbox

By the end of this chapter you can lay out a row or a column of boxes with flexbox: put them side by side, space them, line them up along both axes, let them grow, shrink or wrap onto new lines, and centre anything inside its box, both ways. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: CSS: the layout
  • 4 tasks
  • HTML and CSS
  • In the all labs plan

The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.

Why you need this

Everything on Parda's page so far stacks: each block starts on a new line, one under another. Ira's design has rows. The header's four links sit side by side under the name. Each show card has its poster on the left and its details beside it. The twelve venue cards sit three to a row on a laptop, two on a tablet and one on a phone. And Datta's sold-out stamp for Puneri Patya has its label in the exact middle of the box.

Chapter 8's boxes have sizes, padding and margins, but nothing in them says "put these next to each other". Inline-block can put boxes in a line of text, and then they sit on the text's baseline, with the gaps of the spaces between the tags, and they cannot share out the space that is left. Flexbox is the part of CSS made for laying out a row, or a column, of boxes.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    nav.css: the nav's links in a row

    In nav.css, make the list inside the header's nav (.site-nav ul) a flex container, so that its four links, Home, Venues, Shows and Contact, sit side by side in one row, in that order, with a gap of 8px between one link and the next, and the row centred in the header.

    The Check measures the links at 1000 and at 480 px wide: one row, 8 px between neighbours, and the middle of the row within 2 px of the middle of the list.

  2. Task 2

    show-card.css: the poster beside the details

    In show-card.css, lay out each show card (class show-card) as a row: the poster (class poster) on the left, always 120px wide; a gap of 20px; then the details (class details), taking all the rest of the card's width. The poster's top and the details' top are aligned, and the poster keeps its own height of 150px: it never stretches to the details' height. Keep the ready-made rule that puts 16px under each card.

    The Check measures the first card at 1000 and at 480 px wide (at 480 the details are taller than the poster).

  3. Task 3

    venues.css: the venue cards in rows that wrap

    In venues.css, lay out the twelve venue cards (class venue-card, in the list with class cards) in rows that wrap: each card at least 280px wide and growing to share its row, with a gap of 16px between cards across and down. At 1000 px wide that is three cards a row (four rows), at 700 px two a row, at 400 px one a row; every card in a row is the same width and the row is filled from the list's left edge to its right edge. The page never scrolls sideways.

  4. Task 4 · interview question

    Interview: sold-out.css, the label that will not centre

    Datta's stamp for Puneri Patya, the element with class stamp, holds a Sold out label (class badge sold-out). Fix sold-out.css so that the label's centre is at the stamp's centre, left to right and top to bottom (within 1 px), and the label stays a pill: only as wide and as tall as its words. Keep the stamp's height of 120px. Datta made the wrong element the flex container, and his notes have the two axes the wrong way round.

    Ira will ask: what is the main axis?

What you will learn

  • display: flex: a flex container, whose children become flex items in a row.
  • The main axis and the cross axis, and flex-direction, which turns the row into a column.
  • justify-content and align-items: where the items go along each axis; gap: the space between them.
  • flex-wrap: rows that wrap; flex and flex-basis: items that grow and shrink.
  • When flexbox: one row or one column of things.

More in CSS: the layout

Build "Flexbox" yourself

Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.