# 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 November 15, 2026

By Gregor Dzierzon

## 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.

```css
.div-2 {
    position: sticky;
    left: 30px;
    top: 30px;
}
```

![Div 2 sits in its normal place between div 1 and div 3, 30px in from the left edge of the window](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/09/35d9e834adf04161b92107717f18bfee.png "Div 2 uses position: sticky")

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.

![After scrolling, div 2 has stopped 30px from the top of the window while divs 3, 4 and 5 scroll up past it](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/09/b3765343670a44b6ba2bcd1a64b4cd49.png "Div 2 stops scrolling 30px from the top of the window")

## 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 `0` pixels 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.

## Key terms

- [Sticky positioning](https://www.ask-codey.com/glossary/sticky-positioning)

## Cheat sheets

- [Sticky](https://www.ask-codey.com/cheat-sheets/css-position-sticky-cheat-sheet)

Source: https://www.ask-codey.com/lessons/css-position-sticky
