Web Design & Development · CSS

WDD7 - CSS: box model, display, float and clear

Tue 24 Nov 2026 · P2 (single)
Content scoped for a double period · see teacher notes for what to compress
Learning intentions
Success criteria
Warm up - CSS selectors and cascade recap

WDD7 builds directly on WDD6's selectors - you will use both again today.

WU1
Write a descendant selector that targets links inside nav.
 
WU2
Which CSS placement is easiest to reuse consistently across every page of a multi-page site?
  • Inline CSS has to be repeated on every element.
  • Internal CSS has to be repeated in every page.
  • Correct. One linked stylesheet can style every page.
  • JavaScript is not a CSS placement.
WU3
Two equally specific rules come from the same external stylesheet and both set a paragraph's colour. Which one wins?
  • The browser reads rules in order, so the first is overridden.
  • Correct. Later, equally specific rules override earlier ones.
  • The cascade is not random.
  • Only one declaration can set a given property on a given element.

Key vocabulary

Box model
The idea that every HTML element is a rectangular box made of content, padding, border and margin.
Content
The innermost layer: the actual text, image or other content, sized by width and height.
Padding
Space between the content and the border, inside the box, sharing the box's background.
Border
A visible line drawn around the padding, with its own width, style and colour.
Margin
Transparent space outside the border, used to create space between one box and its neighbours.
Display
The property that controls how an element behaves in the layout: block, inline, inline-block or none.
Float
A property that pulls an element to the left or right and removes it from normal flow, letting other content sit alongside it.
Clear
A property that stops an element sitting beside a previous floated element, forcing it below instead.
Normal flow
The default top-to-bottom, left-to-right order the browser lays out HTML elements in, before float removes anything from it.

The box model

Four layers, from the inside out

Every element on a web page - a paragraph, a heading, a div, an image - is rendered as a rectangular box. That box is built from four layers, from the inside out: content (the text or image itself, sized by width and height), padding (space between the content and the border, which shares the box's background colour), border (a visible line with its own width, style and colour), and margin (transparent space outside the border that creates a gap between this box and whatever sits next to it). This is the box model, and it applies to every element you style.

Unless a page changes the default behaviour, width and height set the size of the content layer only. Padding and border are added outward from that content size, so a box's actual visible size on the page - what you would measure with a ruler on the screen, background colour and all - is content width plus left and right padding plus left and right border. Margin sits outside that visible box and is never part of it: it has no background, and increasing it never makes the box itself any bigger, only the gap around it.

Why this matters for calculations

Higher exam questions on the box model usually give you a content size plus padding and border values, and ask for the box's rendered size. Work outward one layer at a time rather than guessing: start from the content size, add padding on both sides, then add border on both sides. If margin is asked for separately, add it last, and be clear in your answer about whether you are describing the box itself or the total space it occupies including its margin - they are different numbers and exam mark schemes distinguish between them.

Display: block, inline, inline-block and none

Four behaviours

The display property decides how an element behaves in the page layout. A block element (like a div, p or h1 by default) always starts on a new line and stretches to fill the available width, whether or not its content needs that much space. An inline element (like a or span by default) sits within a line of text, only takes up as much width as its content needs, and ignores any width or height you try to give it. inline-block is a middle ground: the element sits inline with its neighbours like an inline element, but still accepts width, height, and vertical padding and margin like a block element does. Setting display: none removes the element from the page entirely - it is not just invisible, it takes up no space at all, as if it were never in the HTML.

block

New line, full width available, accepts width/height normally.

inline

Sits in the text flow, width/height are ignored.

inline-block

Sits in the text flow, but width/height still work.

none

Removed from the page completely - takes up zero space.

Float

Pulling an element out of normal flow

By default, elements are laid out in normal flow: block elements stack top to bottom, and inline elements sit left to right within a line. float: left or float: right changes this for one element: it pulls that element as far as it can to the left or right of its container, and takes it out of normal flow. Everything that comes after a floated element in the HTML no longer treats it as a normal block in the way - other content moves up to sit alongside it, wrapping around it, instead of being pushed below it the way it would be for a normal block element.

This is exactly how a classic "text wraps around an image" layout works: float the image left, and the paragraph text that follows in the HTML flows up the right-hand side of it instead of starting underneath. Floating several block-level boxes the same direction, one after another in the HTML, is the classic (pre-modern-layout) way of getting boxes to sit in a horizontal row rather than stacking vertically.

Clear, and the collapsing container problem

What clear actually does

clear is applied to an element that comes after a floated element, not to the floated element itself and not to its parent container. clear: left stops that element sitting beside anything floated left; clear: right does the same for float right; clear: both covers either direction. A cleared element drops below the floated element instead of wrapping up next to it.

A separate problem: the collapsing container

Because a floated element is removed from normal flow, it stops contributing to its parent's height calculation. If a container's only children are floated, the container can collapse to almost zero height - its border and background shrink to a thin line, even though the floated boxes inside it are still clearly visible. This surprises pupils because it looks like a bug, but it is the box model behaving exactly as designed: the parent genuinely has no non-floated content to measure a height from. Applying clear to the parent itself does nothing here, because clear only affects an element's position relative to floats that came before it - it is not a tool for making a parent contain its floated children. The straightforward Higher-level fix is to give the collapsing parent overflow: hidden; (or overflow: auto;), which has the side effect of forcing it to expand around its floated content again.

Step 1
Float the boxes.box { float: left; }Three boxes now sit side by side.
↓ But the boxes are gone from normal flow
Step 2
Container collapses.gallery has no unfloated childrenIts height shrinks to almost 0px.
↓ Give the container overflow: hidden
Step 3
Container fixed.gallery { overflow: hidden; }Border now hugs the boxes ✓
Same HTML, same three floated boxes - different container CSS .gallery { float children }
Without overflow: hidden ≈0px tall
With overflow: hidden Correct height

Worked examples

Example 1 - Calculating a box's visible width
.card {
  width: 150px;
  padding: 10px;
  border: 5px solid #185FA5;
}
1
Content width is 150px - this is what width sets by default.
2
Add padding on both sides: 10px left + 10px right = 20px. Running total: 150 + 20 = 170px.
3
Add border on both sides: 5px left + 5px right = 10px. Running total: 170 + 10 = 180px.
Visible box width: 180px (margin is not included, because margin sits outside the visible box).
Example 2 - Choosing the right display value
display: inline (default for a)
width: 200px;

Ignored. The link still only takes up as much width as its text.

display: inline-block
width: 200px;

Works. The link now sits inline but reserves a fixed 200px width.

If a design needs a fixed-size clickable box that still sits in a row with others, inline-block is the display value that makes width and height actually take effect.
Example 3 - Floating an image so text wraps around it
img {
  float: left;
  margin-right: 12px;
}
1
The image is pulled to the left edge of its container and removed from normal flow.
2
The paragraph that follows the image in the HTML is a normal block element, so it does not know the image has been floated - it flows up the right-hand side of it.
margin-right stops the text touching the image directly, giving a small gap between them.

Practical: live box model and float editor

Edit the CSS on the left and watch the preview on the right update instantly. Start by changing float: left to float: none on .box to see the boxes return to normal flow, then put it back and try deleting overflow: hidden; from .gallery to watch the container collapse.

Your CSS
Delete overflow: hidden; to reproduce the collapsing-container problem from the context box above.
Live preview
Now you try

An element has content width 80px, padding 15px on all sides, and a 2px border on all sides. No margin is given. Calculate its visible box width.

Content: 80px
+ padding both sides: 15 + 15 = 30px → running total 110px
+ border both sides: 2 + 2 = 4px → running total 114px

Visible box width: 114px

Common mistakes
Exam tip

The SQA wording names box model, display, float and clear individually - use those exact terms, not vaguer ones like "positioning." For box model calculations, show your working one layer at a time (content, then padding, then border) rather than jumping straight to a final number: method marks are often available even if the final total is wrong.

Task Set A - Core questions

Task Set A - Core questions
Complete all questions. Written and practical answers reveal a model answer for self-assessment.
A1
Which layer of the box model sits directly between the content and the border?
 
A2
An element has content width 150px, 10px padding on all sides, and a 5px border on all sides. What is its visible box width in pixels, not counting margin? Write the number only.
 
A3
Which value of the display property removes an element from the page entirely, so it takes up no space at all?
  • Block elements still take up space - a full line's width.
  • Inline elements still take up the space their content needs.
  • This still takes up space, and accepts width/height.
  • Correct. display: none removes the element completely.
A4
Which display value lets an element sit inline with surrounding content while still accepting a width and height?
 
A5
What happens to a floated element with respect to normal document flow?
  • It is removed from normal flow entirely, not just shifted.
  • Correct.
  • Floated elements remain fully visible.
  • Float does not affect whether width/height apply.
A6
Which value of clear guarantees an element drops below floated elements on both the left and the right?
 
A7
Explain why a container that only contains floated children can collapse to almost zero height, and describe one way to fix this.
Model answer
A8
A pupil floats three boxes left inside a container, then sets clear: both; directly on the container itself, expecting this to fix its collapsed height. What is wrong with this approach?
  • clear: both works with either float direction.
  • Correct. This needs a different fix, such as overflow: hidden on the container.
  • clear can be applied to any element.
  • It has no effect here.
A9
This CSS is meant to stop a paragraph sitting beside a floated image and make it drop below instead - but the paragraph still wraps around the image. Find the bug and write the corrected CSS.
img {
  float: left;
}

p {
  float: left;
}
img {
  float: left;
}

p {
  clear: left;
}

Floating the paragraph too just adds it to the same row of floated elements - it does not stop it wrapping. To make the paragraph drop below the image, it needs clear: left;, not another float.

Task Set B - Extension

Task Set B - Extension
This lesson is scoped for a double period but lands on a single - treat B3 as follow-up work if class time runs out.
B1
A pupil floats two boxes left inside a container, then adds a third box after them in the HTML without floating it and without giving it clear. What happens to the third box, and why?
One possible answer
B2
A pupil wants three buttons to sit in a horizontal row without using float. Which display value could achieve this, and what is one limitation of it compared to float for building a full navigation bar?
One possible answer
B3 - Practical (WebStorm, from scratch)
Build a three-box floated gallery from a blank file - no starter file this time, just the spec below.
  1. In WebStorm, create a new HTML file called box-gallery.html.
  2. Add a <div class="gallery"> containing three <div class="box"> elements, each with some short text inside.
  3. In an internal stylesheet, give .box a fixed width, some padding, and a border, then float each one left so they sit in a row.
  4. Fix the container collapse this causes by giving .gallery overflow: hidden;, a border and some padding.
  5. Save, open the file in a browser, and check the three boxes sit in a row with the gallery's border hugging around them - not collapsed to a thin line.
<div class="gallery">
  <div class="box">Box 1</div>
  <div class="box">Box 2</div>
  <div class="box">Box 3</div>
</div>
.gallery {
  overflow: hidden;
  border: 2px dashed #999;
  padding: 10px;
}

.box {
  float: left;
  width: 120px;
  padding: 10px;
  margin: 10px;
  border: 3px solid #185FA5;
  background: #E6F1FB;
}

Compare your own file against this rather than copying it - the exact width/padding/margin numbers do not matter, but the float on .box and the overflow: hidden on .gallery are the two properties doing the real work.

File this in OneNote under:
Higher Computing Science > Web Design & Development > WDD7

Teacher notes - Shift+T to toggle

Timing risk: this lesson is scoped for a double period but is scheduled on a single (Tue 24 Nov 2026, see WDD.md §2a/§4 - one of four flagged "too short" mismatches, along with WDD4, WDD10 and WDD16). Do not try to cover everything in class.

Suggested compressed timing (single period, ~50 min): 5 min warm up, 8 min box model + Example 1, 6 min display + Example 2, 8 min float + Example 3, 10 min clear/collapse (flow-pipeline + context box + live editor), remaining time on Task Set A questions A1-A6 only. Push A7-A9 and all of Task Set B, including the B3 WebStorm practical, to independent/homework time.

Scope control: this lesson deliberately does not go deep on exact margin/padding values (that is WDD8, including margin collapse) or on width/height as their own topic (that is WDD9). Keep worked examples focused on box model layer order, display choice, and the float/clear/collapse relationship only.

B3 is the second WebStorm practical on this site (after WDD6's B4), and the first one built genuinely from scratch rather than from a downloaded starter file, per the instruction to vary the entry point across lessons rather than defaulting to one shape every time.