Web Design & Development · CSS

WDD8 - CSS: margins and padding

Thu 26 Nov 2026 · P2/P3 (double)
Content scoped for a single period · lands on a double, extra time available
Learning intentions
Success criteria
Warm up - box model, display and float recap

WDD8 builds directly on WDD7's box model - today adds the detail WDD7 left out.

WU1
A box has content width 100px and 20px padding on both left and right. Border is 0. What is its visible width in pixels? Write the number only.
 
WU2
Which CSS layer sits outside the border and is always transparent?
  • Padding sits inside the border and shares the box's background.
  • Content is the innermost layer.
  • Correct. Margin is outside the border and is always transparent.
  • Border is visible, not transparent.
WU3
What happens to a container whose only children are all floated, with nothing else inside it?
  • Floated children do not contribute to the parent's height.
  • Correct. Floated children no longer count toward the parent's height.
  • This is a layout quirk, not an error.
  • The floated children remain fully visible.

Key vocabulary

Margin
Transparent space outside a box's border, used to create a gap between it and neighbouring boxes.
Padding
Space between a box's content and its border, inside the box, sharing its background colour.
Shorthand property
A single CSS property, such as margin or padding, that sets all four sides in one declaration.
1-value shorthand
One value applies to all four sides equally, e.g. padding: 10px;.
2-value shorthand
First value sets top and bottom, second sets left and right, e.g. margin: 10px 20px;.
4-value shorthand
Values apply clockwise from the top: top, right, bottom, left.
Margin collapse
When two vertical margins meet between stacked block elements in normal flow, the gap becomes the larger of the two values, not their sum.
margin: auto
Splits remaining horizontal space evenly on either side of a block element that has a fixed width, centring it.

Margin and padding shorthand

Two properties, one shorthand pattern

WDD7 introduced padding and margin as two of the box model's four layers - padding inside the border, sharing the box's background; margin outside the border, always transparent. Both properties accept the same shorthand patterns, so learning the pattern once covers both. Writing four separate declarations such as margin-top, margin-right, margin-bottom and margin-left works, but a single shorthand line is quicker to write and quicker to read.

How many values, and what each one means

A shorthand declaration can take one, two, three or four values, and CSS decides what each value means purely from how many are given - not from any label. With one value, that value applies to all four sides equally. With two values, the first sets top and bottom together, and the second sets left and right together. With three values, the first sets top, the second sets left and right together, and the third sets bottom. With four values, they apply clockwise starting from the top: top, right, bottom, left - the same order for both margin and padding, and worth memorising as a fixed sequence rather than re-deriving it each time.

1 value

padding: 12px;
All four sides: 12px.

2 values

padding: 10px 20px;
Top/bottom: 10px. Left/right: 20px.

3 values

padding: 5px 10px 15px;
Top: 5px. Left/right: 10px. Bottom: 15px.

4 values

padding: 8px 20px 12px 4px;
Top: 8px. Right: 20px. Bottom: 12px. Left: 4px.

Margin collapse

Vertical margins behave differently from horizontal ones

Padding never collapses, and margins never collapse horizontally - if one box has margin-right: 10px and the next box has margin-left: 6px, the total horizontal gap between them is the full 16px, exactly what adding the two values suggests. Vertical margins between stacked block elements behave completely differently. When the bottom margin of one block element meets the top margin of the block element directly below it in normal flow, the two margins collapse into a single gap equal to the larger of the two values - not their sum. A box with margin-bottom: 30px sitting above a box with margin-top: 20px produces a visible gap of 30px, not 50px.

Why this exists

Margin collapse exists so that a page built from many stacked elements, each with its own top and bottom spacing set independently, does not end up with doubled gaps everywhere two of them happen to meet. It only applies to vertical margins, only between block-level elements in normal flow stacked directly above and below each other, and never to padding, which is why the SQA specification names margin collapse as its own distinct behaviour rather than treating margin and padding as interchangeable.

Same two margin values, different axis margin-bottom: 30px; / margin-top: 20px;
Vertical (stacked boxes) 30px gap
Horizontal (side by side) 50px gap

Centring with margin: auto

Auto needs two things to work

Setting margin: auto (or just the two horizontal sides, margin: 0 auto;) tells the browser to work out the left and right margin values itself, splitting whatever horizontal space is left over equally between both sides. This only works under two conditions: the element must be a block-level element, and it must have an explicit width set that is narrower than its container. Without a fixed width, a block element already stretches to fill all the available width by default, leaving no leftover space for auto margins to distribute - the rule has nothing to do, so nothing visibly centres.

Worked examples

Example 1 - Reading a 4-value padding shorthand
.card {
  padding: 8px 20px 12px 4px;
}
1
Four values apply clockwise from the top: top, right, bottom, left.
2
Top = 8px, right = 20px, bottom = 12px, left = 4px.
Total padding added to the box's width: left + right = 4 + 20 = 24px. Total added to height: top + bottom = 8 + 12 = 20px.
Example 2 - Calculating margin collapse
.box-a { margin-bottom: 30px; }
.box-b { margin-top: 20px; }
<!-- box-b sits directly below box-a in normal flow -->
1
Both margins are vertical and the boxes are stacked block elements in normal flow, so margin collapse applies.
2
Compare the two values: 30px and 20px. Take the larger one.
Visible gap: 30px - not 50px. If the same two values applied to left/right margins on boxes sitting side by side instead, the gap would be the full 50px, because horizontal margins never collapse.
Example 3 - Centring a box with margin: auto
No width set
margin: 0 auto;

Fails. The box already fills the container's full width, so there is no leftover space to split.

width: 300px; margin: 0 auto;
width: 300px;
margin: 0 auto;

Works. The browser splits the remaining horizontal space evenly on both sides.

A fixed width narrower than the container is what gives margin: auto space to distribute - without it, there is nothing for auto to calculate.
Now you try

Box P has margin-bottom: 24px. Box Q sits directly below it in normal flow with margin-top: 16px. What is the visible vertical gap between them?

Both margins are vertical, between stacked block elements in normal flow, so they collapse to the larger value.
Compare 24px and 16px → larger is 24px.

Visible gap: 24px (not 40px).

Practical: margin collapse and centring, WebStorm

Download the starter file below and open it in WebStorm. It has a banner div and two stacked card divs with no CSS at all yet.

Download WDD8_starter.html
  1. Add an internal stylesheet inside <head>.
  2. Give .card-a a margin-bottom of 40px and .card-b a margin-top of 15px.
  3. Save and open in a browser. Measure the gap between the cards - is it 55px or 40px? Compare against the margin collapse rule above.
  4. Give .banner a width of 320px and margin: 0 auto; to centre it horizontally.
  5. Save again and check the banner sits centred with equal space either side.
Common mistakes
Exam tip

Name margin collapse by that exact term when explaining it - "the margins add up" is the wrong description and will not gain the mark. State clearly that it only applies to vertical margins between block elements in normal flow, and that the result is the larger of the two values, never the sum. Padding never collapses under any circumstances - if a question compares margin and padding behaviour, that is a safe, always-true distinguishing fact to use.

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
In padding: 10px 15px; (a 2-value shorthand), what is the padding at the top of the box? Include the unit.
 
A2
A box has content width 200px, padding-left 15px, and padding-right 25px. Border is 0. What is its visible width in pixels, not counting margin? Write the number only.
 
A3
Which statement correctly describes margin collapse?
  • Horizontal margins always add - they never collapse.
  • Correct.
  • Padding never collapses under any circumstances.
  • Collapsing produces the larger value, not the sum.
A4
Box A has margin-bottom: 18px. Box B sits directly below it in normal flow with margin-top: 26px. What is the visible gap between them in pixels? Write the number only.
 
A5
Box A has margin-bottom: 40px. Box B sits directly below it in normal flow with margin-top: 15px. What is the visible gap between them in pixels? Write the number only.
 
A6
Box C has margin-right: 10px. Box D sits immediately to its right, side by side (not stacked), with margin-left: 6px. What is the total horizontal gap between them in pixels? Write the number only.
 
A7
In padding: 5px 10px 15px; (a 3-value shorthand), which sides get 10px?
  • Correct. In a 3-value shorthand, the second value sets left and right together.
  • Top gets the first value, 5px.
  • Bottom gets the third value, 15px.
  • A 3-value shorthand sets three distinct amounts, not one shared amount.
A8
A pupil sets margin: auto; on a div, expecting it to centre horizontally, but the div does not move. Explain why, and what needs to be added to fix it.
Model answer
A9
This CSS is meant to centre .banner on the page, but nothing happens. Find the bug and write the corrected CSS.
.banner {
  margin: 0 auto;
  background: #E6F1FB;
}
.banner {
  width: 320px;
  margin: 0 auto;
  background: #E6F1FB;
}

Without a width, .banner already fills the full container width, leaving nothing for the auto margins to distribute. Adding a fixed width narrower than the container gives margin: auto actual space to centre it in.

Task Set B - Extension

Task Set B - Extension
Beyond the core specification - written answers are self-assessed against a model answer.
B1
Three boxes stack vertically in normal flow. Box 1 has margin-bottom: 12px. Box 2 has margin-top: 12px and margin-bottom: 30px. Box 3 has margin-top: 18px. State the two visible gaps (Box 1 to Box 2, and Box 2 to Box 3) and explain using the margin collapse rule.
Model answer
B2
A pupil writes padding: 20px 0; on a container, expecting this to add 20px of padding only above and below the content, with none on the sides. Is this correct? Explain what the 2-value shorthand actually sets.
Model answer
B3
Explain in your own words why margin collapse only affects vertical margins between block elements in normal flow, and never affects padding at all.
Model answer

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

Teacher notes - Shift+T to toggle

Timing: this lesson's content is scoped for a single period but lands on a double (Thu 26 Nov 2026, per WDD.md §2a/§4) - the opposite mismatch from WDD7, so there is spare time rather than a shortfall. Core pace through Task Set A should take roughly one period: 6 min warm up, 10 min shorthand notes + Example 1, 12 min margin collapse notes + context box + Example 2, 8 min centring notes + Example 3, remaining time on Task Set A. Use the second period for the full WebStorm practical in class (rather than pushing it to homework as WDD7 had to), plus Task Set B, and consider pulling forward some WDD9 warm-up material if the class finishes early.

Scope control: deliberately does not cover negative margins, percentage-based margins/padding, or vertical centring with margin: auto (vertical auto-centring does not work the same way without extra positioning context, which is out of scope) - keep to horizontal centring only, matching the SQA spec's box-model-adjacent property list.

Margin collapse is the single highest-value fact in this lesson for exam purposes - it is specific, testable, and easy to get wrong if a pupil answers from general intuition ("things add up") rather than the actual rule. A7/A8/B1 exist specifically to test this from three different angles: written explanation, numeric calculation, and code debugging.

Starter file provided (WDD8_starter.html) rather than a from-scratch build, continuing the alternation between starter-file and from-scratch practicals across WDD6/WDD7/WDD8.