Bird
Raised Fist0
CSSmarkup~15 mins

Position fixed and sticky in CSS - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Position fixed and sticky
What is it?
Position fixed and sticky are two ways to control how elements stay visible on a webpage when you scroll. Fixed position keeps an element in the same place on the screen no matter how much you scroll. Sticky position lets an element act like normal until you scroll past it, then it sticks to a spot on the screen. Both help keep important content visible as users move through a page.
Why it matters
Without fixed or sticky positioning, important parts like menus or buttons would scroll away and disappear, making websites harder to use. These positions improve navigation and user experience by keeping key elements accessible. They solve the problem of losing track of important controls or information on long pages.
Where it fits
Before learning fixed and sticky, you should understand basic CSS positioning like static, relative, and absolute. After this, you can explore advanced layout techniques like Flexbox and Grid, and how to combine positioning with responsive design.
Mental Model
Core Idea
Fixed keeps an element locked on the screen always, while sticky lets it scroll until a point, then locks it in place.
Think of it like...
Imagine a sticky note on a wall: fixed is like taping a note firmly so it never moves, sticky is like a note that moves with a sliding board until it reaches the top, then stays stuck there.
┌─────────────────────────────┐
│        Viewport (Screen)    │
│ ┌───────────────┐           │
│ │ Fixed Element │ ← Always here
│ └───────────────┘           │
│                             │
│ ┌───────────────┐           │
│ │ Sticky Element│           │
│ └───────────────┘           │
│                             │
│ Scroll down →               │
│                             │
│ Sticky element scrolls down │
│ until it reaches top, then  │
│ it sticks there like fixed │
└─────────────────────────────┘
Build-Up - 6 Steps
1
FoundationUnderstanding CSS Position Property
🤔
Concept: Learn the basic CSS position property and its default behavior.
In CSS, every element has a position property that controls how it is placed on the page. The default is 'static', which means elements flow naturally in the page layout. Other values like 'relative' and 'absolute' change how elements move relative to their normal spot or parent containers.
Result
Elements with static position flow normally and scroll with the page.
Understanding the default positioning is essential before learning how fixed and sticky change element behavior.
2
FoundationWhat is Position Fixed?
🤔
Concept: Position fixed locks an element relative to the viewport, ignoring page scroll.
When you set an element's position to fixed, it stays in the same place on the screen even when you scroll. You can use top, bottom, left, and right to place it exactly where you want. For example, a fixed header stays visible at the top always.
Result
The fixed element remains visible at the set spot regardless of scrolling.
Knowing fixed position helps create persistent UI elements like headers or buttons that never disappear.
3
IntermediateWhat is Position Sticky?
🤔Before reading on: do you think sticky elements always stay fixed on screen or only sometimes? Commit to your answer.
Concept: Sticky position lets an element scroll normally until it reaches a threshold, then it sticks in place.
Sticky elements behave like normal content until you scroll past a certain point (like top: 0). Then they stick to that spot on the screen, staying visible while the rest scrolls. This is useful for headers that stay visible only after scrolling down.
Result
Sticky elements scroll with the page but stick to a position once scrolled past.
Understanding sticky position reveals how to create dynamic layouts that adapt as users scroll.
4
IntermediateDifferences Between Fixed and Sticky
🤔Before reading on: which position type requires a scroll to activate its sticky behavior? Fixed or sticky? Commit to your answer.
Concept: Compare how fixed and sticky behave differently during scrolling.
Fixed elements are always locked on screen, ignoring scroll. Sticky elements scroll normally until a threshold, then lock. Sticky depends on scroll position and parent container boundaries, while fixed ignores them.
Result
Fixed elements never move; sticky elements move then stick after scrolling.
Knowing these differences helps choose the right position for your design needs.
5
AdvancedSticky Position and Parent Overflow
🤔Before reading on: do you think sticky elements can stick outside their parent container? Commit to your answer.
Concept: Sticky elements only stick within their parent container's visible area.
If a sticky element's parent has overflow hidden or scroll, the sticky behavior is limited to that container. The element won't stick outside the parent's boundaries. This can cause unexpected behavior if not understood.
Result
Sticky elements stick only inside their parent's scroll area.
Understanding parent overflow effects prevents layout bugs with sticky elements.
6
ExpertBrowser Support and Performance Considerations
🤔Before reading on: do you think fixed and sticky positions have the same browser support and performance impact? Commit to your answer.
Concept: Explore how browsers implement fixed and sticky differently and their impact on performance.
Fixed position is widely supported and simple for browsers to render. Sticky is newer and may have quirks in older browsers. Sticky requires the browser to track scroll position and element boundaries, which can affect performance on complex pages. Developers must test across browsers and optimize usage.
Result
Fixed is reliable everywhere; sticky needs careful testing and optimization.
Knowing browser and performance details helps build robust, smooth user experiences.
Under the Hood
Fixed position removes the element from the normal document flow and positions it relative to the viewport. The browser keeps it at fixed coordinates regardless of scrolling. Sticky position is a hybrid: the element behaves like relative until the scroll position reaches a threshold, then the browser switches it to fixed behavior within the parent container's boundaries.
Why designed this way?
Fixed was designed to keep important UI elements visible at all times, improving usability. Sticky was introduced later to allow elements to remain in flow but become fixed only when needed, enabling more dynamic and space-efficient layouts. The design balances flexibility and control.
Document Flow
┌─────────────────────────────┐
│                             │
│  Normal Elements (static)   │
│                             │
│  ┌───────────────────────┐  │
│  │ Sticky Element (relative)│ Scroll down
│  └───────────────────────┘  │
│                             │
│  Scroll Threshold Reached    │
│  ┌───────────────────────┐  │
│  │ Sticky Element (fixed) │  │
│  └───────────────────────┘  │
│                             │
│  Fixed Element (always fixed)│
│  ┌───────────────────────┐  │
│  │ Fixed Element          │  │
│  └───────────────────────┘  │
└─────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does position sticky work if the parent container has overflow: hidden? Commit to yes or no.
Common Belief:Sticky elements always stick regardless of parent container styles.
Tap to reveal reality
Reality:Sticky elements only stick within the visible area of their parent container. If the parent has overflow hidden or scroll, sticky behavior is limited to that container.
Why it matters:Ignoring this causes sticky elements to not stick as expected, breaking layouts and confusing users.
Quick: Is a fixed element part of the normal page flow? Commit to yes or no.
Common Belief:Fixed elements behave like normal elements and affect page layout.
Tap to reveal reality
Reality:Fixed elements are removed from normal flow and do not affect the position of other elements.
Why it matters:Misunderstanding this leads to layout bugs where space is unexpectedly left empty or overlapped.
Quick: Does position sticky work in all browsers the same way? Commit to yes or no.
Common Belief:Sticky position is fully supported and consistent everywhere.
Tap to reveal reality
Reality:Sticky is supported in modern browsers but may have bugs or lack support in older versions, requiring fallbacks.
Why it matters:Assuming full support can cause broken layouts on some users' devices.
Quick: Does sticky position keep an element fixed on screen from the start? Commit to yes or no.
Common Belief:Sticky elements behave like fixed elements immediately.
Tap to reveal reality
Reality:Sticky elements scroll normally until a threshold, then become fixed.
Why it matters:Confusing sticky with fixed leads to wrong expectations and design mistakes.
Expert Zone
1
Sticky positioning depends on the scroll container, which may not always be the viewport, affecting behavior in nested scroll areas.
2
Fixed elements can cause repaint and compositing layers in browsers, impacting performance if overused or animated.
3
Sticky elements require a threshold value (like top) to work; without it, sticky behaves like relative, which is often overlooked.
When NOT to use
Avoid fixed position for large or complex elements that may block content or cause performance issues; use sticky or other layout methods instead. Sticky should not be used inside containers with overflow hidden or scroll if you want it to stick outside those boundaries. For full control over element visibility, consider JavaScript-based scroll listeners.
Production Patterns
Fixed headers and footers for navigation bars, sticky table headers that remain visible while scrolling data, sticky sidebars that stay in view after scrolling past their start, and fixed action buttons for quick access. Combining sticky with CSS Grid or Flexbox layouts is common for responsive designs.
Connections
JavaScript Scroll Events
Builds-on
Understanding CSS sticky and fixed helps when using JavaScript to create custom scroll-based effects or dynamic element positioning.
User Experience Design
Builds-on
Knowing how fixed and sticky positions affect visibility and accessibility improves design decisions for better user navigation and interaction.
Physics - Friction and Adhesion
Analogy-based connection
The way sticky elements behave like objects that slide then stick relates to physical concepts of friction and adhesion, helping to intuitively grasp the scroll threshold behavior.
Common Pitfalls
#1Sticky element does not stick when scrolling.
Wrong approach:.header { position: sticky; /* missing top value */ }
Correct approach:.header { position: sticky; top: 0; }
Root cause:Sticky requires a threshold like top, bottom, left, or right to know when to stick; without it, it behaves like relative.
#2Fixed element overlaps content but page layout leaves space as if it wasn't fixed.
Wrong approach:
Fixed Header
Content starts at top
Correct approach:
Fixed Header
Content starts below header
Root cause:Fixed elements are removed from flow, so other content must be manually offset to avoid overlap.
#3Sticky element inside a parent with overflow hidden does not stick.
Wrong approach:
Sticky Content
Correct approach:
Sticky Content
Root cause:Sticky elements cannot stick outside the visible area of their parent container.
Key Takeaways
Position fixed locks an element to the viewport so it stays visible during scrolling.
Position sticky lets an element scroll normally until a threshold, then it sticks in place within its parent container.
Sticky requires a threshold value like top or bottom to work and is limited by parent container overflow.
Fixed elements are removed from normal flow and can overlap content unless space is reserved.
Understanding these positions improves user experience by keeping important elements accessible and visible.

Practice

(1/5)
1. What does position: fixed; do to an element on a webpage?
easy
A. Keeps the element always visible in the same spot on the screen, even when scrolling.
B. Makes the element scroll normally with the page content.
C. Positions the element relative to its parent container.
D. Hides the element when the page is scrolled.

Solution

  1. Step 1: Understand fixed positioning

    Elements with position: fixed; stay in the same place on the screen regardless of scrolling.
  2. Step 2: Compare with other positions

    Unlike normal flow or relative positioning, fixed elements do not move when the page scrolls.
  3. Final Answer:

    Keeps the element always visible in the same spot on the screen, even when scrolling. -> Option A
  4. Quick Check:

    Fixed = Always visible on screen [OK]
Hint: Fixed means element stays put on screen during scroll [OK]
Common Mistakes:
  • Thinking fixed elements scroll with the page
  • Confusing fixed with relative positioning
  • Assuming fixed hides the element
2. Which of the following is the correct CSS syntax to make an element sticky at 10px from the top?
easy
A. position: relative; top: 10px;
B. position: fixed; top: 10px;
C. position: absolute; top: 10px;
D. position: sticky; top: 10px;

Solution

  1. Step 1: Identify sticky syntax

    The correct way to make an element sticky is using position: sticky; with an offset like top: 10px;.
  2. Step 2: Check other options

    Fixed keeps element always on screen, absolute positions relative to nearest positioned ancestor, relative moves element relative to normal spot.
  3. Final Answer:

    position: sticky; top: 10px; -> Option D
  4. Quick Check:

    Sticky syntax = position: sticky + offset [OK]
Hint: Sticky needs position sticky plus top/left/right/bottom [OK]
Common Mistakes:
  • Using fixed instead of sticky
  • Forgetting to add top offset
  • Using relative or absolute incorrectly
3. Given this CSS and HTML, what will happen when you scroll the page?
<style>
header {
  position: sticky;
  top: 0;
  background: lightblue;
  padding: 1rem;
}
</style>
<header>Sticky Header</header>
<div style='height: 2000px;'>Content</div>
medium
A. The header stays stuck at the top of the viewport when scrolling down.
B. The header scrolls away with the page content.
C. The header stays fixed at the bottom of the page.
D. The header disappears when scrolling starts.

Solution

  1. Step 1: Understand sticky with top: 0

    The header will scroll normally until it reaches the top of the viewport, then it sticks there.
  2. Step 2: Visualize scrolling effect

    As you scroll down, the header remains visible stuck at the top, making it easy to access.
  3. Final Answer:

    The header stays stuck at the top of the viewport when scrolling down. -> Option A
  4. Quick Check:

    Sticky + top:0 = sticks at top on scroll [OK]
Hint: Sticky sticks at offset after scrolling reaches it [OK]
Common Mistakes:
  • Thinking sticky behaves like fixed always
  • Assuming header scrolls away immediately
  • Confusing top: 0 with bottom positioning
4. You want a navigation bar to stay visible at the top when scrolling, but your CSS uses position: sticky; and it doesn't stick. What is a likely cause?
medium
A. You forgot to add position: fixed; instead.
B. The element has no width set.
C. The parent container has overflow: hidden; or overflow: auto;.
D. The element is inside a <footer> tag.

Solution

  1. Step 1: Check parent container overflow

    Sticky positioning requires the parent container not to clip overflow; overflow: hidden; or auto can prevent sticky from working.
  2. Step 2: Understand sticky requirements

    Sticky depends on scroll container; if parent clips overflow, sticky won't stick.
  3. Final Answer:

    The parent container has overflow: hidden; or overflow: auto;. -> Option C
  4. Quick Check:

    Sticky fails if parent clips overflow [OK]
Hint: Check parent overflow to fix sticky not working [OK]
Common Mistakes:
  • Switching to fixed without need
  • Ignoring parent container styles
  • Assuming tag type affects sticky
5. You want a sidebar that stays visible on the left side as you scroll down, but only after you scroll past its original position. Which CSS setup achieves this behavior?
hard
A. Use position: fixed; left: 0; on the sidebar.
B. Use position: sticky; top: 0; on the sidebar inside a tall container.
C. Use position: absolute; left: 0; top: 0; on the sidebar.
D. Use position: relative; left: 0; on the sidebar.

Solution

  1. Step 1: Understand sticky sidebar behavior

    Sticky lets the sidebar scroll normally until it reaches the top, then it sticks there, perfect for this use case.
  2. Step 2: Why not fixed or absolute?

    Fixed would always keep sidebar visible, ignoring scroll position. Absolute positions relative to container but doesn't stick on scroll.
  3. Final Answer:

    Use position: sticky; top: 0; on the sidebar inside a tall container. -> Option B
  4. Quick Check:

    Sticky = scroll then stick at offset [OK]
Hint: Sticky sticks after scrolling past original spot [OK]
Common Mistakes:
  • Using fixed which ignores scroll position
  • Using absolute which doesn't stick on scroll
  • Using relative which just shifts position