← Teacher guide

Block and inline boxes

Students find out that block elements start a new line and take the full width while inline ones sit in the line, that div and span are plain boxes for grouping, and that display switches between block and inline.

Layout and components · Boxes · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 8 min · Modify 8 min · Mix it up 5 min · Make 9 min

Before class

Read the unit primer "Layout and components" first, then do the Make task yourself in the playground (about 5 minutes).

A block box starts on a new line and takes the full width of its container. An inline box sits inside a line of text and is only as wide as its content. <div> (block) and <span> (inline) are plain boxes: they have no look until a rule styles them. display: block; and display: inline; swap the kind. This lesson uses background colour to show the width, because box properties such as padding and borders come next lesson.

Running the lesson

  1. Predict and Run: most students say the gold hugs the words in both places. The full-width band behind the div is the surprise. Turn on Boxes to show each box. Ask someone who chose the words in both places what the page would look like if the div were only as wide as its words. The idea to land: block means the full width.
  2. Investigate: the first question is the misconception, that a div changes the page by itself. Ask what a rule would have to say to see the div.
  3. Modify: labels are inline, so they run on beside their boxes. display: block puts each on its own line.
  4. Mix it up: the class and the group rule both set the colour. The class wins wherever it is written, and the size comes only from the group rule.
  5. Make: the check ticks on a span, a class, a rule for it and a background colour.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Believing a div adds a look. Ask what the div would have to be told before anything on the page looked different.

When students get stuck

What you will seeA question to askThe fix
The highlight runs across the whole page.Is the element a span or a div?Use a span for a few words in a line. A div is a block and takes the full width.
Nothing changes when the div is added.Is there a rule that names the div or its class?A div has no look of its own. Give it a class and write a rule for the class.
The span does not stack when display block is added.Is the display line inside the rule for the span's class?Put display: block; between the braces of the class rule, with a semicolon.

Answers to the checks

  1. Predict: Look at the gold background. How wide is it behind 9 am, and behind Bring a hat and water.?
    Only the words behind 9 am, and the full width behind the hat line. A span sits inside the line of text. A div is a block: it starts on a new line and takes the full width.
  2. A div with no rule: A student wraps two paragraphs in a <div> and writes no CSS. What changes on the page?
    Nothing you can see: a div has no look of its own
    A div is a plain box. It changes nothing until a rule styles it. Its use is to group elements so that one rule, or one class, can style them together.
  3. Block or inline?: Which three tags sit inside a line of text, instead of starting a new line?
    <strong>, <a> and <span>
    Inline: strong, em, a, img and span. Block: p, h1 to h6, ul, ol, table and div. A block starts on a new line and takes the full width.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Labels on their own lines

<style>
  label {
    color: navy;
    display: block;
  }
</style>
<form>
  <label for="name">Name</label>
  <input id="name">
  <label for="year">Year</label>
  <input id="year">
</form>
<p>Bring a hat.</p>
<p>Bring water.</p>

Modify › Group the paragraphs

<style>
  label {
    color: navy;
    display: block;
  }
  .packing {
    background-color: lightyellow;
  }
</style>
<form>
  <label for="name">Name</label>
  <input id="name">
  <label for="year">Year</label>
  <input id="year">
</form>
<div class="packing">
  <p>Bring a hat.</p>
  <p>Bring water.</p>
</div>

Make

<style>
  .highlight {
    background-color: lightyellow;
  }
</style>
<h1>Spring concert</h1>
<p>The concert is on <span class="highlight">Wednesday at 6 pm</span> in the hall.</p>
<p>Tickets are free. Bring a friend!</p>

Stretch and support

Stretch: Make the three links in a nav stack on their own lines with display: block, then make three paragraphs sit in a line with display: inline. Which is easier to read?

Support: Give students the Run page and ask them only to swap span and div and say what moved.

Exit ticket

Why does a gold background on a span reach only its words, and on a div the whole width?

A span is inline, so it is only as wide as its words. A div is a block, so it takes the full width of the page.

What each step is for

Where it leads

Next lesson gives each box padding, a border and a margin, and works out how wide the box is. Class idea: open a news site and find three block boxes and three inline ones.