Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.3 — Margins in Depth
CSS · Example 3 of 3

Negative margin: an avatar overlapping a banner

Margins can be auto (to centre a block), negative (to pull boxes closer or overlap them), and vertical margins between blocks collapse into one, which explains most mystery gaps in CSS.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Centre a .notice that is at most 500px wide in its parent, then log the computed margin-left when the parent is 700px wide.

02

Two paragraphs: the first has margin-bottom: 24px, the second margin-top: -8px. Both are 30px tall blocks. Log the gap between them.

03

Fix a .card whose first child h2 margin escapes, without changing the h2. Log the distance from the card's top to the h2's top.

Explain it without notes

01

What is margin collapsing and when does it happen?

02

Why does margin: 0 auto centre a block, and what does it need to work?

03

A child's top margin shows up above its parent's background. Why, and what are three ways to fix it?

04

Give two legitimate uses of negative margins and one risk.

05

In which situations do margins never collapse?

Negative margin: an avatar overlapping a banner

Preview

Press Run to see the page.

Console

Nothing logged yet.