Sub topic 8
Sticky Positioning: Scrolling Then Sticking
This chapter assumes you know the position property's other values (static, relative, absolute, fixed), the box model, and the idea of a containing block. It builds on relative positioning and fixed positioning, so skim those chapters first if they are unfamiliar.
What sticky does
position: sticky combines two behaviors. An element starts out in normal flow, like a relatively positioned element. Once scrolling would carry it past a threshold you set, it stops moving and stays at that threshold. It keeps that position until its parent element scrolls out of view, and then it leaves with the parent.
Fixed positioning pins an element to the viewport permanently and removes it from the flow. Sticky is different. The element always keeps its place in the flow, and it only pins itself while the scroll position makes that useful.
.site-header {
position: sticky;
top: 0;
background: white;
}The header scrolls with the page until its top edge reaches the top edge of its scroll container. Then it stays there while its parent is still in view.
The working model
Browsers handle sticky in three steps.
- Layout. The element is laid out in normal flow. Its original box takes up space, and siblings are placed around it as if it were
position: relativewith no offsets. - Scroll check. On each scroll, the browser compares the element's position with the scrollport, the visible rectangle of the nearest scrolling ancestor. It also applies your offsets (
top,bottom,left,right) as insets from the scrollport edges. - Shift. If the normal position would violate an offset, the browser shifts the element just enough to satisfy it. The shift can never move the element outside its containing block, which is the content box of its parent.
The third step explains why a sticky element eventually scrolls away. The parent's bottom edge pushes it out of view.
The offset is required
Sticky does nothing without at least one offset on the axis you care about. With position: sticky alone, the element behaves exactly like position: relative with no shift.
| Declaration | Effect |
|---|---|
| `top: 0` | Sticks to the top edge of the scrollport when scrolling down |
| `top: 4rem` | Sticks 4rem below the top edge, which leaves room for a fixed navbar |
| `bottom: 0` | Sticks to the bottom edge. This works for footers or action bars that should stay visible before the user scrolls to them |
| `left: 0` | Sticks to the left edge during horizontal scrolling, for example a first column in a wide table |
You can combine offsets, such as top: 0 and left: 0 on a corner cell of a table that scrolls in both directions. Each axis is handled independently.
Unlike relative positioning, sticky offsets are not distances to move the element by. They are limits. top: 10px means "do not let my top edge go above 10px from the scrollport's top." The element moves only when it would otherwise break that limit.
A complete example
This page has a sticky section heading inside each section. Save it and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sticky headings</title>
<style>
body { margin: 0; font-family: sans-serif; }
section { padding: 0 1rem 2rem; }
section h2 {
position: sticky;
top: 0;
margin: 0;
padding: 0.5rem 0;
background: white;
border-bottom: 1px solid #ccc;
}
p { height: 200px; margin: 1rem 0; background: #eee; }
</style>
</head>
<body>
<section>
<h2>Section one</h2>
<p>Content</p><p>Content</p><p>Content</p>
</section>
<section>
<h2>Section two</h2>
<p>Content</p><p>Content</p><p>Content</p>
</section>
</body>
</html>As you scroll, "Section one" sticks to the top. When the bottom of the first section reaches the heading, the heading is pushed out with its section. "Section two" then takes its turn. Each heading is limited by its own parent, which gives this stacked-heading effect without any JavaScript.
Space stays in the flow
A sticky element always reserves its original space. When it is stuck, the gap where it would have been remains, so nothing below it jumps. This differs from position: fixed, where the element leaves the flow and later content moves up underneath it.
The stuck element can also overlap content that scrolls past it. That is intended, because it is what you want from a header. It also means the header needs an opaque background, or the content shows through. It usually needs a z-index as well.
Sticky and stacking
A sticky element always creates a stacking context, as fixed elements do. A positioned element with z-index: auto is painted in the positioned layer, in DOM order. A later positioned sibling, such as a card with position: relative, can therefore paint over your stuck header. The fix is to give the header a z-index, for example z-index: 10. If the header is still covered, check whether an ancestor's stacking context is capping it. The stacking contexts chapter covers that case.
Why sticky fails: the common errors
Most reports of "sticky not working" come from a short list of causes. Work through them in order.
1. No offset
The element has position: sticky but no top, bottom, left, or right. Add one.
2. An ancestor creates a scroll container that does not scroll
Sticky is measured against the nearest ancestor that has a scrolling mechanism. Setting overflow: hidden, auto, or scroll on any ancestor makes it the scroll container. This applies even when that ancestor never actually scrolls because it has no fixed height. The sticky element then tracks that box instead of the viewport, and it never has anywhere to stick.
/* Breaks sticky inside .page, because .page is now the scroll container */
.page {
overflow: hidden;
}
/* Often fixes it. clip cuts off overflow without creating a scroll container */
.page {
overflow: clip;
}overflow: clip is supported in current browsers. If you added overflow: hidden only to contain floats or hide overflow, replace it. Search every ancestor up to body, since the culprit is often several levels up. The same applies to overflow-x and overflow-y: setting either one to a non-visible value turns the other axis from visible into auto.
3. The parent is too short
The sticky element can only move within its parent's content box. If the parent is exactly as tall as the element, there is no room to move, and the element looks like it is not sticking. A common version of this is a sticky element inside a small wrapper div that holds only the header. Move the element to a taller parent, such as the body, a page wrapper, or the section it belongs to.
4. Flex or grid stretching
In a flex row or a grid, items are stretched to the container's height by default (align-items: stretch). A sticky sidebar then has the same height as its parent and no room to move. Set align-self: start on the sidebar so it only takes its content height.
.layout {
display: flex;
gap: 2rem;
}
.sidebar {
position: sticky;
top: 1rem;
align-self: flex-start; /* without this the sidebar is stretched full height */
width: 14rem;
}
.content {
flex: 1;
}5. The wrong threshold for the scroller
A sticky element inside a scrolling div sticks to that div's edges, not the window's. If your header sticks at the top of the box but you wanted it under a fixed navbar, either add the navbar's height to top or move the scrolling responsibility to the page.
6. Covered by later content
The element sticks, but other content paints over it. This is the z-index case above.
Sticky table headers
You can make a table's header row stay visible by setting sticky on the cells rather than the row. Applying it to th elements is the most reliable approach across browsers.
thead th {
position: sticky;
top: 0;
background: #f4f4f4;
z-index: 1;
}Two details matter here. The table needs to be inside a container whose scrolling you want, so the same ancestor overflow rules apply. With border-collapse: collapse, borders belong to the table rather than the cells and do not travel with a stuck header. Use border-collapse: separate; border-spacing: 0 and put borders on the cells if you need the lines to stay with the header.
Debugging checklist
- Confirm
position: stickyis applied, using the browser's computed styles panel. - Confirm an offset is set on the axis you expect.
- Walk up the ancestors and look for
overflowset to anything butvisibleorclip. - Check the parent's height. It must exceed the sticky element's height by the distance you want it to travel.
- If the element is in a flex or grid container, check
align-self. - If it sticks but is hidden, check
z-indexand background.
When to choose sticky
Use sticky when the element should live in the normal flow and only pin itself as the page scrolls. Typical cases are section headings, table headers, and sidebars. Use fixed when the element should always be pinned to the viewport and not take space from the flow, as with a modal backdrop. If the element only needs to be pinned for part of the page, sticky is also a better choice than a JavaScript scroll listener, because the browser handles the work during scrolling.
Exercise
A sidebar has position: sticky; top: 1rem; and sits in a flex container with the default align-items. It scrolls away with the page and never sticks. Give two likely causes and the fix for each.
Show answer
First, the flex default align-items: stretch makes the sidebar as tall as its container, so it has no room to move inside its parent. Fix it with align-self: flex-start on the sidebar. Second, an ancestor may have overflow: hidden, auto, or scroll, which makes that ancestor the scroll container so the sidebar sticks relative to it and never to the viewport. Remove the overflow, or change it to overflow: clip.
Exercise
A sticky heading h2 with top: 0 is the only child of a div that wraps just the heading. The div is the same height as the heading, so the heading never sticks. Why, and how do you fix it?
Show answer
A sticky element can only move within its parent's content box. The wrapper div is exactly as tall as the heading, so the heading has no room to travel. Fix it by placing the heading in a taller parent, such as the section that holds its content, or by removing the wrapper.