Position relative lets you move an element a little bit from where it normally sits. It keeps space for the element in the page layout.
Position relative in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector {
position: relative;
top: 10px; /* moves down 10px */
left: 5px; /* moves right 5px */
}The position: relative; property keeps the element in the normal page flow.
Using top, left, bottom, and right moves the element from its original spot.
div {
position: relative;
top: 20px;
left: 10px;
}p {
position: relative;
top: -5px;
left: 0;
}button {
position: relative;
right: 15px;
bottom: 10px;
}The blue box moves down and right from its normal place, but space for it remains reserved. The second box stays where it is.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Position Relative Example</title> <style> .box { width: 150px; height: 100px; background-color: lightblue; margin: 20px; position: relative; top: 30px; left: 40px; border: 2px solid navy; padding: 10px; font-family: Arial, sans-serif; } </style> </head> <body> <div class="box">This box is moved down 30px and right 40px.</div> <div>This box stays in normal position.</div> </body> </html>
Relative positioning moves the element visually but keeps its original space in the layout.
If you want the element to not take space where it was, use position: absolute; instead.
Use browser DevTools (right-click element > Inspect) to see how the element moves.
Position relative moves an element from its normal spot without changing page layout.
You can move elements up, down, left, or right using top, left, bottom, and right.
It is useful for small adjustments and layering elements.
Practice
position: relative; do to an HTML element?Solution
Step 1: Understand position relative behavior
Position relative moves the element from where it normally is but keeps its space reserved in the layout.Step 2: Compare with other position types
Unlike absolute or fixed, relative does not remove the element from the document flow.Final Answer:
Moves the element relative to its normal position without affecting other elements. -> Option DQuick Check:
Relative = moves element but keeps space [OK]
- Thinking relative removes element from flow
- Confusing relative with absolute or fixed
- Assuming relative positions element at page corner
Solution
Step 1: Identify correct property for moving down
Usingtop: 10px;moves the element 10px down relative to its normal position.Step 2: Confirm position type
Position must be relative to usetopfor relative movement.Final Answer:
position: relative; top: 10px; -> Option BQuick Check:
Relative + top moves element down [OK]
- 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
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?Solution
Step 1: Understand left and top with relative
Left: 20px moves the element 20px to the right; top: 10px moves it 10px down.Step 2: Combine movements
Both together shift the element diagonally right and down from its original spot.Final Answer:
Shifted 20px right and 10px down from its normal position. -> Option AQuick Check:
Left positive = right, top positive = down [OK]
- Thinking left:20px moves left instead of right
- Confusing top positive as moving up
- Ignoring combined effect of left and top
p element 15px down, but it doesn't work as expected:
p {
position: relative;
bottom: 15px;
}
What is the problem?Solution
Step 1: Understand bottom property with relative
Positivebottom: 15px;moves the element up by 15px relative to its normal position.Step 2: Compare intention vs actual behavior
Intended to move down, but positivebottommoves up instead.Final Answer:
bottom: 15px;moves the element up, not down. -> Option AQuick Check:
Bottom positive moves up [OK]
- Confusing bottom positive as moving down
- Thinking position must be absolute to move
- Believing display affects position movement
Solution
Step 1: Understand layering with relative positioning
Usingposition: relative;on both keeps layout space but allows moving the second box over the first.Step 2: Apply left and top offsets to second box
Settingleft: 30px;andtop: 20px;moves the second box right and down overlapping the first.Step 3: Avoid absolute to keep layout unchanged
Absolute removes element from flow, changing layout. Relative keeps layout intact.Final Answer:
First box: position: relative; Second box: position: relative; left: 30px; top: 20px; -> Option CQuick Check:
Relative + offsets overlap without layout shift [OK]
- 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
