Why position matters
By default, HTML elements follow the normal document flow โ they stack on top of each other in the order they appear in your code. But sometimes you need to move an element somewhere else on the page, keep it fixed while scrolling, or layer elements on top of each other.
The CSS position property gives you control over how elements are placed. Combined with offset properties (top, right, bottom, left) and z-index for stacking, you can create sophisticated layouts like sticky navigation bars, fixed back-to-top buttons, and layered designs.
The five position values
CSS has five main position values, each with different behavior:
Let's break down each one:
1. position: static
The default for all elements. Elements follow the normal document flow, stacking vertically. Offset properties like top and left have no effect on static elements.
2. position: relative
The element stays in the normal document flow and takes up its original space, but you can offset it with top, right, bottom, and left. Other elements don't move to fill the gap โ the element's original space is preserved.
3. position: absolute
The element is removed from the normal document flow. It's positioned relative to the nearest positioned ancestor (an ancestor with position other than static). If no positioned ancestor exists, it's relative to the document root. Absolutely positioned elements don't take up space in the flow.
4. position: fixed
The element is fixed to the viewport โ it stays in the same place even when you scroll. Perfect for sticky navigation bars, floating action buttons, and back-to-top buttons. It's always positioned relative to the viewport, not a parent container.
5. position: sticky
A hybrid of relative and fixed. The element scrolls normally with the page, but when it reaches a specified offset (e.g., top: 0), it "sticks" to that position while scrolling. When you scroll past its container, it goes with the flow again.
Try it yourself: Position demo
Edit the code below to see how different position values work. Notice how the absolutely positioned box is relative to the parent container, which has position: relative.
Absolute positioning within a relative parent
This is a common pattern: a parent container with position: relative and child elements with position: absolute. The absolutely positioned children are positioned relative to their parent, not the document.
Notice how the .sale-badge is positioned to the top-right corner of the .product-card container. If the parent didn't have position: relative, the badge would position itself relative to the next positioned ancestor (or the document).
Try it yourself: Sticky header & fixed button
Scroll this preview to see the sticky header and fixed back-to-top button in action. The sticky header sticks when you scroll past it, and the fixed button stays in the bottom-right corner.
Z-index and stacking order
When elements overlap, z-index controls which one appears on top. Higher z-index values appear in front of lower ones. Important: z-index only works on positioned elements (anything except position: static).
Stacking contexts
Setting z-index on a positioned element creates a new stacking context. Children of that element are stacked relative to each other, but the whole group is stacked relative to other elements. This can be tricky:
- A child with
z-index: 9999inside a parent withz-index: 1might still appear behind a sibling withz-index: 2. - The parent's stacking context takes precedence.
Task: Build a mini e-commerce page with:
- A sticky header at the top with the store name
- A main content area with multiple product cards
- Each product card has an absolutely positioned "Sale!" badge in the top-right corner
- A fixed "back to top" button in the bottom-right corner with a fixed z-index
- The page should have enough content to require scrolling
Start with the Try It Yourself editors above as templates!
Hint: Use position: sticky; top: 0; for the header, position: relative; on product cards, position: absolute; for badges, and position: fixed; for the back-to-top button. Use z-index: 10 for sticky header, z-index: 20 for the fixed button to ensure they layer correctly.
Key takeaways
- position: static is the default โ elements follow normal document flow.
- position: relative offsets an element from its normal position but keeps it in the flow.
- position: absolute removes an element from the flow and positions it relative to the nearest positioned ancestor.
- position: fixed keeps an element fixed in the viewport, perfect for sticky headers and buttons.
- position: sticky is a hybrid โ element scrolls normally then sticks at a threshold.
- z-index controls stacking order but only works on positioned elements.
- Stacking contexts determine how children are layered relative to each other and to the rest of the page.
Test your knowledge
You just mastered positioning.
Ready to build full-stack apps?
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ