Sticky
A sticky element scrolls with the page until it reaches its top offset, then stays put until its parent scrolls away.
Apprentice · CSS · Updated
After this lesson you can
- Make an element scroll with the page, then stick at a top offset, with sticky positioning
- Choose static, relative, absolute, fixed or sticky positioning for an element in a described layout
Key takeaways
- A sticky element starts in its normal place and keeps its space in the page.
- It scrolls with the page until it's as far from the top of the window as its top value, then it stays there.
- It stays only while its parent is on screen. When the parent scrolls away, the sticky element goes with it.
- Static, relative, absolute, fixed and sticky each fit a different job, so choose by how the element should move.
A sticky element sits in the page like a normal (relative) element until you scroll it to its top offset; then it stays put at that spot while the rest scrolls past, until its parent element scrolls out of view.
.div-2 {
position: sticky;
left: 30px;
top: 30px;
}

The left: 30px keeps it at least 30px from the window's left edge, which is why it sits a little to the right of the other divs.
As the page is scrolled, the sticky div scrolls with the page until it's 30px from the top of the window, its top value. Once it gets there, it no longer scrolls with the page.

Choosing a position value
To pick a value, ask two things: should the element keep its space in the page, and should it move when the page scrolls?
| Value | Keeps its space? | Scrolls with the page? | Measured from |
|---|---|---|---|
static |
Yes | Yes | Nothing: top and left are ignored |
relative |
Yes | Yes | Its own normal place |
absolute |
No | Yes | Its nearest positioned ancestor, or the page |
fixed |
No | No, it stays on screen | The browser window |
sticky |
Yes | Until it reaches its top offset, then it stays while its parent is on screen |
Its normal place, then the window's edge once it sticks |
Do it: keep each menu heading in view
The Sunrise Bakery's menu page is long. As a reader scrolls through the cakes, the Cakes heading scrolls away, and they lose track of which part of the menu they're in. The owner wants each section's heading to stay at the top of the window while its section is on screen.
What's on the page:
h1: the bakery's name, at the top.section: one for each part of the menu, Cakes and Breads.h2: each section's heading, the first thing inside its section.
For example, here is what the Cakes heading should do as the reader scrolls down:
| As the page scrolls | The Cakes heading |
|---|---|
| The heading is still below the top of the window | Scrolls up with the page, like any element |
| It reaches the top of the window, 0 pixels from it | Stops there and stays, while the cakes scroll past under it |
| The Cakes section scrolls out of view | Leaves with its section, and the Breads heading takes its turn |
Your job: write one rule for the section headings, the h2 elements.
- Make them sticky.
- They stick
0pixels from the top of the window. - Give them a white background,
#ffffff, so the items scrolling under them don't show through. - Leave the bakery's name, the
h1, as it is: it stays in its normal place and scrolls away.
Practiced live on the lesson page: write the CSS right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Sticky gives you table headers and menus that stay in reach exactly while their section is on screen, with no JavaScript.