Normally the browser places elements one after another in the normal flow: blocks stack down the page and inline elements run along a line. The CSS position property lets you break out of that flow when you need to: nudging an element, pinning a badge to the corner of a card, keeping a header visible while the page scrolls, or layering pop-ups above everything else. This lesson explains the five position values, the offset properties and z-index.
In this lesson you will learn
static,relative,absolute,fixedandsticky- Using
top,right,bottomandleft - What the “containing block” of an absolute element is
- Making a sticky header
- Controlling overlap with
z-index
This lesson builds on the CSS box model.
relative and absolute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Positioning</title>
<style>
body { font-family: sans-serif; margin: 8px; }
.box { background: #00695c; color: white; padding: 6px 10px; border-radius: 6px; display: inline-block; }
.relative { position: relative; top: 10px; left: 30px; background: #ef6c00; }
.card { position: relative; background: #eceff1; height: 110px; margin-top: 24px; border-radius: 8px; }
.badge { position: absolute; top: 8px; right: 8px; background: #c62828; color: white;
padding: 2px 8px; border-radius: 10px; font-size: 13px; }
.corner { position: absolute; bottom: 8px; left: 8px; }
</style>
</head>
<body>
<span class="box">static</span>
<span class="box relative">relative: moved 10px down, 30px right</span>
<span class="box">static</span>
<div class="card">
The card is position: relative
<span class="badge">NEW</span>
<span class="box corner">absolute, bottom-left</span>
</div>
</body>
</html>
Result
staticis the default: the element sits in the normal flow and ignorestop,leftand the other offsets.position: relativemoves the element from where it would normally be. Its original space is kept, so neighbours do not move up to fill it.position: absoluteremoves the element from the flow and positions it against its containing block: the nearest ancestor that is itself positioned (anything except static). If there is none, it uses the page.- That is why the card has
position: relativewith no offsets: it becomes the anchor, sotop: 8px; right: 8pxputs the “NEW” badge in the card’s corner rather than the page’s.
sticky, fixed and z-index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sticky and z-index</title>
<style>
body { font-family: sans-serif; margin: 0; }
header { position: sticky; top: 0; background: #1565c0; color: white; padding: 10px; z-index: 10; }
p { margin: 10px; line-height: 1.6; }
.stack { position: relative; height: 90px; margin: 10px; }
.stack div { position: absolute; width: 90px; height: 60px; color: white; padding: 4px; border-radius: 6px; }
.a { background: #6a1b9a; left: 0; top: 0; z-index: 1; }
.b { background: #2e7d32; left: 40px; top: 20px; z-index: 3; }
.c { background: #ef6c00; left: 80px; top: 10px; z-index: 2; }
</style>
</head>
<body>
<header>Sticky header: scroll inside this box</header>
<div class="stack"><div class="a">z 1</div><div class="b">z 3</div><div class="c">z 2</div></div>
<p>Scroll down. The header stays at the top of this frame because of position: sticky and top: 0.</p>
<p>Paragraph two of filler text to make the page long enough to scroll.</p>
<p>Paragraph three. Sticky elements behave like relative ones until they reach their offset.</p>
<p>Paragraph four. Then they stick in place while their parent is visible.</p>
<p>Paragraph five, the end.</p>
</body>
</html>
Result
position: sticky; top: 0makes the header scroll normally until it reaches the top, then stay there while its parent is on screen. Scroll inside the result frame to see it. Sticky needs an offset such astopto work.position: fixedpins an element to the browser window itself, so it never scrolls, as with chat buttons or cookie bars. Like absolute, it leaves the normal flow.- When positioned elements overlap,
z-indexdecides which is on top: higher numbers win. Green (z-index 3) covers orange (2), which covers purple (1).z-indexonly affects positioned elements and flex or grid items.
Position Values
| Value | In normal flow? | Offsets relative to | Typical use |
|---|---|---|---|
static | Yes | Not used | Default |
relative | Yes (space kept) | Its own normal position | Small nudges, anchor for absolute children |
absolute | No | Nearest positioned ancestor | Badges, tooltips, dropdowns |
fixed | No | The browser window | Floating buttons, fixed navbars |
sticky | Yes, until it sticks | Its scrolling container | Sticky headers, table headings |
To centre an absolute element: top: 50%; left: 50%; transform: translate(-50%, -50%);. For page layout itself, prefer Flexbox and Grid; positioning is for special cases.
Common Mistakes
- Forgetting to position the parent, so an absolute child jumps to a corner of the whole page.
- Using absolute positioning for whole layouts, which breaks on different screen sizes.
- Sticky not sticking because no
topis set or an ancestor hasoverflow: hidden. - Raising z-index to 9999 without understanding stacking contexts; a parent with its own z-index or a
transformlimits how high a child can go.
Practice Exercises
Exercise 1. Place a “Sale” label in the top-left corner of a product image.
Show answer
.product { position: relative; }
.product .sale { position: absolute; top: 0; left: 0; background: #c62828; color: #fff; padding: 4px 8px; }
Exercise 2. Make a “Back to top” button that always stays 20px from the bottom-right of the window.
Show answer
.to-top { position: fixed; right: 20px; bottom: 20px; z-index: 100; }
Frequently Asked Questions
What is the difference between fixed and sticky?
A fixed element is always pinned to the window and takes no space in the layout. A sticky element behaves normally until you scroll to its offset, then sticks, and only within its parent.
Why does z-index not work on my element?
It needs a position other than static (or to be a flex or grid item). Also check whether a parent creates its own stacking context, which caps the child’s level.
Summary
relativenudges an element and anchors absolute children.absolutepositions against the nearest positioned ancestor.fixedpins to the window;stickysticks once scrolled to its offset.z-indexorders overlapping positioned elements.
Next: Responsive Web Design.
