CSS

CSS Position Sticky: Syntax, Usage, and Examples

The CSS sticky position is a powerful layout feature that allows elements to remain fixed within the bounds of their parent container as the user scrolls. This behavior offers the best of both worlds: an element behaves like a position: relative element until it reaches a specific scroll threshold, and then it sticks like a fixed element. It's especially useful for creating sticky headers, sidebars, or table headers that remain in view while scrolling — a common pattern in web development and modern front-end design.


What Is CSS Sticky Position?

CSS sticky position is a hybrid between relative and fixed positioning. It allows an element to scroll with its parent container until it reaches a defined position (via top, left, right, or bottom), after which it becomes fixed within the viewport—only relative to its containing block.

This feature improves usability by keeping key information in view, such as a navigation bar (navbar), sticky filters, or data table headers, without permanently taking up space.

You can apply sticky behavior to many HTML elements, including header, aside, or even a div acting as a wrapper.

Basic Syntax

selector {
  position: sticky;
  top: 0;
}

In this example, the element will behave like it’s in normal flow until the web page scrolls to the top offset (0), at which point the element becomes "stuck" to the top of the container.


How Sticky Position CSS Works

Sticky elements only stick within their nearest scrollable ancestor and are constrained by the boundaries of that element. This makes CSS sticky position different from fixed, which anchors the element to the viewport.

For the sticky behavior to work properly, certain conditions must be met:

  • The element must have position: sticky.
  • It must have a scroll threshold set (top, bottom, left, or right).
  • Its parent element must not have overflow: hidden, overflow: scroll, or overflow: auto in a way that hides the sticky behavior.

If you’re debugging sticky layouts, inspecting the DOM in Chrome or Safari dev tools helps confirm where the sticky element lives in the hierarchy and why it might not be working.


Example: Sticky Header

header {
  position: sticky;
  top: 0;
  background-color: white;
  z-index: 1000;
}

This keeps a header or navbar pinned while the rest of the content scrolls. Many developers share ready-to-use header snippets on Codepen and Github to speed up implementation.


Sticky Sidebar Layout

.sidebar {
  position: sticky;
  top: 20px;
}

In this layout, the sidebar will scroll normally until it hits 20px from the top of the viewport, after which it sticks and remains visible while the rest of the page scrolls.

This is popular for filter panels, user interface menus, or ads that follow the user.


Understanding Sticky’s Containment Rules

The sticky element stays confined to the boundaries of its parent container. Once the user scrolls past the bottom of the container, the element stops sticking and scrolls away with the rest of the content.

If the parent doesn’t have enough height or has an inappropriate overflow value, sticky behavior may break.


CSS Position Sticky Not Working: Troubleshooting Guide

It’s common to run into issues when implementing sticky positioning. Here are reasons why sticky position CSS might not work:

1. No Threshold Value

You must include a threshold, like top: 0. Sticky won’t activate without it.

.sticky-element {
  position: sticky;
  top: 0; /* Required for sticky to engage */
}

2. Overflow Issues on Ancestors

If any parent element has overflow set to hidden, scroll, or auto, it can interfere with sticky behavior.

.container {
  overflow: visible; /* Ensure it's not hidden */
}

3. Not Enough Space

Sticky needs enough space in its container to activate. If the element’s container is shorter than the sticky element itself, it may not function as expected.

Developers often test different scenarios on Codepen or consult browser compatibility tables to confirm browser support for sticky behavior, especially on mobile devices.


Sticky Position in CSS Grid Layouts

CSS sticky position works well with grid-based layouts. If you're using a grid container and want sticky elements within a grid cell, you can still apply sticky positioning.

.grid-container {
  display: grid;
  grid-template-columns: 1fr 3fr;
}

.sidebar {
  position: sticky;
  top: 10px;
}

This allows sticky sidebars or headers inside grid systems. However, ensure that the grid item’s height allows the sticky behavior to fully engage.


Sticky Table Headers

Another popular use case is making table headers sticky:

thead th {
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 1;
}

This keeps headers visible while scrolling through large data tables.

Sticky positioning also plays nicely with horizontally scrollable tables when used in combination with left or right values. This is especially helpful for long tables on dashboards or admin web pages. Some developers also add subtle CSS animations or box-shadow to the header so users can see it’s staying in place.


Scroll Direction and Sticky Axes

The axis of stickiness (vertical or horizontal) depends on which threshold you set:

  • top or bottom: vertical scroll
  • left or right: horizontal scroll

You can make elements stick horizontally as well:

.column-header {
  position: sticky;
  left: 0;
  background: #eee;
}

This is useful in data dashboards or when horizontal scrolling is needed.


z-Index and Layering Sticky Elements

Because sticky elements often sit above other content, it’s common to give them a z-index value to ensure proper layering.

.sticky-nav {
  position: sticky;
  top: 0;
  z-index: 100;
}

Without a z-index, the sticky element may be visually obscured by overlapping content. Pair it with CSS properties like background-color to avoid transparency issues.


When Not to Use Sticky Position

While sticky positioning is powerful, it's not always the right tool. Avoid using it:

  • Inside containers with unpredictable height
  • On elements that require full control across multiple parent containers
  • When fallback behavior is critical in older browsers (like IE11)

Also, avoid layering too many sticky elements as it can clutter the interface and confuse users.


Combining Sticky with Other Features

Sticky can be used in tandem with animations, transitions, or JavaScript-based scroll events to enhance user experience.

Example: Smooth Reveal of Sticky Header

.sticky-header {
  position: sticky;
  top: 0;
  transition: background-color 0.3s;
}

.sticky-header.active {
  background-color: #f8f8f8;
}

Use JavaScript to add the .active class when the scroll position exceeds a threshold, creating a dynamic interaction pattern.


Accessibility Considerations

Sticky positioning should enhance—not hinder—navigation. Avoid covering content with sticky headers or footers unless offset by padding or margins.

Ensure that:

  • Sticky headers don’t block form fields or buttons
  • Sticky elements don’t interfere with tab navigation
  • Sticky behavior works well across screen sizes

Summary

CSS sticky position offers a flexible, intuitive way to make elements remain visible during scroll—only within their defined space. By combining the best of relative and fixed positioning, sticky elements enhance navigation, reinforce context, and provide better UX, particularly for headers, menus, sidebars, and data tables.

To make the most of sticky position CSS:

  • Set an appropriate threshold (top, left, etc.)
  • Ensure parent containers don’t have conflicting overflow settings
  • Avoid overusing sticky elements on the same screen
  • Use with CSS Grid or Flexbox for advanced layouts

By troubleshooting common issues like layout constraints or missing offsets, and applying sticky behavior judiciously, you can make your layouts more usable and engaging.

Learn CSS for Free
Start learning now
button icon
To advance beyond this tutorial and learn CSS by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster