To keep an element visible as the page scrolls, set position: sticky and a non-auto inset on the axis where it should stick—for example, top: 0 for a top-mounted navigation bar. Sticky elements remain in normal document flow, follow the nearest ancestor with a scrolling mechanism, and cannot travel beyond their containing block.
Make an element stick while scrolling
Here is a basic sticky navigation bar:
.site-nav {
position: sticky;
top: 0;
z-index: 10;
}
The top: 0 inset sets the threshold at the top edge of the relevant scrolling area. The z-index is optional; use it if the navigation should layer above nearby content. Sticky positioning creates a stacking context, but the explicit value helps express the intended layer.
For a bottom-sticking element, use bottom; for writing-mode-aware layouts, use logical insets such as inset-block-start. The essential rule is that the sticky axis needs a non-auto inset. If both insets on that axis are auto, the element behaves like a relatively positioned element on that axis. See the MDN reference for the CSS position property.
Understand what sticky does
Sticky is not simply “fixed after scrolling.” The element first occupies its normal place in the document. When scrolling brings it to the specified inset threshold, it is offset to stay there; it retains its layout space. Its movement ends when it reaches the opposite edge of its containing block.
The nearest ancestor with a scrolling mechanism determines the scroll reference. An ancestor with overflow: hidden, auto, or scroll can become that reference, even when a different outer area is the one visibly scrolling. The containing block, in turn, bounds how far the sticky element can travel. MDN describes these relationships in its position reference and guide to containing blocks.
Choose sticky or fixed positioning
| Behavior | position: sticky |
position: fixed |
|---|---|---|
| Space in normal flow | Retained | Different positioning mode; do not rely on sticky’s flow behavior |
| Scroll reference and travel | Follows the nearest ancestor with a scrolling mechanism and is bounded by its containing block | Not the same bounded, normal-flow behavior as sticky |
| Typical use | A navigation bar that scrolls with the page before reaching its sticking threshold; headings in a scrolling index | Use when the design calls for fixed positioning rather than an element that remains constrained by its containing block |
MDN’s positioning guide demonstrates both a navigation bar and headings that stick as they reach the top. Choose based on the required layout behavior, not on the shorthand that sticky is “fixed.”
Fix common sticky-position problems
The element never sticks
- Set a non-
autoinset on the intended axis, such astop: 0. - Inspect ancestors for
overflow: hidden,auto, orscroll. The nearest ancestor that creates a scrolling mechanism may be the one sticky follows, not the viewport or the area you expected. - Check the containing block and available space. The sticky element needs room to move before it can reach its boundary.
It sticks inside the wrong area
Walk up the element’s ancestor tree and inspect each overflow value. Find the nearest ancestor that creates a scrolling mechanism; that ancestor can determine where the sticky threshold applies. The relevant behavior is described in the MDN position reference.
It stops sticking too soon
Inspect the sticky element’s containing block and the height available in its parent layout. Sticky movement is limited by the containing block, so a short block can leave little distance for the element to remain stuck. See MDN’s containing block guide.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsIt appears behind nearby content
Sticky creates a stacking context. If the element should overlay neighboring content, assign an appropriate z-index and inspect the surrounding stacking contexts; a local value may not overcome a higher-level stacking context.
Or skip the browser setup
If you need a screenshot of a page to inspect a sticky layout, ScreenshotNeo can capture it with one GET request. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Learn about ScreenshotNeo or sign up for 1,000 free screenshots a month with no card.
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Rank #2
Rank #3
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Rank #4
Best Value
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




