Position absolute lets you place an element exactly where you want on the page. It helps you move things freely without affecting other parts.
Position absolute in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector {
position: absolute;
top: value;
right: value;
bottom: value;
left: value;
}Use top, right, bottom, and left to move the element from the edges.
The element moves relative to the nearest parent that has position set to relative, absolute, or fixed. If none, it moves relative to the page.
div {
position: absolute;
top: 10px;
left: 20px;
}button {
position: absolute;
bottom: 0;
right: 0;
}span {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}This example shows a blue container box with a smaller dark blue box placed at the top right corner using position: absolute. The container has position: relative so the small box moves relative to it.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Position Absolute Example</title> <style> .container { position: relative; width: 300px; height: 200px; background-color: #cce5ff; border: 2px solid #004085; margin: 2rem auto; padding: 1rem; font-family: Arial, sans-serif; } .box { position: absolute; top: 10px; right: 10px; background-color: #004085; color: white; padding: 0.5rem 1rem; border-radius: 0.5rem; font-weight: bold; } </style> </head> <body> <main> <section class="container" aria-label="Blue container with absolute positioned box"> This is a container box. <div class="box" role="note">Top Right</div> </section> </main> </body> </html>
Always set position: relative on the parent container to control where the absolute element moves.
Absolute elements are removed from the normal page flow, so they don't take up space and can overlap other content.
Use keyboard and screen reader friendly roles and labels when placing important content absolutely.
Position absolute lets you place elements exactly where you want inside a container.
It moves relative to the nearest positioned parent or the page if none.
Use top, right, bottom, and left to control the exact spot.
Practice
position: absolute; do to an element in CSS?Solution
Step 1: Understand position absolute behavior
Position absolute removes the element from normal flow and places it relative to the nearest positioned ancestor.Step 2: Compare with other position types
Unlike static or fixed, absolute positions relative to a parent with position set, not the viewport or normal flow.Final Answer:
It places the element exactly where you want inside its nearest positioned ancestor. -> Option BQuick Check:
Position absolute = relative to nearest positioned parent [OK]
- Thinking absolute keeps element in normal flow
- Confusing absolute with fixed position
- Assuming absolute always positions relative to viewport
Solution
Step 1: Identify correct position property
Onlyposition: absolute;allows exact placement withtopandleft.Step 2: Check the offset properties
Usingtop: 20px;andleft: 30px;moves the element 20px down and 30px right from its positioned ancestor.Final Answer:
position: absolute; top: 20px; left: 30px; -> Option CQuick Check:
Absolute + top/left = exact position [OK]
- Using position relative instead of absolute
- Using position static which ignores top/left
- Confusing fixed with absolute positioning
<div class='container'>
<div class='box'>Red Box</div>
</div>
.container {
position: relative;
width: 200px;
height: 200px;
background: lightgray;
}
.box {
position: absolute;
top: 50px;
right: 20px;
width: 100px;
height: 50px;
background: red;
color: white;
}Solution
Step 1: Identify container positioning
The container hasposition: relative;, so it becomes the reference for absolute children.Step 2: Locate the box using top and right
The box is positioned 50px from the top and 20px from the right inside the container.Final Answer:
Inside the container, 50px from top and 20px from right edge -> Option AQuick Check:
Absolute inside relative = positioned inside container [OK]
- Thinking absolute positions relative to page if parent is relative
- Ignoring right property and assuming left positioning
- Assuming box is centered without explicit centering
.child {
position: absolute;
top: 10px;
}
.parent {
width: 300px;
height: 300px;
background: blue;
}The HTML is:
<div class='parent'> <div class='child'>Hello</div> </div>
Solution
Step 1: Check parent positioning
The parent has no position set, so it defaults to static, which does not create a positioning context.Step 2: Understand absolute positioning reference
The child with position absolute will position relative to the nearest positioned ancestor or the page if none exists.Final Answer:
Because the parent has no position set, so child positions relative to the page -> Option DQuick Check:
Absolute needs positioned parent to position inside it [OK]
- Assuming absolute always positions inside parent
- Thinking top needs left or right to work
- Believing display property affects absolute positioning
position: relative;. Which CSS for the tooltip ensures it appears above the button using absolute positioning?.tooltip {
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%) translateY(-10px);
background: black;
color: white;
padding: 5px;
border-radius: 3px;
}Solution
Step 1: Understand bottom: 100% usage
Settingbottom: 100%places the tooltip exactly at the top edge of the button.Step 2: Apply transform for offset and centering
translateX(-50%)centers horizontally, andtranslateY(-10px)moves it 10px further up.Final Answer:
This CSS places the tooltip 10px above the button, centered horizontally. -> Option AQuick Check:
bottom 100% + translateY(-10px) = 10px above [OK]
- Using top instead of bottom for positioning above
- Not centering with translateX(-50%)
- Forgetting container must be relative
