T2 CSS · Lesson 7
Positioning
Taking a box out of the normal flow: nudging it with relative, pinning it to a parent with absolute, keeping it on screen with fixed, holding it at an edge with sticky, and choosing which overlapping box lies on top with z-index.
- say how the normal flow places boxes, and nudge one with position: relative and its offsets
- pin a badge or a ribbon to the corner of a card with a relative parent and an absolute child
- keep a button or a bar on screen with position: fixed, and leave room for it
- make a heading stick at the top while its section is read, with position: sticky
- choose which of overlapping boxes lies on top with z-index, and know when z-index does nothing
The back cover
Most boxes sit in the normal flow, one after another, like people in a queue. Some need to step out of it: a badge pinned to a card's corner, a bar that stays on screen, a header that stops at the top as you scroll.
These pages show how the position property takes a box out of the flow. They cover relative for a nudge, absolute for a pin, fixed for the screen and sticky for an edge. Then they show z-index, which decides which box lies on top when boxes overlap.
Before you start
The story: The ribbon that flew away
The tea stall at the Fair has sold out, and the warden wants a red "Sold out" ribbon pinned across the top corner of its card. Corin pins one on. On the board it appears in the top corner of the whole board instead, far from the tea stall.
The warden has two more wishes. A small "Back to the top" lamp should stay in the corner of the glass while people scroll through the long list of stalls. And the list's heading should stay at the top while its stalls slide past beneath it.
Meher holds up a pin. "Everything so far has flowed, one box after another," she says. "Now you lift a piece out of the flow, and you must say what it is pinned to."
Everything so far has been placed by the flow of the page: boxes one after another, stacked, in rows, in grids. Sometimes one piece must step out of that flow: a ribbon pinned to the corner of a card, a button that stays on screen while you scroll, a heading that sticks at the top. The position property does that. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Block boxes stack in order. | Three divs, one under another. | That order is the normal flow, which position can leave. |
| Margins push boxes apart. | margin-top: 20px moves a box and the boxes after it. | Positioning moves one box without moving the others. |
| Elements nest inside each other. | A span inside a card inside a board. | A pinned box is placed against one of its ancestors. |
| The viewport is the visible window. | 100vh is its height. | A fixed box is pinned to the viewport. |
| Overflow and scrolling. | A long page scrolls; a box with overflow: auto scrolls inside. | Sticky boxes stick while their scroll area moves. |
| Overlaps are usually a mistake. | Spilled text drawn over the next paragraph. | Positioning makes deliberate overlaps, and z-index decides which is on top. |
| The box model. | Width, padding, border. | A positioned box keeps all its layers. |
| A pin on a noticeboard. | A note pinned to one corner of a poster. | absolute pins a box to a corner of its positioned parent. |
| Measuring what was drawn. | getBoundingClientRect(). | Every block checks where the box ended up, even after scrolling. |
By the end of this lesson you will be able to:
- say what the normal flow is, and nudge a box with position: relative without moving its neighbours;
- pin a box to a corner of its parent with position: absolute, and choose which parent it pins to;
- keep a box on screen while the page scrolls with position: fixed;
- make a heading stick at the top of its section with position: sticky;
- decide which of two overlapping boxes lies on top with z-index.
In one line
position lifts a box out of the page's normal flow and pins it to something: itself, a parent, the window, or a scroll edge; you need the box model and the idea of the viewport.
The normal flow, and nudging with relative
At a glance
People in a railway queue stand one behind another, each in their place. That is the normal flow of a page: every box placed after the one before it. Every element starts with position: static, which means in the flow, where it falls.
With position: relative, a box is drawn shifted by top, right, bottom or left, each pushing it away from that edge. But its place in the flow stays where it was, like a passenger who leans out of the queue without leaving it.
Take a guess: If a box has position: relative and top: 20px, does the box below it move down too?
Passengers in a queue at a railway ticket window stand one behind another, each in their place. That is the normal flow of a page: every box placed after the one before it, by the rules of the earlier lessons. Every element starts with position: static, which means "in the flow, where it falls".
position: relative: a nudge that keeps the place
Now one passenger leans a little to the left to see the timetable, without leaving the queue. Their place in the line is still theirs; nobody steps forward to fill it. That is position: relative: the box is drawn shifted by the offsets top, right, bottom and left, but its place in the flow stays where it was.
<!DOCTYPE html>
<style>
body { margin: 0; }
.stall { width: 100px; height: 40px; background-color: gold; }
.nudged { position: relative; top: 10px; left: 20px; background-color: teal; }
</style>
<div class="stall">Rice</div>
<div class="stall nudged">Dal</div>
<div class="stall">Atta</div>
Checked in Chrome
- Dal is drawn 20 right and 10 down, at 20, 50, while Atta stays at 80
- the nudged box is relative; the others are static, the default
Read the offsets carefully: top: 10px pushes the box 10 pixels away from its top edge, so it moves down. left: 20px pushes it away from its left edge, so it moves right. And Atta, the next box, did not move at all: as far as the flow is concerned, Dal is still in its old place, with a gap where it was.
A memory aid
An offset pushes away from its own edge. top: 10px moves a box down; left: 20px moves it right; a negative value pulls the other way.
That makes relative useful for small visual nudges, such as lifting an icon a pixel or two to line it up with its text. Its bigger job is the next section's: a relative box becomes the thing that absolute boxes inside it are pinned to.
Relative for layout
Nudging boxes around with relative offsets to build a layout leaves gaps where they were and overlaps where they go. Layout belongs to flow, flexbox and grid; relative is for small touches and for being a pinning point.
Worked example 1
A price tag's ₹ sign sits a little low beside the price. Lift just the sign 2px, without moving the price or anything else on the line.
What we have: one small element that should move a little
What we need: a nudge that leaves everything else in place
The plan: position: relative on the sign, with top: -2px to pull it up
<!DOCTYPE html>
<style>
body { margin: 0; }
.sign { position: relative; top: -2px; }
</style>
<p><span class="sign">₹</span><span class="amount">180</span></p>
Checked in Chrome
- the sign is pulled up 2px
- and the price still starts right after it, as if nothing moved
.sign { position: relative; top: -2px; }
Check: the sign moved, and the price kept its place right beside the sign's original spot.
Another way to keep a place: visibility
The box model lesson hid boxes in two ways, and they fit this idea of a kept place. With visibility: hidden the box is not drawn, yet its place in the flow is still held, like a passenger who steps behind a pillar without leaving the queue. With display: none the box is removed, and the boxes after it close up.
<!DOCTYPE html>
<style>
body { margin: 0; }
.stall { width: 100px; height: 40px; background-color: gold; }
.away { visibility: hidden; }
</style>
<div class="stall">Rice</div>
<div class="stall away">Dal</div>
<div class="stall">Atta</div>
Checked in Chrome
- Dal is not drawn, but Atta still sits at 80: the place is kept
- the hidden box reads visibility: hidden
| Rule | Drawn? | Its place in the flow |
|---|---|---|
position: relative with offsets | yes, shifted | kept |
visibility: hidden | no | kept |
display: none | no | removed: the next boxes close up |
When you are stuck: relative positioning
An offset pushes away from its own edge: top moves down, left moves right.
A gap left behind after moving a box: that is relative keeping its place; use margins or flexbox to really move it.
Nothing moves? Offsets do nothing on a static box: add position: relative.
Your turn
What is the default value of position?Tap to reveal
- In the flow, where it falls.
static.
A relative box has left: 30px. Which way does it move?Tap to reveal
- Away from its left edge.
Right, 30px.
Does moving a relative box move the boxes after it?Tap to reveal
- Its place in the flow stays.
No: they stay as if it had not moved.
Did you know?
Positioning was written by rivals together
Positioning was written jointly by rivals. A W3C Working Draft of 31 January 1997, Positioning HTML Elements with Cascading Style Sheets, names two authors. They are Scott Furman of Netscape Communications and Scott Isaacs of Microsoft, the makers of the two major browsers of the time, with an editor from Hewlett Packard. It introduced the new position property.
Why it fits here: This topic's position property, with relative and absolute, first appeared in that joint draft.
Sources: W3C: Positioning HTML Elements with Cascading Style Sheets (1997); Wikipedia: CSS box model
A nudge can overlap its neighbour
A relatively positioned box can land on top of its neighbour. The CSS 2.1 standard says that shifting a box this way has no effect on the box that follows: the next box is placed as if the first had not moved. So the two can overlap, and the space where the first box was stays empty.
Why it fits here: It is the other side of this topic's rule that a relative box keeps its place.
In one line
Every box starts static, in the normal flow; position: relative draws it shifted by top, right, bottom and left, each pushing away from its own edge, while its place in the flow stays put. Visibility: hidden keeps the place of a box it hides.
Pinning a box: position: absolute
At a glance
A shopkeeper pins a sold-out slip to the corner of one price card. That pin is position: absolute. The box leaves the normal flow, so the boxes around it close up as if it were not there.
It is placed by its offsets against its containing block: the nearest ancestor with a position other than static, or else the page. So give the parent position: relative to pin a box to it. The inset property sets all four offsets at once.
Take a guess: If a badge has position: absolute and its card has position: static, what is the badge placed against?
A shopkeeper pins a "Sold out" slip to the corner of one price card. The slip is not in the line of cards; it sits on top of one of them, at its corner, and moves with that card. position: absolute is that pin.
The box leaves the normal flow, so the boxes around it close up as if it were not there. It is placed by its offsets against its containing block: the nearest ancestor whose position is not static, or, if there is none, the page itself.
Out of the flow
<!DOCTYPE html>
<style>
body { margin: 0; }
.stall { width: 100px; height: 40px; background-color: gold; }
.pinned { position: absolute; top: 0; right: 0; background-color: teal; }
</style>
<div class="stall">Rice</div>
<div class="stall pinned">Dal</div>
<div class="stall">Atta</div>
Checked in Chrome
- Dal leaves the flow: Atta moves up to 40, straight after Rice
- and Dal is pinned to the top right corner of the page
Pinned to the right parent
To pin the slip to one card rather than to the page, make that card the containing block: give it position: relative. With no offsets it does not move at all, but now the absolute boxes inside it are placed against it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { position: relative; width: 300px; height: 100px; margin: 50px; background-color: lightyellow; }
.ribbon { position: absolute; top: 0; right: 0; width: 80px; background-color: maroon; color: white; }
</style>
<div class="card">
Masala tea, ₹15
<span class="ribbon">Sold out</span>
</div>
Checked in Chrome
- the ribbon's right edge is the card's right edge
- and its top is the card's top: pinned to the card's corner
A memory aid
Relative parent, absolute child. To pin a box to a corner of something, give the something position: relative, and the box position: absolute with its offsets.
Without that one line on the card, the ribbon flies to the corner of the page, which is exactly Corin's slip in the story. The mistakes section measures it.
inset: all four offsets at once
inset sets top, right, bottom and left together, with the same one-to-four values as padding. inset: 0 stretches an absolute box over its whole containing block; inset: 10px leaves a 10px border of space around it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.photo { position: relative; width: 200px; height: 100px; background-color: teal; }
.caption { position: absolute; inset: 10px; background-color: rgb(255 255 255 / 70%); }
</style>
<div class="photo"><div class="caption">Nashik onion market</div></div>
Checked in Chrome
- 10px in from the photo's top left
- and 10px in from every side: 180 by 80
Centring a pinned box
An absolute box with a width and a height, inset: 0 and margin: auto sits in the exact centre of its containing block: the auto margins share the space on every side.
<!DOCTYPE html>
<style>
body { margin: 0; }
.screen { position: relative; width: 400px; height: 200px; background-color: lightyellow; }
.badge { position: absolute; inset: 0; margin: auto; width: 100px; height: 50px; background-color: gold; }
</style>
<div class="screen"><div class="badge">Sale</div></div>
Checked in Chrome
- centred: (400 − 100) ÷ 2 = 150 across, (200 − 50) ÷ 2 = 75 down
Worked example 2
A product photo, 240 by 160, needs a small "New" badge pinned 8px in from its top left corner, and the photo's caption pinned along its bottom edge, the full width. Write the rules and work out where each lands.
What we have: a photo box, a badge for one corner, a caption for the bottom
What we need: the containing block, and offsets for the two pinned boxes
The plan: position: relative on the photo; the badge absolute at top 8 and left 8; the caption absolute at bottom 0, left 0 and right 0
<!DOCTYPE html>
<style>
body { margin: 0; }
.photo { position: relative; width: 240px; height: 160px; background-color: teal; }
.new { position: absolute; top: 8px; left: 8px; background-color: gold; }
.caption { position: absolute; bottom: 0; left: 0; right: 0; height: 30px; background-color: white; }
</style>
<div class="photo">
<span class="new">New</span>
<div class="caption">Basmati rice, 5 kg</div>
</div>
Checked in Chrome
- the badge sits 8 in from the top left
- the caption runs the full 240 along the bottom, ending at 160
.photo { position: relative; } .new { position: absolute; top: 8px; left: 8px; } .caption { position: absolute; bottom: 0; left: 0; right: 0; }
Check: both boxes landed where the offsets said, measured against the photo.
When you are stuck: absolute positioning
A pinned box in the wrong place: is its parent positioned? Give the parent position: relative.
Neighbours closing up into the space: that is absolute leaving the flow, as it should.
To stretch a pinned box: opposite offsets together (left: 0; right: 0), or inset: 0.
Your turn
An absolute box has no positioned ancestor. What is it placed against?Tap to reveal
- The last resort.
The page itself.
What does inset: 0 do to an absolute box?Tap to reveal
- All four offsets at 0.
Stretches it over its whole containing block.
What one line makes a card the thing a ribbon inside it is pinned to?Tap to reveal
- It does not move the card.
position: relative on the card.
Did you know?
No positioned parent? A box the size of the screen
With no positioned ancestor, an absolute box is placed against the initial containing block. CSS 2.1 says that block has the size of the viewport and sits at the top of the page. So bottom: 0 puts the box at the bottom of the first screenful, not at the bottom of a long page.
Why it fits here: This topic says such a box is placed against the page; this is what that box really is.
inset is read like margin
The standard now calls top, right, bottom and left the inset properties. Their shorthand, inset, gives each side its value exactly as margin does: one value for all four sides, or up to four going round clockwise from the top. So inset: 0 pins all four edges at once.
Why it fits here: This topic's inset: 0 is this shorthand, read the way margin is read.
In one line
position: absolute lifts a box out of the flow and places it by its offsets against the nearest positioned ancestor, or the page; give the parent position: relative to pin to it, and use inset for all four sides.
Staying on screen: position: fixed
At a glance
A railway coach has a sign by its door that stays put however far the train travels. With position: fixed, a box is pinned to the viewport, the window you look through, so it stays in the same place on the screen while the page scrolls under it.
A fixed box takes no space in the flow, like an absolute one. So a fixed bar at the top would cover the first lines of the page, unless the page is given room for it, such as a padding as tall as the bar.
Take a guess: If you scroll a page 800 pixels down, how far does a fixed button move on the screen?
A railway coach has a sign by the door, "Emergency chain", painted on the wall. However far the train travels, the sign stays exactly where it is in the coach. position: fixed pins a box to the viewport, the window you look through, so it stays in the same place on the screen while the page scrolls under it.
<!DOCTYPE html>
<style>
body { margin: 0; }
.long { height: 3000px; }
.to-top { position: fixed; right: 10px; bottom: 10px; width: 80px; height: 30px; background-color: gold; }
</style>
<div class="long">A long list of stalls...</div>
<a class="to-top" href="#">Back to top</a>
Checked in Chrome
- the button sits 10px above the bottom of the window
- and after scrolling 800px down the page, it is still 10px above the bottom
The check scrolls the page 800 pixels and measures again: the button has not moved on the screen. Its offsets, right and bottom, are measured from the edges of the window, not of any parent.
What fixed boxes are used for
| Use | Pinned to | Example |
|---|---|---|
| a button that is always reachable | a bottom corner | "Back to top", or a chat button |
| a bar that never scrolls away | the top, with left: 0 and right: 0 | a top menu on some sites |
| a notice across the bottom | the bottom edge | a cookie or offline notice |
| a dialog over the page | the centre, with inset: 0 and margin: auto | a sign-in box over a dimmed page |
A fixed bar that hides the content
A fixed box is out of the flow, so the page does not make room for it: a fixed bar at the top covers the first lines of the page underneath. Give the page a matching space, such as padding-top equal to the bar's height, so nothing is hidden at the start.
<!DOCTYPE html>
<style>
body { margin: 0; padding-top: 50px; }
.bar { position: fixed; top: 0; left: 0; right: 0; height: 50px; background-color: navy; color: white; }
</style>
<div class="bar">Asha Stores</div>
<h1 class="first">Fresh today</h1>
Checked in Chrome
- the bar spans the window, from left: 0 to right: 0
- and the body's padding keeps the first heading below the 50px bar, not hidden under it
Fixed boxes and small screens
On a phone, every fixed box takes a share of a small screen for the whole visit. Keep them few and small, and let a visitor close one that is not needed, such as a notice.
Worked example 3
A delivery site wants a "Call us" button always in the bottom left corner, 16px from both edges, 120 by 40. Write it, and say where its top-left corner is in an 800 by 600 window, before and after scrolling.
What we have: a button pinned to the bottom left of the window
What we need: the rule, and its position before and after scrolling
The plan: position: fixed with left: 16px and bottom: 16px; the window's height decides its top
- Left edge: 16.
- Top edge: the window's height minus 16 minus 40: 600 − 16 − 40 = 544 in an 800 by 600 window.
- Scrolling changes nothing: fixed boxes stay put on the screen.
<!DOCTYPE html>
<style>
body { margin: 0; }
.long { height: 3000px; }
.call { position: fixed; left: 16px; bottom: 16px; width: 120px; height: 40px; background-color: teal; color: white; }
</style>
<div class="long">Our menu...</div>
<a class="call" href="tel:+919876543210">Call us</a>
Checked in Chrome
- 16 from the left, and 16 + 40 = 56 from the bottom
- the same after scrolling 800px
.call { position: fixed; left: 16px; bottom: 16px; }: at 16, 544 in an 800 by 600 window, before and after scrolling.
Check: the check measured the same place before and after the page scrolled.
Worked example 4
A shop's sign-in box, 300 by 200, must appear in the centre of the window over a dimmed page, and stay there if the page behind it scrolls. Write the two boxes, and work out where the sign-in box lands in an 800 by 600 window.
What we have: a dimming layer for the whole window, and a box for its centre
What we need: both boxes pinned to the window, the box centred and on top
The plan: both fixed; the layer with inset: 0 to cover the window; the box with inset: 0 and margin: auto, as for centring an absolute box
- The layer: fixed with inset: 0, so its four edges sit on the window's four edges.
- The box: fixed with a width and a height, inset: 0 and margin: auto, so the auto margins share the spare space equally.
- Across: (800 − 300) ÷ 2 = 250. Down: (600 − 200) ÷ 2 = 200.
- The box comes after the layer in the HTML, so it lies on top without a z-index.
<!DOCTYPE html>
<style>
body { margin: 0; }
.dim { position: fixed; inset: 0; background-color: rgb(0 0 0 / 50%); }
.signin { position: fixed; inset: 0; margin: auto; width: 300px; height: 200px; background-color: white; }
</style>
<p>Today's offers...</p>
<div class="dim"></div>
<div class="signin">Sign in to Asha Stores</div>
Checked in Chrome
- the dimming layer covers the whole window
- the box is centred: 250, 200 in an 800 by 600 window
- and at the centre, the sign-in box lies on top of the layer
.dim { position: fixed; inset: 0; } .signin { position: fixed; inset: 0; margin: auto; width: 300px; height: 200px; }: the box lands at 250, 200.
Check: the browser drew the layer over the whole window and found the box on top at the centre.
When you are stuck: fixed positioning
Offsets of a fixed box are measured from the window's edges.
Content hidden under a fixed bar: give the page padding equal to the bar's height.
A fixed box that scrolls away after all: a parent with a transform or a filter can trap it, a detail for the transitions lesson.
Your turn
What is a fixed box pinned to?Tap to reveal
- The window you look through.
The viewport.
Does a fixed box move when the page scrolls?Tap to reveal
- The emergency sign in the coach.
No: it stays in the same place on the screen.
Why can a fixed top bar hide the first heading?Tap to reveal
- Does the page make room for it?
Because it is out of the flow, so the page does not leave space for it.
Did you know?
A transform can unfix a fixed box
A fixed box can stop being fixed. The CSS Transforms standard says that any transform on an element makes it the containing block for all its descendants, fixed ones included. So a fixed button inside a parent that has a transform is pinned to that parent instead, and it scrolls away with it.
Why it fits here: When this topic's fixed button scrolls away, a transform on a parent is the first thing to check.
Source: W3C: CSS Transforms Level 1
Fixed boxes print on every page
Print a page, and a fixed box does something new. CSS 2.1 says that, for paged media, boxes with fixed positions are repeated on every page. The standard even suggests a use for it: placing a signature at the bottom of each page.
Why it fits here: It is what this topic's fixed bar does on paper instead of on a screen.
In one line
position: fixed pins a box to the viewport by its offsets, so it stays in place while the page scrolls; it takes no space in the flow, so give the page room for a fixed bar.
Sticking at the edge: position: sticky
At a glance
In a long train timetable you keep losing the header row as you read down. A sticky header slides down with you and stops at the top of your view. With position: sticky, a box stays in the normal flow until scrolling would carry it past its offset, such as top: 0.
Then it holds the box there, but only while its parent is still in view; when the parent scrolls away, the box goes with it. Without an offset, or without room in its parent, it never sticks.
Take a guess: If a sticky header has no top value, does it stick when you scroll?
Think of a long printed list of trains with a header row: Train, From, Departs. In a long timetable you keep losing the header as you read down. Now imagine the header slides down the page with you and stops at the top of your view, staying there until its own section ends. That is position: sticky.
The box stays in the normal flow, like a relative box, until scrolling would carry it past an edge given by an offset, such as top: 0. From then on it is held at that edge, but only while its parent is still in view.
<!DOCTYPE html>
<style>
body { margin: 0; }
.intro { height: 100px; }
.heading { position: sticky; top: 0; margin: 0; height: 30px; background-color: gold; }
.list { height: 3000px; }
</style>
<div class="intro">Welcome to the Fair</div>
<h2 class="heading">Stalls</h2>
<div class="list">Rice, Dal, Atta, and many more...</div>
Checked in Chrome
- before scrolling, the heading sits in the flow, 100px down
- after scrolling 500px, it is held at the top of the window, top: 0
Unlike fixed, a sticky box takes up its space in the flow, so nothing is hidden under it at the start. And unlike fixed, it is held only while its section is on screen: in a page of several sections, each sticky heading takes over from the last.
Sticky needs an offset, and its parent
| Without | What happens |
|---|---|
an offset such as top: 0 | nothing: the box never knows where to stick |
| room in its parent | a sticky box cannot leave its parent, so if the parent is no taller than the box, it has nowhere to slide |
| a scrolling page or box | a parent with overflow: hidden or auto becomes the area it sticks inside, which often stops it sticking to the window |
<!DOCTYPE html>
<style>
body { margin: 0; }
section { height: 600px; }
h2 { position: sticky; top: 0; margin: 0; height: 30px; background-color: gold; }
</style>
<section class="veg"><h2>Vegetables</h2></section>
<section class="grain"><h2>Grains</h2></section>
<div style="height: 2000px"></div>
Checked in Chrome
- scrolled 300px, the Vegetables heading is stuck at the top
- scrolled 800px, Vegetables has left with its section, and Grains is stuck in its place
Each heading sticks only inside its own section. When the Vegetables section scrolls away, its heading goes with it, and the Grains heading takes the top.
A memory aid
Relative until the edge, then fixed until its parent ends. Sticky is the two of them in turn.
Worked example 5
A long price list is divided into sections, each with a heading. The headings should stay visible at the top while their section is being read, below a 50px fixed bar. Write the heading rule, and say where a heading stops.
What we have: section headings, a 50px fixed bar at the top
What we need: a sticky rule with the right offset
The plan: position: sticky with top: 50px, so a heading stops just below the bar instead of under it
<!DOCTYPE html>
<style>
body { margin: 0; padding-top: 50px; }
.bar { position: fixed; top: 0; left: 0; right: 0; height: 50px; background-color: navy; }
section { height: 800px; }
section h2 { position: sticky; top: 50px; margin: 0; background-color: gold; }
</style>
<div class="bar"></div>
<section><h2>Spices</h2></section>
<div style="height: 2000px"></div>
Checked in Chrome
- scrolled 400px, the heading stops at 50, just below the bar
section h2 { position: sticky; top: 50px; }: each heading stops 50px from the top, below the bar.
Check: after scrolling, the heading was held at exactly 50px, below the fixed bar rather than hidden under it.
When you are stuck: sticky positioning
Not sticking at all: add an offset such as top: 0.
Sticking only briefly: the parent is barely taller than the sticky box; it can only slide within its parent.
Not sticking to the window: an ancestor with overflow other than visible has become its scroll area.
Your turn
What must a sticky box have to stick?Tap to reveal
- Where should it stop?
An offset, such as top: 0.
Does a sticky box take space in the flow?Tap to reveal
- Relative until the edge.
Yes: unlike fixed, it keeps its place.
When does a sticky heading stop sticking?Tap to reveal
- It cannot leave its parent.
When its parent scrolls out of view.
Did you know?
Sticky only ever pushes one way
A sticky box with top: 0 only ever moves down. The Positioned Layout standard says an element with a top value and no bottom value will only ever be pushed down by sticky positioning, never upwards. It also notes that several sticky boxes in one container move independently, so they may overlap.
Why it fits here: This topic's top: 0 is the case the standard describes: the box is only ever pushed down.
A sticky box never leaves its parent
A sticky box never leaves its parent. The Positioned Layout standard moves a sticky box towards the edge of the view only insofar as it can while the box stays inside its containing block. That is why a sticky header stops sticking, and scrolls away, when the end of its own section arrives.
Why it fits here: It is the reason this topic's sticky box holds only while its parent is in view.
In one line
position: sticky keeps a box in the flow until scrolling reaches its offset, such as top: 0, then holds it there while its parent is in view; it needs an offset and room in its parent.
Which box lies on top: z-index
At a glance
Drop two playing cards so that they overlap, and the one dropped second lies on top. Positioned boxes pile up the same way: the one that comes later in the HTML is drawn on top. The z-index property changes that order, and a higher number lies on top.
z-index works on positioned boxes. And a parent's z-index is a ceiling: a child can never rise above a box that its parent sits under, however high the child's own number. Inside the parent, the child's number still orders it among its siblings.
Take a guess: If a child has z-index: 999 inside a parent with z-index: 1, can it rise above the parent's sibling with z-index: 2?
Drop two playing cards on a table so that they overlap. The one dropped second lies on top. Pages work the same way: when positioned boxes overlap, the one that comes later in the HTML is drawn on top, unless you say otherwise. A page has depth as well as width and height.
<!DOCTYPE html>
<style>
body { margin: 0; }
.table { position: relative; width: 200px; height: 120px; }
.card { position: absolute; width: 100px; height: 80px; }
.first { top: 0; left: 0; background-color: gold; }
.second { top: 30px; left: 40px; background-color: teal; }
</style>
<div class="table">
<div class="card first">Rice</div>
<div class="card second">Dal</div>
</div>
Checked in Chrome
- where the two overlap, the later box, Dal, is on top
The check asks the browser which element is at the point 60, 50, inside the overlap, as a click there would find. It answers the second card.
z-index: choosing the order
z-index is a number for a box's place in the pile: higher numbers lie on top of lower ones. It works on positioned boxes (relative, absolute, fixed, sticky), and on flex and grid items.
<!DOCTYPE html>
<style>
body { margin: 0; }
.table { position: relative; width: 200px; height: 120px; }
.card { position: absolute; width: 100px; height: 80px; }
.first { top: 0; left: 0; background-color: gold; }
.second { top: 30px; left: 40px; background-color: teal; }
.first { z-index: 2; }
.second { z-index: 1; }
</style>
<div class="table">
<div class="card first">Rice</div>
<div class="card second">Dal</div>
</div>
Checked in Chrome
- with z-index 2 against 1, Rice is now on top, though it comes first
- its z-index reads 2
z-index on an ordinary box
z-index on a static box, one with no position, does nothing at all. If z-index seems to be ignored, the box is probably not positioned: add position: relative.
<!DOCTYPE html>
<style>
body { margin: 0; }
.low { width: 100px; height: 60px; margin-bottom: -40px; background-color: gold; z-index: 5; }
.high { position: relative; width: 100px; height: 60px; margin-left: 20px; background-color: teal; }
</style>
<div class="low">Rice</div>
<div class="high">Dal</div>
Checked in Chrome
- (the slip: the gold box's z-index of 5 is ignored, because it is static; the positioned box lies on top)
A parent's z-index is a ceiling
A positioned box with a z-index makes a little pile of its own, called a stacking context. Everything inside it is sorted within that pile, and the whole pile is placed as one in the page. So a child with z-index 1000 still cannot rise above a box that lies on top of its parent.
| Number | Where it lands |
|---|---|
| no z-index (auto) | in the HTML's order, later on top |
z-index: 1 and up | above the auto boxes, higher numbers higher |
z-index: -1 | below the auto boxes, behind the normal content |
A memory aid
Later on top, unless z-index says otherwise, and only for positioned boxes. Keep z-index numbers small and spaced (1, 10, 100), so there is room to slip a layer in between.
Worked example 6
A product card has a photo and, pinned over its corner, a "New" badge, both absolute inside the card. The badge comes first in the HTML, so the photo covers it. Fix it with z-index, and check which is on top at the badge's corner.
What we have: two absolute boxes, the badge first, overlapping
What we need: the badge on top
The plan: give the badge a higher z-index than the photo; both are positioned, so z-index applies
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { position: relative; width: 200px; height: 150px; }
.badge { position: absolute; top: 0; left: 0; width: 50px; height: 24px; background-color: gold; z-index: 1; }
.photo { position: absolute; inset: 0; background-color: teal; }
</style>
<div class="card">
<span class="badge">New</span>
<div class="photo"></div>
</div>
Checked in Chrome
- at the corner, the badge is on top of the photo
- elsewhere, the photo shows
.badge { z-index: 1; }: higher than the photo's auto, so the badge lies on top.
Check: the browser finds the badge at the corner and the photo elsewhere.
When you are stuck: layers
Which is on top? By default the later box; a higher z-index beats that.
z-index ignored: the box is static; give it position: relative.
A child that will not come above another box, whatever its z-index: its parent's own z-index is the ceiling.
Your turn
Two positioned boxes overlap and neither has a z-index. Which is on top?Tap to reveal
- Cards dropped on a table.
The one that comes later in the HTML.
Does z-index work on a static box?Tap to reveal
- Only some boxes take part.
No: it needs a position (or to be a flex or grid item).
Why keep z-index numbers spaced, such as 1, 10, 100?Tap to reveal
- Think of adding a layer later.
To leave room to slip a layer in between.
Did you know?
A see-through box starts its own pile
Making a box slightly see-through changes its layers. The CSS Color standard says an element with opacity less than 1 is drawn from a single image, so nothing outside it can slip in between the pieces inside it. That is why such an element creates a new stacking context: its children's z-index values now count only among themselves.
Why it fits here: It is another way to create this topic's ceiling, with no z-index written at all.
Source: W3C: CSS Color Level 3
Flex and grid items need no position for z-index
z-index usually needs a positioned box, but flex and grid items are an exception. The Flexbox and Grid standards both say that on their items, a z-index other than auto creates a stacking context even if position is static, behaving as if position were relative.
Why it fits here: It is the one place where this topic's z-index works without any position.
In one line
Overlapping boxes are piled in HTML order, later on top; z-index, on positioned boxes, sets the pile's order, higher on top, and a parent's z-index is a ceiling for everything inside it.
Where it is used
Positioning is used sparingly, for the pieces that must sit outside the flow.
| What you learned | Where it is used | A real example |
|---|---|---|
| relative + absolute | badges, ribbons, captions on photos | a "20% off" badge pinned to the corner of each product photo |
| absolute with inset: 0 | overlays | a dark see-through layer over a photo, with its caption on top |
| fixed | always-reachable buttons and bars | a chat button in the bottom corner of a bank's site |
| sticky | headings and table headers | the header row of a long price table that stays visible while scrolling |
| z-index | anything that overlaps | a menu that drops down over the page instead of under it |
| fixed with inset: 0 and margin: auto | dialogs | a sign-in box centred over a dimmed page |
Mistakes and debugging
These are the positioning slips everyone makes, each measured in the browser.
1. The ribbon that flies to the corner of the page
<!DOCTYPE html>
<style>
body { margin: 0; }
.card { width: 300px; height: 100px; margin: 50px; background-color: lightyellow; }
.ribbon { position: absolute; top: 0; right: 0; background-color: maroon; color: white; }
</style>
<div class="card">Masala tea <span class="ribbon">Sold out</span></div>
Checked in Chrome
- (the slip: the card is not positioned, so the ribbon is pinned to the page's top right corner, far from the card)
The fix: .card { position: relative; }, so the card becomes the ribbon's containing block.
2. Content hidden under a fixed bar
A fixed top bar covers the first lines of the page. The fix: padding on the body equal to the bar's height.
3. A sticky box with no offset
position: sticky alone does nothing. The fix: top: 0, or the edge it should stick to.
4. z-index on a static box
The number is ignored. The fix: give the box a position, such as relative.
5. A whole layout built with absolute
Every box pinned at fixed offsets looks right on one screen and overlaps on every other, because absolute boxes do not make room for each other. The fix: flow, flexbox and grid for layout; absolute only for pieces laid over others.
6. An absolute box that loses its size
A div set to absolute shrinks to fit its content, because it no longer stretches across its parent the way a block in the flow does. The fix: give it a width, or stretch it with opposite offsets such as left: 0 and right: 0.
The debugging corner: what is it pinned to?
Select a positioned box in DevTools' Elements panel, then look in the Computed tab at position and the four offsets. To find its containing block, walk up the elements above it and look for the first whose position is not static; the page highlights each box as you hover over it. For layers, the Layers panel (in the More tools menu) shows the page's piles in three dimensions.
Quick card
The lesson on one card.
Every idea of the lesson
<!DOCTYPE html>
<style>
body { margin: 0; padding-top: 40px; }
.bar { position: fixed; top: 0; left: 0; right: 0; height: 40px; background-color: navy; color: white; z-index: 10; }
.card { position: relative; width: 300px; height: 120px; background-color: lightyellow; }
.ribbon { position: absolute; top: 0; right: 0; background-color: maroon; color: white; z-index: 1; }
.heading { position: sticky; top: 40px; margin: 0; background-color: gold; }
.nudge { position: relative; top: -2px; }
.long { height: 2000px; }
</style>
<div class="bar">Asha Stores</div>
<div class="card">Masala tea <span class="nudge">₹15</span> <span class="ribbon">Sold out</span></div>
<section class="long">
<h2 class="heading">All stalls</h2>
</section>
Checked in Chrome
- the ribbon is pinned to the card's top right corner
- the body's padding keeps the card below the fixed bar
- the price is nudged up 2px with relative
- scrolled, the heading sticks just below the bar
- and the bar, fixed with z-index 10, stays at the top, over everything
| position | In the flow? | Placed against | Use it for |
|---|---|---|---|
static | yes | nothing: where it falls | almost everything |
relative | yes, its space kept | its own place | small nudges; being a pinning point |
absolute | no | the nearest positioned ancestor, or the page | badges, ribbons, overlays |
fixed | no | the viewport | always-visible buttons and bars |
sticky | yes, until its offset | a scroll edge, within its parent | headings that stay in view |
And top, right, bottom, left (or inset) place the box; z-index, on positioned boxes, decides the order of the pile.
The story: Pinned to the right thing
You tell the tea stall's card that it is the thing to pin to, and the ribbon settles across its own top corner, wherever the card moves on the board.
The lamp is pinned to the glass itself, so it stays in its corner while the list scrolls. The heading sticks to the top of its list once you scroll past it. Where the ribbon and the heading overlap, you choose which lies on top.
Corin moves the tea stall to the other end of the board. The ribbon goes with it. "It knows where it belongs now," he says.
Reagents gathered: position, relative, absolute, fixed, sticky, z-index.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)