Web Design & Development · CSS

WDD9 - CSS: height and width

Mon 30 Nov 2026 · P1/P2 (double)
Content scoped for a single period · lands on a double, extra time available
Learning intentions
Success criteria
Warm up - margins and padding recap

WDD9 combines width/height with the box model and margin rules from WDD7 and WDD8.

WU1
Box A has margin-bottom: 22px. Box B sits directly below it in normal flow with margin-top: 14px. What is the visible gap between them in pixels? Write the number only.
 
WU2
In margin: 10px 20px; (a 2-value shorthand), what does the second value set?
  • Correct. In a 2-value shorthand, the second value sets left and right together.
  • Top and bottom take the first value, 10px.
  • This is a 2-value form, so no single side is set alone.
  • A 2-value shorthand sets two distinct amounts, not one shared amount.
WU3
Which property must be given a fixed width narrower than its container before margin: auto can centre a block element?
  • Display alone does not give auto margins space to work with.
  • Float removes an element from normal flow - it is not needed for centring with auto margins.
  • Correct. Without an explicit width, the element fills all available space and there is nothing left for auto to distribute.
  • Clear controls position relative to floats, unrelated to centring.

Key vocabulary

width
The property that sets the size of an element's content box along the horizontal axis.
height
The property that sets the size of an element's content box along the vertical axis.
Percentage sizing
A width or height given as a percentage of the parent element's own size, rather than a fixed pixel value.
Intrinsic size
An element's natural size based on its own content, used when no explicit width or height is set.
Overflow
What happens to content that does not fit inside its box; by default it spills out visibly rather than being hidden or resized.
Fixed sizing
Setting width or height to an exact pixel value that does not change with the parent or the content.

Setting width and height

Default sizing before anything is set

Before any width or height is set, a block-level element behaves in a specific default way: its width stretches to fill all of the available width inside its parent, while its height shrinks to fit whatever content is inside it - just tall enough, no more. This is sometimes called the element's intrinsic size. Setting width or height explicitly overrides this default behaviour, fixing that dimension to the value given regardless of how much content is inside, or how much space the parent has available.

Width and height still only set the content box

As with the box model from WDD7 and WDD8, width and height set the size of the content layer only. Padding and border are still added outward from that, so a box declared as width: 300px; with padding: 20px; and a 5px border on every side has a visible width of 300 + 40 (padding both sides) + 10 (border both sides) = 350px, exactly as WDD7 and WDD8 already established for the box model in general.

Percentage width and height

Percentage width: calculated from the parent's width

A percentage width is calculated as a percentage of the parent element's own width. If a parent is 800px wide and a child has width: 50%;, the child renders at 400px wide - straightforward, because the parent always has a defined width one way or another (its own explicit width, or the viewport width if nothing else is set).

Percentage height: needs the parent to have an explicit height

Percentage height works the same way in principle - a percentage of the parent's height - but has a much more common failure case. A parent's height is very often left at its default, auto, meaning it shrinks to fit its own content rather than having any fixed height of its own. If the parent's height is auto, there is no fixed reference height for a percentage child height to be calculated from, so a percentage height on the child typically has no effect - it behaves as if no height had been set on the child at all, rather than producing any specific number. This only starts working correctly once the parent is given an explicit height of its own, such as a fixed pixel value.

Same child rule, different parent height .child { height: 50%; }
Parent height: auto No effect
Parent height: 400px 200px

Overflow: when content doesn't fit

The default is visible, not hidden or resized

Fixing a box's height (or width) creates a new question: what happens if the content inside genuinely needs more space than the box provides? By default, the browser does not shrink the content to fit, and does not automatically grow the box to accommodate it. Instead, the content simply overflows - it spills outside the edges of the box, visibly, often overlapping whatever comes after it on the page. This default behaviour is controlled by the overflow property, whose default value is visible. WDD7 already introduced overflow: hidden; as a fix for a different problem (a collapsing floated container); here the same property is relevant again because fixed-height text containers are a common real-world source of overflowing content, especially once content length is variable - user-submitted text, or a translated version of a page with longer words than the original.

Worked examples

Example 1 - Visible size of a fixed width/height box
.thumb {
  width: 300px;
  height: 100px;
  padding: 20px;
  border: 5px solid #185FA5;
}
1
Content size is exactly 300px wide and 100px tall - this is what width/height set directly.
2
Add padding on both sides for each dimension: 20 + 20 = 40px added to both width and height.
3
Add border on both sides for each dimension: 5 + 5 = 10px added to both width and height.
Visible size: 350px wide (300+40+10) by 150px tall (100+40+10).
Example 2 - Percentage width relative to the parent
.parent { width: 800px; }
.child { width: 50%; }
1
The parent's width is fixed at 800px.
2
The child's 50% width is calculated relative to that: 50% of 800px.
Child renders at 400px wide.
Example 3 - Percentage height needs an explicit parent height
.parent has no height set
.child { height: 50%; }

Has no effect. The parent's height is auto, so there is no fixed reference for 50% to be calculated from.

.parent { height: 400px; }
.child { height: 50%; }

Works. The child renders at 200px tall - 50% of the parent's fixed 400px height.

Percentage height only becomes predictable once the parent's own height stops being auto.
Now you try

A parent element has an explicit height of 600px. Its child has height: 25%. What is the child's rendered height in pixels?

The parent has an explicit height (600px), so the percentage has a fixed reference to calculate from.
25% of 600px = 150px.

Child height: 150px.

Practical: percentage height and overflow, WebStorm

Build this from scratch - no starter file this time.

  1. In WebStorm, create a new HTML file called sizing.html.
  2. Add a <div class="parent"> containing a <div class="child"> with some short text inside.
  3. In an internal stylesheet, give .child height: 50%; and a background colour, but leave .parent with no height set. Open in a browser - the child's height should look like it has had no effect.
  4. Now give .parent an explicit height: 400px;. Save and refresh - the child should now visibly become exactly half that height.
  5. Finally, give .child a fixed height: 30px; and paste in several sentences of text. Observe the text overflowing the box by default, then add overflow: hidden; to .child and observe the difference.
<div class="parent">
  <div class="child">Some sample text</div>
</div>
.parent {
  height: 400px;
  border: 2px dashed #999;
}

.child {
  height: 50%;
  background: #E6F1FB;
}

Compare your own file against this rather than copying it - the key thing to observe is the child's rendered height changing only once .parent has an explicit height of its own.

Common mistakes
Exam tip

If a question asks what happens when content is too big for a fixed-size box, the precise term is overflow, and the correct default behaviour is that it spills outside the box (overflow: visible) - not that it gets clipped, and not that the box grows automatically. For percentage height questions, always check whether the parent's height is stated explicitly in the question - if it is auto or unstated, that is usually the point being tested.

Task Set A - Core questions

Task Set A - Core questions
Complete all questions. Written and code answers reveal a model answer for self-assessment.
A1
A box has content width 300px, padding 20px on left and right, and a 5px border on left and right. What is its visible width in pixels, not counting margin? Write the number only.
 
A2
A parent element is 800px wide. Its child has width: 50%. What is the child's rendered width in pixels? Write the number only.
 
A3
By default, what happens when content is taller than a box with a fixed height?
  • Clipping only happens if overflow: hidden is set - it is not the default.
  • Correct. The default overflow value is visible.
  • A fixed height does not grow automatically to fit content.
  • This is normal rendering behaviour, not an error.
A4
A child element has height: 50%. Its parent has no height set, so the parent's own height is determined by its content (auto). What happens to the child's height?
  • Percentage height is relative to the parent, not the viewport.
  • Correct.
  • Percentage height is relative to the parent, not the element's own content.
  • The height does not collapse to zero - it behaves as if unset.
A5
A parent element has an explicit height of 400px. Its child has height: 50%. What is the child's rendered height in pixels? Write the number only.
 
A6
Which CSS property controls what happens to content that does not fit inside a fixed-size box?
 
A7
What is a block-level element's default width behaviour before any width is explicitly set?
  • That describes the default height behaviour, not width.
  • Correct.
  • There is no fixed default pixel width.
  • Width and height are calculated independently of each other.
A8
A pupil sets height: 50% on a child element expecting it to become half the size of its container, but nothing visibly changes. Explain why, and what needs to be true about the parent for it to work.
Model answer
A9
A pupil wants exactly 200px by 200px thumbnail boxes including padding, but this CSS produces boxes that are actually 220px by 220px. Find the bug and write the corrected CSS so the visible box (including padding) is exactly 200x200.
.thumb {
  width: 200px;
  height: 200px;
  padding: 10px;
}
.thumb {
  width: 180px;
  height: 180px;
  padding: 10px;
}

width and height set the content box only - the original 200px content size plus 10px padding on each side added up to 220px visible. Reducing the content width/height to 180px means 180 + 10 + 10 = 200px visible in each dimension.

Task Set B - Extension

Task Set B - Extension
Beyond the core specification - written answers are self-assessed against a model answer.
B1
A container has height: auto and contains three children, each with height: 50%. Explain what happens to each child's height, and why.
Model answer
B2
A pupil sets a fixed height on a paragraph, and later the site is translated into a language that produces longer words. Explain what problem this could cause, using the concept of overflow.
Model answer
B3
Give one situation where setting an explicit height is a risky design choice, and one situation where it is a reasonable, safe choice.
Model answer

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

Teacher notes - Shift+T to toggle

Timing: content is scoped for a single period but this lesson lands on a double (Mon 30 Nov 2026, per WDD.md §2a/§4 table) - the same "extra time" mismatch as WDD6 and WDD8, not a shortfall. Core pace through Task Set A: 6 min warm up, 10 min sizing notes + Example 1, 12 min percentage width/height notes + context box + Examples 2/3, 6 min overflow notes, remaining time on Task Set A. Use the spare second period for the full WebStorm practical in class, Task Set B, and extra retrieval practice on WDD7/WDD8 box model content ahead of WDD10.

Scope control: deliberately does not cover box-sizing: border-box (an alternative box model where width/height include padding and border) - the SQA spec and Chris's own L8 both work with the default content-box model throughout, so introducing border-box here would contradict the box model as already taught in WDD7/WDD8. Also does not cover vh/vw viewport units or min-width/max-width/min-height/max-height, which are not part of the confirmed spec list for this lesson.

Percentage height is the highest-value gotcha in this lesson, matching margin collapse's role in WDD8 - it is the one behaviour pupils are most likely to get wrong from intuition alone. A4/A5/A8/B1 test it from four angles: conceptual MC, numeric calculation, written explanation, and a multi-child extension case.

Practical built from scratch (no starter file), continuing the WDD6(starter)/WDD7(scratch)/WDD8(starter)/WDD9(scratch) alternation.