CSS Position
CSS Position: Relative, Absolute, Fixed, and Sticky Layouts
The CSS position property controls whether an element stays in the normal document flow or detaches and anchors to a parent card, the browser viewport, or a scroll threshold. Combined with top, right, bottom, left, and z-indexit builds sticky headers, notification badges, and modal overlays.
This guide explains all five position values, shows a working example, and covers the bugs that most often break positioned layouts.
flowchart TD
P["CSS position"] --> S["static (Default document flow)"]
P --> R["relative (Offset from own spot; anchor for children)"]
P --> A["absolute (Anchored to nearest positioned parent)"]
P --> F["fixed (Locked to browser viewport)"]
P --> ST["sticky (Scrolls normally, then sticks at threshold)"]
What Is the CSS Position Property?
By default, every element sits in the normal flow: block elements stack downward, inline elements flow along a line. The position property changes how an element is placed, and the offset properties (top, right, bottom, left, or the shorthand inset) then move it relative to a reference point. That reference point depends on the value you choose.
Two ideas make everything else easier to understand:
Normal flow: the default layout where elements take up space and push each other around.
Containing block: the box that an offset is measured from. For
absolute, it's the nearest ancestor that is positioned (anything other thanstatic). Forfixed, it's usually the viewport.
Comparing the Five Position Modes
static: Default value; ignorestop,left, andz-index.relative: Stays in normal layout space while nudging visually with offsets—and crucially creates a coordinate anchor forposition: absolutechild elements.absolute: Leaves normal layout flow and anchors to the nearest ancestor whosepositionis notstatic(or the viewport if none exists).fixed: Pins to the browser viewport even while scrolling—ideal for floating chat widgets or full-screen modal backdrops.sticky: Actsrelativeuntil scrolling hitstop: 0;, then sticks inside its parent container.
.product-card {
position: relative;
padding: 20px;
border: 1px solid #cbd5e1;
border-radius: 8px;
}
.sale-badge {
position: absolute;
top: 12px;
right: 12px;
background-color: #dc2626;
color: #ffffff;
padding: 3px 8px;
font-size: 0.75rem;
border-radius: 4px;
}
.site-header {
position: sticky;
top: 0;
z-index: 50;
background-color: #ffffff;
}
What each part does:
.product-cardisrelative, so it becomes the reference box for the badge..sale-badgeis absolute, withtop: 12px; right: 12px, so it sits 12px from the card's top and right edges. It no longer takes up space inside the card..site-headerisstickywithtop: 0, so it scrolls normally until it reaches the top of the viewport, then stays there.z-index: 50Keeps it above scrolling content, and the background stops content from showing through.
How to Check That Your Positioning Works
Open DevTools, select the positioned element, and look at the Computed tab for its
positionvalue and offsets.Walk up the DOM and look for ancestors with
position,overflow,transform, filter, orperspective. These explain most surprises.Resize the window and scroll to check that sticky and fixed elements behave at phone widths.
Toggle
overflowortransformoff on ancestors in DevTools to find which one breaks the layout.If an element looks misplaced, temporarily add outline:
2px solid redto it and its parent to see their boxes.
Common Mistakes to Avoid
Using
position: absolutewithoutposition: relativeon the parent: Without a positioned parent, your badge flies up to the top-right corner of the whole browser window.Putting
overflow: hiddenon an ancestor ofposition: sticky: Overflow clipping breaks sticky headers silently; useoverflow-x: clip;if needed.Setting
z-indexon static elements:z-indexworks only on positioned elements (relative,absolute,fixed,sticky) or Flex/Grid items.
Conclusion
This is easiest to use when you pick the value by job. Use relative to anchor children, absolute for badges and panels, fixed for widgets and backdrops, and sticky for headers that stay in the flow. When something misbehaves, check ancestors for overflow, transformand stacking contexts before changing offsets or raising z-index.
Frequently Asked Questions
Why is position: sticky preferred over fixed for top navbars?
sticky Keeps the header inside normal document flow so you do not have to add fake padding-top to body to stop content from hiding underneath it.
What does inset: 0 mean in modern CSS?
inset: 0; is shorthand fortop: 0; right: 0; bottom: 0; left: 0;, great for stretching full-screen modal backdrops.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.