Course contentsShow
← Web Development
Lesson 224 of 2,9824. CSS Layout FoundationsFree lesson

Sticky Positioning Containing Block

How sticky elements are constrained by their parent container's boundaries.

Sticky Positioning Containing Block

What you'll learn: How the boundaries of a sticky element's parent container limit where it can "stick."

The Invisible Fence

You've learned that position: sticky creates elements that toggle between relative and fixed behavior. But here's a crucial limitation: a sticky element can only stick within its parent container. Once the parent scrolls out of view, the sticky element goes with it—no exceptions.

Think of it like a dog on a leash in a fenced yard. The dog (sticky element) can run around freely within the yard (parent container), but the fence is an absolute boundary. When the owner walks away dragging the yard with them, the dog must follow.

How It Works

The parent container of a sticky element acts as its containing block for stickiness. The sticky element will:

  1. Scroll normally with its parent until it hits its threshold (top, bottom, etc.)
  2. "Stick" at that position while the parent continues scrolling
  3. Unstick and scroll away when the bottom edge of the parent reaches the sticky element's position

This means if you have a sticky sidebar inside a short article, the sidebar will stop being sticky once you scroll past the article's end—even if there's more page content below.

Practical Implications

  • Sticky elements in small containers have limited "stick time"
  • The parent's height determines how long something can remain sticky
  • If the parent is shorter than the sticky element itself, stickiness may not work at all

Key Takeaway: A sticky element is always constrained by its parent container's boundaries—it cannot stick beyond where its parent exists on the page.