October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Use CSS `position: sticky` (and Fix Common Problems)

Set a non-auto inset to make an element sticky, then check ancestor overflow and its containing block if it sticks in the wrong place or stops early.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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-auto inset on the intended axis, such as top: 0.
  • Inspect ancestors for overflow: hidden, auto, or scroll. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

It 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
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • 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

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.