Bird
Raised Fist0
CSSmarkup~20 mins

Position fixed and sticky in CSS - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Position Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding position: fixed behavior
What happens to an element with position: fixed when you scroll the page?
AIt stays in the same place relative to the viewport, not moving when scrolling.
BIt scrolls with the page content like a normal element.
CIt moves only horizontally but not vertically when scrolling.
DIt disappears when you scroll past its original position.
Attempts:
2 left
💡 Hint
Think about a sticky note stuck to your computer screen that never moves when you scroll.
🧠 Conceptual
intermediate
2:00remaining
How position: sticky works
Which statement best describes how position: sticky behaves?
AIt disappears when scrolling past its container.
BIt behaves like <code>relative</code> until a scroll threshold, then it sticks to a position.
CIt behaves like <code>absolute</code> and moves with the nearest positioned ancestor.
DIt behaves like <code>fixed</code> all the time, ignoring scroll position.
Attempts:
2 left
💡 Hint
Imagine a header that scrolls up but then sticks at the top of the page.
📝 Syntax
advanced
2:00remaining
CSS for sticky header with top offset
Which CSS snippet correctly makes a header sticky at 0 distance from the top of the viewport?
CSS
header {
  /* Your CSS here */
}
Aposition: relative; top: 0;
Bposition: fixed; top: 0;
Cposition: absolute; top: 0;
Dposition: sticky; top: 0;
Attempts:
2 left
💡 Hint
Sticky means it scrolls until it reaches the top, then stays there.
rendering
advanced
2:00remaining
Visual effect of fixed vs sticky
You have two boxes: one with position: fixed; top: 10px; and another with position: sticky; top: 10px;. What will you see when you scroll down the page?
AThe sticky box stays fixed at 10px from top always; the fixed box scrolls normally.
BBoth boxes scroll normally and disappear when out of view.
CThe fixed box stays visible at 10px from top always; the sticky box scrolls until 10px from top then sticks.
DBoth boxes disappear when scrolling past their original position.
Attempts:
2 left
💡 Hint
Think about which box behaves like glued to the screen and which one only sticks after scrolling.
accessibility
expert
3:00remaining
Accessibility considerations for fixed and sticky elements
Which accessibility best practice should you follow when using position: fixed or position: sticky for navigation bars?
AEnsure keyboard focus can reach all interactive elements and that fixed/sticky bars do not cover content.
BUse fixed or sticky positioning without any additional ARIA roles or focus management.
CHide fixed or sticky elements from assistive technologies to reduce clutter.
DAvoid using fixed or sticky positioning because screen readers cannot read them.
Attempts:
2 left
💡 Hint
Think about users who navigate with keyboard or screen readers and how fixed bars might block content.

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