Position: relative keeps the element in normal flow but allows offsetting it; absolute removes it from flow and positions it relative to the nearest positioned ancestor; fixed removes it from flow and pins it to the viewport. Use relative for anchoring children, absolute for overlays, and fixed for sticky UI like headers. Positioning affects stacking, layout flow, and performance; test scrolling and overlap edge cases.
What is the difference between position: relative, absolute, and fixed?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
CSS positioning allows you to precisely control where elements appear. The key difference among relative, absolute, and fixed lies in their reference points and whether they occupy space in the normal document flow.
Value | Reference Point | Affects Document Flow? | Typical Use |
|---|---|---|---|
relative | Itself (original position) | ✅ Yes | Small offsets without removing from layout. |
absolute | Nearest positioned ancestor (or the document if none) | ❌ No | Tooltips, popovers, or overlays positioned freely. |
fixed | Viewport (browser window) | ❌ No | Sticky headers, floating buttons, or navigation bars that stay in view. |
Example: Demonstrating Different Positions
.relative-box {
position: relative;
top: 10px; left: 20px;
}
.absolute-box {
position: absolute;
top: 10px; left: 20px;
}
.fixed-box {
position: fixed;
top: 0; right: 0;
background: #0ea5e9;
color: white;
padding: 10px;
}
- Relative: Moves the element relative to its normal spot, but the space it occupies remains reserved.
- Absolute: Removes the element from the normal flow and positions it based on the nearest ancestor with
positionset. - Fixed: Anchors the element to the viewport, unaffected by scrolling.
Positioning Hierarchy
- Nested
absoluteelements look for the nearest ancestor withposition: relativeorabsoluteto align against. - Fixed elements always align with the viewport unless inside a transformed parent (e.g.,
transform: translateZ(0)creates a new stacking context).
Use Cases
relative: fine-tune an element’s position for minor alignment.absolute: create dropdown menus or tooltips detached from normal layout flow.fixed: keep navigation bars or chat buttons visible during scrolling.
Practical scenario
Pin a badge to a card corner and a header to the top on scroll.
Common pitfalls
- Absolute elements escaping because the parent isn’t positioned.
- Fixed elements causing overlay or z-index conflicts.
- Ignoring mobile viewport and safe areas.
Absolute positioning is simple but brittle. Test on scroll, resize, and mobile viewports.
Imagine three notes: one pinned in place (fixed), one floating near its section header (absolute), and one slightly nudged but still within its group (relative).
relativemoves an element without removing it from flow.absoluteremoves the element and positions it freely.fixedlocks the element relative to the viewport for persistent visibility.- Choosing the right positioning is essential for precise UI layouts and responsive design.