← Layout and components

Boxes

See every element as a box with padding, a border and a margin, and find out that width sets only the content.

Cards, buttons, menus and photos on every website are boxes, sized and spaced with the same few parts.

Every element on a page is a box, and CSS sets how much room each box has inside it and around it.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

page.html
<style>
  .card {
    width: 200px;
    padding: 20px;
    border: 5px solid navy;
  }
</style>
<div class="card">Owls: 12 points</div>

width sets how wide a box is, padding adds space inside its border, and border draws the line. Read the code. How wide is the whole card?

How wide is the card on the page, from the outside of its border on the left to the outside on the right, in pixels?

RunRun it and compare the result with your prediction.

Run

The Box model view is on: point at the card. Blue is the content, green the padding, gold the border, and the label shows the sizes. Compare the width with your prediction. Then change padding to 40px and point at it again, and add margin: 20px; to see the orange margin.

Code What it does Why
width: 200px; Sets how wide the content is It sets only the words' area. Padding and border are added outside it, on both sides, so the card is wider than 200
padding: 20px; Adds room inside the border, around the content It is part of the box, so the background colour fills it. 20 on each side adds 40
border: 5px solid navy; Draws a line around the padding Thickness, style and colour in one line. 5 on each side adds 10
margin: 20px; Adds space outside the border, between boxes It is outside the box, so it is not part of the card's width and has no background
20px with its px Every size needs a unit Without one, such as padding: 20;, the browser ignores the line and shows no error
page.html
<style>
  .card {
    width: 200px;
    padding: 20px;
    border: 5px solid navy;
  }
</style>
<div class="card">Owls: 12 points</div>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

The picture shows the first card in the Box model view: orange margin, gold border, green padding, blue content.

page.html
<style>
  .card {
    width: 180px;
    padding: 15px;
    border: 4px solid teal;
    margin: 16px;
    background-color: lightyellow;
  }
</style>
<div class="card">Owls: 12 points</div>
<div class="card">Hawks: 9 points</div>
The page this code makes, with the Box model view on
What this code makes (Box model view: margin orange, border gold, padding green, content blue)

The first card's rule is width: 180px; padding: 15px; border: 4px solid teal; margin: 16px;.

The words are squashed against the border.

The page this code makes, with the Box model view on
What this code makes (Box model view: margin orange, border gold, padding green, content blue)

This card should have room inside its border.

In the picture, how wide is the first card on the page, outside border to outside border, in pixels?

The words touch the border of a card. Which property gives them room inside the border?

The border shows, but the words are squashed against it: the padding does nothing, and there is no error. Click the line that causes it.

ModifyChange the code on purpose and check the result.

Modify

Give the cards 12px of padding.

Need a hint?

Add padding: 12px; inside the .card rule.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Space the cards apart with 16px of margin.

Need a hint?

Margin is outside the border: margin: 16px;

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Make each card's content 200px wide. How wide is each card now? Point at it to check.

Need a hint?

width: 200px; Remember that padding and border are added to it.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.

Give the cards 12px of padding.

    This ticks when your page has all of the above.

    Space the cards apart with 16px of margin.

      This ticks when your page has all of the above.

      Make each card's content 200px wide.

        This ticks when your page has all of the above.

        InvestigateTrace how each line of code connects to what you see.

        Mix it up

        Both tags are span elements in one line of text.

        page.html
        <style>
          .tag {
            width: 120px;
            padding: 8px;
            border: 2px solid teal;
          }
        </style>
        <p>Today: <span class="tag">Owls</span> <span class="tag">Hawks</span></p>
        
        Both tags should be boxes 120px wide, but each is only as wide as its word. The padding works, the width does not. What fixes the width?

        MakeBuild something new using what you have learned. Expect a few tries; that is normal.

        Make

        Make a card for a house or a club: a <div> with a heading, a picture and a line of text, styled with padding, a border, margin and a width. Point at it with Box model and work out its width before you check.

        Try it yourselfYour edits are saved in this browser.
        Need a hint?

        A card is one box that holds the heading, the picture and the line of text. Build the box first, then style it.

        Another hint

        Start with a <div class="card"> holding an <h2>, an <img> and a <p>.

        Another hint

        Then a <style> block with a .card rule: padding, border, margin and width.

        Show the expected result
        What the finished page looks like
        What it should look like. Your words can be different.

        Make a card with a heading and a picture, styled with padding, a border and a width.

          This ticks when your page has all of the above.

          For teachers

          Students see every element as a box of content, padding, border and margin, and find out that width sets only the content, so padding and border make the box wider. About 45 minutes.

          Open the teacher guide: answers, solutions, slides and a worksheet.

          Students will be able to

          Before class

          Read the unit primer "CSS in 5 minutes" (the box paragraph), then make the Make task's card yourself and point at it with Box model on.

          The Box model view (a button in the HTML playground) works like the browser's developer tools: point at any box to see its margin (orange), border (gold), padding (green) and content (blue), with sizes. It never changes the page. The Boxes button is different: it adds space to show nesting, so use Box model in this lesson.

          The one idea: width sets the content only. The box on the page is the width plus the padding and border on both sides. Real sites often set box-sizing: border-box so that width includes padding and border; mention it once, after students are comfortable with the sums.

          Running the lesson

          1. Predict: almost everyone says 200. The card is 250 wide: padding and border are added to the width on both sides. Let students check with Box model. Most will say 200. Ask what the 20 and the 5 in the rule are for, then let them point at the card with Box model.
          2. Run: change the padding and point again. Ask: which number in the label changed, and which part of the card grew?
          3. Investigate: the number task is the sum on paper (180 + 30 + 8). The wrong answers are the real slips: content only, one side only, margin included. The last task is `padding: 20` with no unit; the browser ignores it without an error.
          4. Mix it up: padding works on an inline span but width does not. Ask who remembers what block and inline meant.
          5. Modify: build the card one property at a time, checking each with Box model, and work out the width before pointing.

          The hard part (on purpose)

          Working out that padding and border add to the width. Ask for the sum on paper first (200 + 20 + 20 + 5 + 5), then check it with Box model.

          When students get stuck

          Stretch and support

          Stretch: Use different padding on each side (padding: 10px 30px;) and predict the width before pointing at it. Then add border-radius: 12px; for rounded corners.

          Support: Give students the Modify starter with Box model on and ask them only to add padding, then margin, pointing at the card after each.

          Where it leads

          Next lesson is a review of the unit on a whole newsletter page: reading a longer stylesheet and turning the articles into cards. Class idea: measure a real website's buttons with the browser's developer tools (right-click, Inspect).

          What each step is for

          Now you can
          Next lesson5. Turn the newsletter into cardsRead a longer stylesheet, find the rule that does a job, and change just that rule.