Challenge - 5 Problems
Margin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding margin collapsing
Consider two adjacent
<div> elements stacked vertically. Both have margin-bottom: 20px; and margin-top: 30px; respectively. What is the total vertical space between them?CSS
<style> .box1 { margin-bottom: 20px; background: lightblue; height: 50px; } .box2 { margin-top: 30px; background: lightgreen; height: 50px; } </style> <div class="box1"></div> <div class="box2"></div>
Attempts:
2 left
💡 Hint
Margins between vertical block elements collapse to the larger margin, not the sum.
✗ Incorrect
When two vertical margins meet, the larger margin value is used instead of adding them. Here, 30px is larger than 20px, so the space is 30px.
📝 Syntax
intermediate1:30remaining
Which CSS rule sets equal margin on all sides?
You want to set a margin of 1.5rem on all four sides of a
<section>. Which CSS rule achieves this?CSS
section {
/* Your margin rule here */
}Attempts:
2 left
💡 Hint
A single value for margin applies to all sides equally.
✗ Incorrect
Using one value in margin sets all four sides to that value.
❓ rendering
advanced2:00remaining
Visual effect of negative margin
What visual effect does applying
margin-top: -20px; to a <div> have?CSS
<style> .container { background: #eee; padding: 2rem; } .box { background: coral; height: 100px; margin-top: -20px; } </style> <div class="container"> <div class="box">Box with negative top margin</div> </div>
Attempts:
2 left
💡 Hint
Negative margin pulls the element closer or over adjacent elements.
✗ Incorrect
Negative margin-top shifts the element upward, possibly overlapping previous content.
❓ selector
advanced2:30remaining
Selecting elements with different margin values
You want to select all
<p> elements that have a margin-left of exactly 2rem using CSS attribute selectors. Which selector is correct?Attempts:
2 left
💡 Hint
Use substring matching to find styles containing the margin-left property.
✗ Incorrect
The *= attribute selector matches any element whose attribute contains the given substring. This works for inline styles containing margin-left: 2rem.
❓ accessibility
expert3:00remaining
Margin and keyboard focus visibility
Why is it important to avoid using large negative margins on focusable elements like buttons or links in terms of accessibility?
Attempts:
2 left
💡 Hint
Think about how keyboard users see which element is focused.
✗ Incorrect
Large negative margins can shift the focus outline outside the visible area, making it hard for keyboard users to know which element is focused. This harms accessibility.