Bird
Raised Fist0
CSSmarkup~10 mins

Position relative in CSS - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the box position relative.

CSS
div {
  position: [1];
  width: 100px;
  height: 100px;
  background-color: lightblue;
}
Drag options to blanks, or click blank then click option'
Astatic
Babsolute
Cfixed
Drelative
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absolute' instead of 'relative' changes the positioning context.
Using 'static' means no positioning is applied.
2fill in blank
medium

Complete the code to move the box 20px down from its normal position.

CSS
div {
  position: relative;
  top: [1];
  width: 100px;
  height: 100px;
  background-color: coral;
}
Drag options to blanks, or click blank then click option'
A0
B-20px
C20px
Dauto
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative values moves the element up, not down.
Using 'auto' does not move the element.
3fill in blank
hard

Fix the error in the code to correctly position the box relative and move it 15px left.

CSS
div {
  position: [1];
  left: -15px;
  width: 80px;
  height: 80px;
  background-color: lightgreen;
}
Drag options to blanks, or click blank then click option'
Arelative
Babsolute
Cfixed
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' means offset properties like left do not work.
Using 'fixed' changes the element's position relative to the viewport.
4fill in blank
hard

Fill both blanks to create a relative positioned box moved 10px right and 5px up.

CSS
div {
  position: [1];
  left: [2];
  top: -5px;
  width: 90px;
  height: 90px;
  background-color: peachpuff;
}
Drag options to blanks, or click blank then click option'
Arelative
Babsolute
C10px
D5px
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absolute' changes the positioning context.
Using positive top moves the box down, not up.
5fill in blank
hard

Fill all three blanks to create a relative box moved 25px down, 15px left, with a blue border.

CSS
div {
  position: [1];
  top: [2];
  left: [3];
  width: 120px;
  height: 120px;
  border: 2px solid blue;
  background-color: lavender;
}
Drag options to blanks, or click blank then click option'
Aabsolute
Brelative
C-15px
D25px
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absolute' changes the positioning context.
Mixing up positive and negative values for top and left.

Practice

(1/5)
1. What does position: relative; do to an HTML element?
easy
A. Positions the element absolutely at the top-left corner of the page.
B. Removes the element from the page flow completely.
C. Fixes the element to the viewport so it doesn't move when scrolling.
D. Moves the element relative to its normal position without affecting other elements.

Solution

  1. Step 1: Understand position relative behavior

    Position relative moves the element from where it normally is but keeps its space reserved in the layout.
  2. Step 2: Compare with other position types

    Unlike absolute or fixed, relative does not remove the element from the document flow.
  3. Final Answer:

    Moves the element relative to its normal position without affecting other elements. -> Option D
  4. Quick Check:

    Relative = moves element but keeps space [OK]
Hint: Relative moves element but keeps space in layout [OK]
Common Mistakes:
  • Thinking relative removes element from flow
  • Confusing relative with absolute or fixed
  • Assuming relative positions element at page corner
2. Which of the following is the correct CSS syntax to move an element 10px down using relative positioning?
easy
A. position: relative; bottom: 10px;
B. position: relative; top: 10px;
C. position: absolute; top: 10px;
D. position: fixed; left: 10px;

Solution

  1. Step 1: Identify correct property for moving down

    Using top: 10px; moves the element 10px down relative to its normal position.
  2. Step 2: Confirm position type

    Position must be relative to use top for relative movement.
  3. Final Answer:

    position: relative; top: 10px; -> Option B
  4. Quick Check:

    Relative + top moves element down [OK]
Hint: Use top with relative to move down [OK]
Common Mistakes:
  • Using bottom to move element down (it moves up)
  • Mixing position absolute or fixed instead of relative
  • Using left or right instead of top for vertical movement
3. Given this CSS:
div {
  position: relative;
  left: 20px;
  top: 10px;
  background-color: lightblue;
  width: 100px;
  height: 50px;
}
What will be the visual position of the div compared to its normal spot?
medium
A. Shifted 20px right and 10px down from its normal position.
B. Shifted 20px left and 10px up from its normal position.
C. No movement; stays in normal position.
D. Shifted 20px right and 10px up from its normal position.

Solution

  1. Step 1: Understand left and top with relative

    Left: 20px moves the element 20px to the right; top: 10px moves it 10px down.
  2. Step 2: Combine movements

    Both together shift the element diagonally right and down from its original spot.
  3. Final Answer:

    Shifted 20px right and 10px down from its normal position. -> Option A
  4. Quick Check:

    Left positive = right, top positive = down [OK]
Hint: Positive left moves right; positive top moves down [OK]
Common Mistakes:
  • Thinking left:20px moves left instead of right
  • Confusing top positive as moving up
  • Ignoring combined effect of left and top
4. This CSS code is intended to move a p element 15px down, but it doesn't work as expected:
p {
  position: relative;
  bottom: 15px;
}
What is the problem?
medium
A. bottom: 15px; moves the element up, not down.
B. The position property should be absolute to move down.
C. The value should be negative like bottom: -15px; to move down.
D. The element needs display: block; to move.

Solution

  1. Step 1: Understand bottom property with relative

    Positive bottom: 15px; moves the element up by 15px relative to its normal position.
  2. Step 2: Compare intention vs actual behavior

    Intended to move down, but positive bottom moves up instead.
  3. Final Answer:

    bottom: 15px; moves the element up, not down. -> Option A
  4. Quick Check:

    Bottom positive moves up [OK]
Hint: Positive bottom moves element up, negative moves down [OK]
Common Mistakes:
  • Confusing bottom positive as moving down
  • Thinking position must be absolute to move
  • Believing display affects position movement
5. You want to layer two boxes so the second box overlaps the first by 30px to the right and 20px down, but keep the page layout unchanged. Which CSS is best?
hard
A. First box: position: static; Second box: position: relative; right: 30px; bottom: 20px;
B. First box: position: absolute; Second box: position: absolute; left: 30px; top: 20px;
C. First box: position: relative; Second box: position: relative; left: 30px; top: 20px;
D. First box: position: relative; Second box: position: absolute; left: 30px; top: 20px;

Solution

  1. Step 1: Understand layering with relative positioning

    Using position: relative; on both keeps layout space but allows moving the second box over the first.
  2. Step 2: Apply left and top offsets to second box

    Setting left: 30px; and top: 20px; moves the second box right and down overlapping the first.
  3. Step 3: Avoid absolute to keep layout unchanged

    Absolute removes element from flow, changing layout. Relative keeps layout intact.
  4. Final Answer:

    First box: position: relative; Second box: position: relative; left: 30px; top: 20px; -> Option C
  5. Quick Check:

    Relative + offsets overlap without layout shift [OK]
Hint: Use relative + left/top to overlap without layout change [OK]
Common Mistakes:
  • Using absolute removes element from flow, breaks layout
  • Using right/bottom offsets incorrectly for desired direction
  • Not setting position relative on first box for layering context