HTML and CSS · Chapter 8 of 12

The box model

By the end of this chapter you can say what every element is to the browser, a box of content, padding, border and margin, and measure one; give boxes space, borders, round corners and shadows; centre a column that is never too wide; choose between block, inline and inline-block; and explain why a box ends up wider than the width you gave it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: CSS: the look
  • 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

Ira's design for the home page is cards: twelve white venue cards with a hairline border and a soft shadow, on the cream, in a column that never grows wider than 960 pixels. In this chapter's page they are none of that. The venue names and areas sit straight on the cream, the text touches both edges of a laptop's window, and the "From ₹130" badge is a strip of colour no taller than its letters.

Sayali has a list of laptops and phones that venue owners use, and the page must look like Ira's drawing on all of them. Colours and fonts cannot do it: they change what is inside an element, not how much room it takes. And Datta has found a card that is wider than the page's column, although its width says 100%.

What you build: 4 tasks

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

  1. Task 1

    cards.css: the venue cards' boxes

    In cards.css, give the venue cards, the elements with class venue-card, Ira's card: padding 20px on every side, a border 1px solid #eadfce, a border-radius of 16px, a background-color of #ffffff and a box-shadow of 0 10px 30px -18px rgba(61, 10, 21, 0.45).

    The Check measures the first card at 1000 px wide: its name, the h3, must start 21 px in from the card's left edge and 21 px down from its top (1 px of border and 20 px of padding).

  2. Task 2

    page.css: the page's column

    In page.css, put the page's content, the <main> element, in Ira's column: at most 960px wide, centred in the window, with padding 24px at the top and bottom and 20px at the sides.

    The Check opens the page 1200 px wide: <main> must be 960 px wide and start 120 px from the left, and the heading Twelve venues, one website must start at 140 px. At 400 px wide, a phone, <main> must take the whole width, 400 px, starting at 0.

  3. Task 3

    pills.css: the badges as pills

    pills.css already gives the badges, the elements with class badge (From ₹130 on the show card and New on Kattaa Cafe Stage), Ira's colours and type from chapter 6: keep them (background-color #fff3d6, color #8a4b00, font-size 12px, font-weight 700). In the same rule, give them the box of a pill: display inline-block, padding 3px at the top and bottom and 10px at the sides, and a border-radius of 999px.

    The Check measures the From ₹130 badge: it must be display inline-block with that padding, and its line must be at least as tall as the badge.

  4. Task 4 · interview question

    Interview: hours.css, the card that is too wide

    Datta's Box office hours card, the element with class hours, should fill the page's column exactly, from its left edge to its right edge, like the grid of venue cards above it. It says width: 100% and is 34 px wider than the column, sticking out past its right edge. Fix hours.css so that the card is exactly as wide as the column, at 1000 and at 400 px wide, keeping Datta's padding of 16px on every side and his border of 1px solid #f0b94d.

    Ira will ask: what is the box model, and what does box-sizing change?

What you will learn

  • The box model: content, padding, border and margin, from the inside out.
  • width, max-width and margin: 0 auto: a column that is never too wide, centred.
  • box-sizing: whether a width counts the padding and border or only the content.
  • display: block, inline and inline-block, and what each does with padding.
  • Margins that collapse, units (px, rem, em, %), border-radius and box-shadow.

More in CSS: the look

Build The box model 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.