Sub topic 8
Sticky positioning: scrolling then sticking
This chapter assumes you know basic HTML and CSS selectors, the box model, and units such as px, %, and rem. It also assumes you have read the earlier chapters on normal flow, offsets, relative positioning, and containing blocks.
What sticky does
position: sticky is a hybrid. The element starts out like a relative element: it sits in normal flow and keeps its space. Once you scroll far enough that the element would cross a threshold you set with an offset, it stops moving and stays put. When its parent scrolls out of view, the element leaves with it.
You need two things for sticky to work:
position: sticky- At least one offset (
top,right,bottom,left, orinset) that sets the threshold
Without an offset, the element never has a threshold, so it behaves like a normal element.
.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
Sticky positioning involves two boxes besides the element itself:
- The scroll container. This is the nearest ancestor that scrolls. Often it is the viewport, but it can be any ancestor with
overflowset toauto,scroll, orhidden. The offset is measured against the edges of this box. - The containing block. This is the element's parent box. The sticky element can never leave it. When the parent's bottom edge reaches the sticky element, the element is pushed up and scrolls away with the parent.
The element keeps its original space in the flow, so nothing around it jumps when it sticks.
A worked example: section headings
Sticky shines for headings in a long list, such as a contacts page. Each heading sticks while its own section is visible. The next heading pushes it away.
<div class="list">
<section>
<h2>A</h2>
<p>Alice</p><p>Aaron</p><p>Amir</p>
</section>
<section>
<h2>B</h2>
<p>Bella</p><p>Boris</p><p>Bruno</p>
</section>
</div>
<style>
.list { height: 200px; overflow-y: auto; }
section h2 {
position: sticky;
top: 0;
margin: 0;
background: #eee;
}
section p { margin: 0; padding: 1rem; }
</style>Here each h2 has the section as its parent. It sticks to the top of .list, which is the scroll container. When the section ends, the heading leaves with it, and the next section's heading takes over. Sticky elements need a background, or the content scrolling underneath will show through.
Sticky versus fixed
| Property | `fixed` | `sticky` |
|---|---|---|
| Takes space in the flow | No | Yes |
| Measured against | Viewport | Nearest scroll container |
| Limited by parent | No | Yes |
| Starts in its natural place | No | Yes |
Use fixed for things that must always be visible, such as a modal backdrop. Use sticky for things that belong to a section of the page.
Why sticky sometimes does not stick
Sticky is the position value that most often fails silently. Check these causes in order:
- No offset. Add
top: 0or another offset. - An ancestor has
overflowset. If any ancestor between the element and the viewport hasoverflow: hidden,auto, orscroll, that ancestor becomes the scroll container. If it does not actually scroll, the element never sticks. Remove theoverflowor move the sticky element. - The parent is too short. The element can only move inside its parent. If the parent is the same height as the element, there is no room to travel.
- A flex or grid parent stretches the item. With
align-items: stretch, the sticky item fills the parent's height and has no room to move. Setalign-self: start.
Exercise
Exercise
A page has a <header> with position: sticky; top: 0. It scrolls away with the page and never sticks. The header is inside a <div class="wrapper"> with overflow: hidden, and the wrapper is as tall as its content. Name two reasons sticky fails here and how to fix each.
Show answer
First, the wrapper has overflow: hidden, so it becomes the scroll container. It does not scroll, so the header never reaches its threshold. Remove the overflow or change it to overflow: clip, which does not create a scroll container. Second, the wrapper is only as tall as its content, and the header's parent is that wrapper, so the header has little or no room to travel. Place the header as a direct child of body, or make its parent tall enough to contain the content the header should stay over.