Sticky & Fixed Positioning
Master advanced positioning techniques with sticky and fixed elements. Create headers that stick, floating buttons, and elements that stay in view.
How do I make a sticky header in CSS?
Add position: sticky; top: 0; to the header. It scrolls normally until it reaches the top of the viewport, then stays there. Use position: fixed only when the element must always stay on screen, and add padding to the page so the fixed element doesn't cover content.
header {
position: sticky;
top: 0;
z-index: 10;
background: white; /* so content doesn't show through */
}
.chat-button {
position: fixed;
right: 1rem;
bottom: 1rem;
}Interactive Sticky & Fixed Demo
Code Editor
<div class="demo-container">
<header class="sticky-header">
<h3>Sticky Header</h3>
<p>This header sticks to the top when scrolling</p>
</header>
<main class="content">
<p>Scroll down to see the sticky behavior...</p>
<div class="content-block">Content Block 1</div>
<div class="content-block">Content Block 2</div>
<div class="content-block">Content Block 3</div>
</main>
<div class="fixed-element">Fixed</div>
</div>Preview
Fixed Positioning
Fixed positioning positions elements relative to the viewport. The element stays in the same position even when scrolling, making it perfect for persistent UI elements.
Basic Fixed Position
.fixed-element {
position: fixed;
top: 20px; /* Distance from top of viewport */
right: 20px; /* Distance from right of viewport */
z-index: 1000; /* Ensure it's on top */
}The element is removed from the normal document flow and positioned relative to the viewport.
Fixed Header
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: white;
z-index: 100;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* Add top padding to body to prevent overlap */
body {
padding-top: 80px; /* Header height */
}Classic fixed header that stays at the top
Floating Action Button
.fab {
position: fixed;
bottom: 20px;
right: 20px;
width: 56px;
height: 56px;
border-radius: 50%;
background: #3b82f6;
color: white;
border: none;
cursor: pointer;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 1000;
}Persistent floating action button
Sticky Positioning
Basic Sticky Position
.sticky-element {
position: sticky;
top: 0; /* Stick when top reaches viewport top */
background: white;
z-index: 10;
}
/* Sticky positioning requires a containing block */
/* The element must have a scrollable ancestor */Sticky elements behave like relative positioning until they reach a threshold, then become fixed.
Sticky Navigation
.sticky-nav {
position: sticky;
top: 0;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-bottom: 1px solid #e5e7eb;
z-index: 50;
}
/* Smooth transition when sticking */
.sticky-nav {
transition: all 0.3s ease;
}Navigation that sticks with smooth transitions
Sticky Sidebar
.layout {
display: grid;
grid-template-columns: 250px 1fr;
gap: 2rem;
}
.sticky-sidebar {
position: sticky;
top: 2rem;
height: fit-content;
max-height: calc(100vh - 4rem);
overflow-y: auto;
}Sidebar that sticks within its container
Z-index and Stacking Context
Z-index Management
/* Define z-index hierarchy */
:root {
--z-dropdown: 1000;
--z-sticky: 1020;
--z-fixed: 1030;
--z-modal: 1040;
--z-popover: 1050;
--z-tooltip: 1060;
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: var(--z-modal);
}Organize z-index values with CSS custom properties for consistency.
Stacking Context
/* Creates new stacking context */
.stacking-context {
position: relative;
z-index: 0; /* Creates context */
}
/* Other properties that create stacking context: */
/* opacity less than 1 */
/* transform: any value */
/* filter: any value */
/* isolation: isolate */
/* will-change: transform */
Understanding stacking contexts prevents z-index conflicts
Common Z-index Values
/* Typical z-index scale */
.behind { z-index: -1; }
.base { z-index: 0; }
.dropdown { z-index: 10; }
.sticky { z-index: 20; }
.fixed { z-index: 30; }
.modal-backdrop { z-index: 40; }
.modal { z-index: 50; }
.popover { z-index: 60; }
.tooltip { z-index: 70; }Use a consistent scale with room for new layers
Practical Examples
Modal Overlay
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
}
.modal-content {
background: white;
padding: 2rem;
border-radius: 8px;
max-width: 500px;
max-height: 80vh;
overflow-y: auto;
}Full-screen modal overlay with centered content.
Table Header Sticky
.table-container {
max-height: 400px;
overflow: auto;
border: 1px solid #e5e7eb;
}
.table {
width: 100%;
border-collapse: collapse;
}
.table th {
position: sticky;
top: 0;
background: #f9fafb;
border-bottom: 2px solid #e5e7eb;
padding: 0.75rem;
text-align: left;
z-index: 10;
}Table headers that stick when scrolling through data.
Notification Toast
.toast-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 1050;
pointer-events: none; /* Allow clicks through */
}
.toast {
background: #10b981;
color: white;
padding: 1rem 1.5rem;
border-radius: 8px;
margin-bottom: 0.5rem;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
pointer-events: all; /* Enable interactions */
transform: translateX(100%);
animation: slideIn 0.3s ease forwards;
}
@keyframes slideIn {
to { transform: translateX(0); }
}Animated notification toasts positioned at screen edge.
Best Practices
Best Practices
- • Use sticky for contextual elements within sections
- • Use fixed for persistent UI elements
- • Manage z-index with CSS custom properties
- • Account for mobile viewport changes
- • Test scroll behavior on different devices
- • Consider accessibility and focus management
Performance Tips
- • Use transform for animations over position changes
- • Add will-change for elements that will animate
- • Consider containment for complex fixed layouts
- • Minimize repaints with backdrop-filter
- • Use position: relative sparingly for performance
- • Test on lower-end devices for smooth scrolling
CSS Sticky and Fixed — step by step
position: sticky Basics
sticky acts like relative until the element reaches its scroll threshold, then behaves like fixed. Requires a top/bottom/left/right value to activate.
🎯 Quick Check
What must you set on a sticky element for it to actually stick?
Sticky & Fixed Positioning Mastered!
You understand advanced positioning. Explore more CSS tutorials to expand your skills!
Sticky and Fixed Positioning: summary
Key points
- A sticky element only sticks while its parent is on screen, so place it high enough in the DOM.
- Any ancestor with
overflow: hidden,autoorscrollstopsstickyfrom working. - Fixed elements leave the normal flow; reserve space for them with padding.
- Give sticky headers a background and a
z-indexso scrolled content passes underneath.
Frequently asked questions
›Why is my sticky header not sticking?
Usually an ancestor has overflow set, the top offset is missing, or the sticky element's parent is only as tall as the header itself.
›Should I use sticky or fixed for a header?
Prefer sticky. It keeps the header in the flow, so you don't need to offset the content below it.