WDD7 - CSS: box model, display, float and clear
- I can describe the four layers of the CSS box model: content, padding, border and margin.
- I can use the display property to control whether an element behaves as block, inline, inline-block or is removed entirely.
- I can use float to position elements side by side, and clear to control what happens next to them.
- I can calculate a box's visible width or height by adding content, padding and border.
- I can choose the correct display value for a given layout situation.
- I can explain why a container collapses when all of its children are floated, and fix it.
WDD7 builds directly on WDD6's selectors - you will use both again today.
nav.Key vocabulary
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.
New line, full width available, accepts width/height normally.
Sits in the text flow, width/height are ignored.
Sits in the text flow, but width/height still work.
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.
.box { float: left; }Three boxes now sit side by side..gallery has no unfloated childrenIts height shrinks to almost 0px..gallery { overflow: hidden; }Border now hugs the boxes ✓.gallery { float children }
Worked examples
.card {
width: 150px;
padding: 10px;
border: 5px solid #185FA5;
}
150px - this is what width sets by default.20px. Running total: 150 + 20 = 170px.10px. Running total: 170 + 10 = 180px.width: 200px;
Ignored. The link still only takes up as much width as its text.
width: 200px;
Works. The link now sits inline but reserves a fixed 200px width.
inline-block is the display value that makes width and height actually take effect.img {
float: left;
margin-right: 12px;
}
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.
overflow: hidden; to reproduce the collapsing-container problem from the context box above.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
- Treating margin as part of the visible box. Margin is transparent and outside the border - it never has a background colour and is not included when a question asks for a box's rendered size, only for the total space it occupies.
- Setting width or height on an inline element. Both are ignored on default inline elements such as
aorspan. Useinline-blockorblockif the size needs to take effect. - Assuming a floated element still holds its old space. Once floated, an element leaves normal flow completely - later content moves up to fill the space it would otherwise have taken.
- Applying clear to the container holding the floated children. Clear only affects an element's position relative to floats that came before it in the HTML - it does not make a parent contain its floated children. That needs a different fix, such as
overflow: hidden;on the parent.
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
clear: both; directly on the container itself, expecting this to fix its collapsed height. What is wrong with this approach?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
- In WebStorm, create a new HTML file called
box-gallery.html. - Add a
<div class="gallery">containing three<div class="box">elements, each with some short text inside. - In an internal stylesheet, give
.boxa fixed width, some padding, and a border, then float each one left so they sit in a row. - Fix the container collapse this causes by giving
.galleryoverflow: hidden;, a border and some padding. - 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
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.