Sticky Positioning Thresholds
You'll learn how to control exactly when a sticky element "sticks" during scrolling.
What Are Sticky Positioning Thresholds?
In the last lesson, you learned that position: sticky makes an element behave like a hybrid—it flows normally until you scroll past it, then it "sticks" in place. But how do you control the exact moment it sticks? That's where offset properties (top, right, bottom, left) come in.
These offset properties act as thresholds—they define the boundaries where the sticky behavior activates. Think of them like invisible tripwires: when the element reaches that specified distance from the edge of its scrolling container, it locks into position.
How Offset Properties Define Stickiness
When you set position: sticky, you must also set at least one offset property:
.sticky-header {
position: sticky;
top: 0; /* Stick when it reaches 0px from the TOP of the viewport */
}
This tells the browser: "Keep this element in normal flow, but when the user scrolls and this element is about to move past 0px from the top, hold it there instead."
You can set different thresholds:
.sticky-sidebar {
position: sticky;
top: 20px; /* Stick 20px from the top, not flush against it */
}
Important: The offset defines where the element sticks relative to its scrolling container, not where it starts in the document. It only becomes sticky within its parent's boundaries.
Key Takeaway: Offset properties (top, bottom, left, right) define the threshold distance at which a sticky element locks into position during scroll—without them, position: sticky won't work.