Last updated: March 13, 2026
How to Make a Sticky Header in CSS
Learn how to create a navigation header that stays at the top of the screen when scrolling. Step-by-step guide with live demos.
What is a Sticky Header?
A sticky header is a navigation bar that remains visible at the top of the viewport as users scroll down the page. This improves user experience by keeping navigation accessible at all times.
position: sticky (Recommended)
RecommendedThe modern and recommended approach. The header stays in document flow until it reaches the top of the viewport, then becomes fixed. No need to add padding to compensate.
CSS Code
Pros
- +Stays in document flow (no content jump)
- +No need to add padding to body
- +Simpler implementation
- +Works with container boundaries
- +Better for SEO and accessibility
Cons
- -Won't work if parent has overflow: hidden
- -Parent needs sufficient height
- -Slightly less browser support (97%+)
position: fixed
The classic approach that removes the header from document flow. The header is always fixed to the viewport, requiring padding on the body to prevent content from hiding behind it.
CSS Code
Pros
- +99%+ browser support
- +Always fixed regardless of scroll
- +Works in any container structure
- +No parent overflow issues
Cons
- -Removed from document flow
- -Requires body padding offset
- -Content can jump on page load
- -Need to maintain padding when header height changes
Comparison: sticky vs fixed
Use this table to quickly understand the differences between position: sticky and position: fixed.
| Aspect | position: sticky | position: fixed |
|---|---|---|
| Takes up space in flow | Yes | No (needs padding) |
| Parent container aware | Yes | No |
| Browser support | 97%+ | 99%+ |
| Easier to implement | Yes | Needs offset |
| Works with overflow: hidden parent | No | Yes |
| Respects container boundaries | Yes | No |
Takes up space in flow
Parent container aware
Browser support
Easier to implement
Works with overflow: hidden parent
Respects container boundaries
Step-by-Step Implementation
Follow these steps to create a sticky header from scratch using position: sticky (recommended).
Basic Header Structure
Create your header HTML with navigation or content.
Apply position: sticky
Add position: sticky to make the header stick when scrolling.
Set top: 0
Define where the element should stick. top: 0 means it sticks to the top of the viewport.
Add z-index
Ensure the header appears above other content when scrolling.
Add Background Color
Give the header a solid background so content doesn't show through.
(Optional) Add Shadow on Scroll
Use CSS-only technique with a pseudo-element to show shadow when header is sticky.
Live Demo
Scroll inside this container to see both sticky and fixed headers in action.
Common Issues and Fixes
Troubleshoot common problems when implementing sticky headers.
Sticky header not working
Parent element has overflow: hidden or overflow: auto
Remove overflow property from parent elements or use position: fixed instead.
Header doesn't stick
Missing top property
Always specify where the element should stick with top, bottom, left, or right.
Header disappears while scrolling
Parent container doesn't have enough height
Ensure the sticky element's parent has more content than the viewport height.
Content hidden behind fixed header
Using position: fixed without body padding
Add padding-top to body equal to header height.
Z-index not working
Other elements have higher z-index or create new stacking contexts
Use a high z-index value and check for competing stacking contexts.
Tailwind CSS Version
Here's how to create sticky headers using Tailwind CSS utility classes.
RecommendedSticky Header
Fixed Header
Key Tailwind Classes
stickyposition: sticky
fixedposition: fixed
top-0top: 0
z-50z-index: 50
Frequently Asked Questions
position: sticky keeps the element in the document flow until it reaches the specified offset (like top: 0), then it becomes fixed. position: fixed immediately removes the element from the document flow and positions it relative to the viewport. Sticky is usually better because it doesn't require adding padding to compensate for the removed element.
The most common reasons are: 1) A parent element has overflow: hidden or overflow: auto, which breaks sticky positioning. 2) You forgot to add a top, bottom, left, or right value. 3) The parent container doesn't have enough height for the sticky element to scroll within. Check your ancestor elements for overflow properties and ensure top: 0 is set.
For a pure CSS solution, you can use scroll-driven animations (in supported browsers) or always show the shadow. For JavaScript, add an event listener to the scroll event and toggle a class when scrollY > 0. The class can add box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1) to the header.
Use z-index: 50 to z-index: 100 for sticky headers. This is high enough to stay above most content but leaves room for modals and overlays (which typically use z-index: 1000+). Tailwind uses z-50 as a common value for headers.
Apply position: sticky directly to the header element, not to wrapper elements. If your header is inside a wrapper, make sure the wrapper doesn't have overflow: hidden. You can also use position: sticky on just the nav element inside the header if that's what you want to stick.
More CSS Tutorials
Copy CSS from Website
Extract styles with 2 clicks
Center a Div (CSS)
Flexbox, Grid, and more
Center a Div (Tailwind)
Utility classes for centering
Convert CSS to Tailwind
CSS to utility classes
Glassmorphism Effect
Frosted glass UI style
Neumorphism Button
Soft UI button design
Responsive Navbar
Mobile-friendly navigation
CSS Pagination
Page navigation styles
CSS Accordion
Expandable content sections
Dropdown Menu
Hover and click dropdowns
CSS Modal
Popup dialog boxes
Toast Notifications
Animated alert messages
Hamburger Menu
Animated menu icon
Animate Gradients
Moving gradient backgrounds
Skeleton Loader
Loading placeholder UI
Element Screenshot
Capture any element as image
Pick Color from Website
Eyedropper tool comparison
Identify Fonts
Find fonts on any website
Download All Images
Bulk save images from any site
Measure Elements
Page ruler and measurements
Extract Colors from Website
Get any color palette instantly
Dark Mode Toggle
CSS variables and localStorage
Responsive Grid
CSS Grid auto-fit and minmax
Center Text in CSS
text-align, Flexbox, and Grid
Make Text Bold
font-weight values explained
Add Shadow in CSS
box-shadow, text-shadow, drop-shadow
Round Corners in CSS
border-radius and pill shapes
Style File Input
Custom upload buttons
