0
0
CSSmarkup~20 mins

Margin in CSS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Margin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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>
A20px
B50px
C30px
D0px
Attempts:
2 left
💡 Hint
Margins between vertical block elements collapse to the larger margin, not the sum.
📝 Syntax
intermediate
1: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 */
}
Amargin: 1.5rem;
Bmargin: 1.5rem 1.5rem 1.5rem;
Cmargin: 1.5rem 0 1.5rem 0;
Dmargin: 0 1.5rem;
Attempts:
2 left
💡 Hint
A single value for margin applies to all sides equally.
rendering
advanced
2: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>
AThe box disappears from the page.
BThe box moves 20px downward, increasing space below.
CThe box's height shrinks by 20px.
DThe box moves 20px upward, overlapping content above it.
Attempts:
2 left
💡 Hint
Negative margin pulls the element closer or over adjacent elements.
selector
advanced
2: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?
Ap[style*='margin-left: 2rem']
Bp[style='margin-left: 2rem']
Cp[style^='margin-left: 2rem']
Dp[style$='margin-left: 2rem']
Attempts:
2 left
💡 Hint
Use substring matching to find styles containing the margin-left property.
accessibility
expert
3: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?
ANegative margins automatically add ARIA labels to elements.
BNegative margins can move the focus outline off-screen, making keyboard focus invisible.
CNegative margins increase clickable area, improving accessibility.
DNegative margins improve screen reader announcements by hiding elements.
Attempts:
2 left
💡 Hint
Think about how keyboard users see which element is focused.