Bird
Raised Fist0
CSSmarkup~20 mins

Margin in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the CSS margin property do?
easy
A. Changes the background color of an element
B. Adds space outside an element to separate it from others
C. Adds space inside an element around its content
D. Sets the font size of text inside an element

Solution

  1. Step 1: Understand margin property purpose

    The margin property controls the space outside an element, creating separation from other elements.
  2. Step 2: Differentiate margin from padding

    Padding adds space inside the element, margin adds space outside it.
  3. Final Answer:

    Adds space outside an element to separate it from others -> Option B
  4. Quick Check:

    Margin = space outside element [OK]
Hint: Margin controls outside space, padding controls inside space [OK]
Common Mistakes:
  • Confusing margin with padding
  • Thinking margin changes element color
  • Mixing margin with font properties
2. Which of the following is the correct CSS syntax to set a 2rem margin on all sides of an element?
easy
A. margin: 2rem;
B. margin: 2rem 2rem 2rem;
C. margin: 2rem 0;
D. margin: 2rem 2rem 2rem 2rem 2rem;

Solution

  1. Step 1: Recall shorthand margin syntax

    Using one value sets margin equally on all four sides.
  2. Step 2: Check options for correct shorthand

    margin: 2rem; uses one value correctly. margin: 2rem 2rem 2rem; uses three values (top, sides, bottom). margin: 2rem 0; uses two values (top/bottom and left/right). margin: 2rem 2rem 2rem 2rem 2rem; has five values, which is invalid.
  3. Final Answer:

    margin: 2rem; -> Option A
  4. Quick Check:

    One value sets all sides equally [OK]
Hint: One value sets margin on all sides equally [OK]
Common Mistakes:
  • Using too many values in margin shorthand
  • Confusing margin with padding syntax
  • Forgetting units like rem or px
3. Given this CSS code:
div {
  margin: 1rem 2rem 3rem 4rem;
}

What is the margin on the bottom side of the div?
medium
A. 1rem
B. 2rem
C. 3rem
D. 4rem

Solution

  1. Step 1: Understand margin shorthand order

    Margin values in order: top, right, bottom, left.
  2. Step 2: Identify bottom margin value

    The third value is bottom margin, which is 3rem here.
  3. Final Answer:

    3rem -> Option C
  4. Quick Check:

    Margin order: top, right, bottom, left [OK]
Hint: Remember margin order: TRBL (top, right, bottom, left) [OK]
Common Mistakes:
  • Mixing up the order of margin values
  • Choosing left or right value instead of bottom
  • Ignoring the order and picking first value
4. This CSS code is intended to add 10px margin on all sides of a paragraph, but it doesn't work:
p {
  margin: 10;
}

What is the error?
medium
A. Missing unit 'px' after 10
B. Wrong property name, should be 'padding'
C. Margin cannot be set on paragraphs
D. Value 10 is too small to see margin

Solution

  1. Step 1: Check CSS value units

    CSS length values require units like px, rem, em unless zero.
  2. Step 2: Identify missing unit error

    Value '10' without unit is invalid, so margin is ignored.
  3. Final Answer:

    Missing unit 'px' after 10 -> Option A
  4. Quick Check:

    Length values need units except zero [OK]
Hint: Always add units like px or rem for margin values except zero [OK]
Common Mistakes:
  • Forgetting units on numeric values
  • Confusing margin with padding
  • Assuming margin can't apply to paragraphs
5. You want to center a fixed-width box horizontally inside its container using margin. Which CSS rule achieves this best?
hard
A. margin-left: 50%;
B. margin: auto 0;
C. margin: 10px;
D. margin: 0 auto;

Solution

  1. Step 1: Understand horizontal centering with margin

    Setting left and right margins to auto centers a block horizontally.
  2. Step 2: Analyze options for horizontal centering

    margin: 0 auto; sets top/bottom margin 0 and left/right margin auto, centering horizontally. margin: auto 0; sets vertical margins auto, which doesn't center horizontally. margin: 10px; sets fixed margin on all sides, no centering. margin-left: 50%; moves box 50% from left, but does not center properly.
  3. Final Answer:

    margin: 0 auto; -> Option D
  4. Quick Check:

    Horizontal center = margin-left/right auto [OK]
Hint: Use margin: 0 auto to center block horizontally [OK]
Common Mistakes:
  • Using margin auto on top/bottom instead of left/right
  • Setting fixed margin values instead of auto
  • Using margin-left 50% without transform