HTML and CSS · Chapter 10 of 12

Grid

By the end of this chapter you can lay out boxes in rows and columns that line up with each other: give a grid its columns in pixels and in shares of the room (fr), repeat them, let the browser decide how many fit, make one item span two columns, and draw a whole page as named areas. And you can say when a grid is the right tool and when flexbox is. 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

Chapter 9's venue cards wrap three to a row, and each row is a row of its own: when a card's name takes two lines, nothing lines up with the row below. Ira's new design for the home page is a real table of cards, four columns on a laptop, two on a phone, every card in its column. The launch week's six show cards go in as many columns as fit. And the show page is drawn as a plan, the poster down the left, the details and the showtimes beside it, the prices across the bottom, which no single row or column can make.

Datta tried a grid for the box office page and got two of the classic results: a card alone on a second row, and three cards that run off the page. Both come from one property, the list of columns.

What you build: 4 tasks

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

  1. Task 1

    venues.css: the venue cards in a grid

    In venues.css, replace chapter 9's flex rules with a grid for the twelve venue cards (class venue-card, in the list with class cards): equal columns, each at least 200px wide and sharing the room that is left, with a gap of 16px across and down. At 1100 px and at 1000 px wide that is four cards a row (three rows); at 480 px wide, two a row (six rows). The columns fill the list from its left edge to its right edge, and no column is narrower than 200 px.

  2. Task 2

    shows.css: the show cards, as many a row as fit

    In shows.css, replace the ready-made flex column with a grid for the six show cards in the element with class shows: columns each at least 300px wide, as many as fit, growing to share the row, with a gap of 16px across and down. At 1000 px wide that is three a row (two rows), at 700 px two a row, at 400 px one a row, and the cards always fill the row from edge to edge. Keep the ready-made rule that puts 20px under the cards. The page must not scroll sideways.

  3. Task 3

    show.css: the show page, with named areas

    In show.css, lay out show.html's article (class show-page) as a grid of named areas, replacing the ready-made flex column: two columns, the first 1 share wide and the second 2 shares, with a gap of 16px, and three rows drawn as: poster details, then poster times, then aside aside. The poster (class poster) fills the first column of the first two rows; the details (class details) and the showtimes (class times) sit to its right, one above the other; the aside (class aside) spans both columns at the bottom.

    The Check measures show.html at 1000 px wide: the poster is a third of the room less the gap, the details' top is the poster's top, the poster ends where the showtimes end, and the aside runs from the grid's left edge to its right edge.

  4. Task 4 · interview question

    Interview: box-office.css, the grids that go wrong

    box-office.html (Box office in the nav) has two grids in Datta's box-office.css. Fix them so that at 1000 px wide each one shows its three cards side by side in one row, the same width, filling the page's column from its left edge to its right edge, with a gap of 16px: the box office hours (class hours), where the third card now sits alone on a second row, and the seat prices (class prices), whose cards now run past the column's right edge so that the page scrolls sideways. At 480 px wide no card may stick out of the column, and the page must not scroll sideways at either width.

    Ira will ask: grid or flexbox, and why?

What you will learn

  • display: grid and grid-template-columns: a container with columns, and its children placed in its cells, row by row.
  • fr, repeat() and minmax(): shares of the room, columns written once, and a smallest and a largest size.
  • auto-fill and auto-fit: as many columns as fit, empty ones kept or folded away.
  • grid-column: span 2 and grid-template-areas: an item across two columns, and a layout drawn with names.
  • Grid or flexbox: rows and columns together, or one line at a time.

More in CSS: the layout

Build "Grid" 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.