CSS TutorialLesson #70

Sticky Position

An element with `position: sticky;` toggles between

CSS position: sticky

An element with position: sticky; toggles between a relative and fixed position, depending on the scroll position.

A sticky element is positioned relative until a certain scroll position is reached - then it "sticks" in that place (like position:fixed).

<iframe src="trycss_position_sticky_ifr.htm" style="width:100%;border:4px solid #e5e5e5"></iframe>

Note: You must specify at least one of the top, right, bottom or left properties, for sticky positioning to work.

In this example, when the sticky element reach the top of the page (top: 0), it sticks to this position:

Example

Example Code

css
Try it Yourself
div.sticky {
  position:
  sticky;
  top: 0;
  background-color: green;

  border: 2px solid #4CAF50;
}

Common Uses for Sticky Positioning

Sticky positioning is commonly used for:

  • Sticky headers - Navigation bars that stick to the top when scrolling
  • Sticky sidebars - Side content that stays visible while scrolling main content
  • Sticky table headers - Table headers that remain visible when scrolling long tables

Sticky vs. Fixed

The key differences between sticky and fixed positioning:

Featureposition: stickyposition: fixed
Initial behaviorActs like relativeAlways fixed to viewport
Space in documentTakes up space initiallyDoes not take up space
Scroll behaviorSticks after thresholdAlways fixed
Container boundYes, respects parent boundariesNo, always relative to viewport

All CSS Positioning Properties

PropertyDescription
bottomSets the bottom margin edge for a positioned box
clipClips an absolutely positioned element
leftSets the left margin edge for a positioned box
positionSpecifies the type of positioning for an element
rightSets the right margin edge for a positioned box
topSets the top margin edge for a positioned box

Interactive Code Examples

CSS Example 1

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview: