Skip to main content

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.

Scroll inside to see the effect
Sticky Header - Stays on Top!
Scroll content section 1. Keep scrolling to see the sticky header stay at the top.
Scroll content section 2. Keep scrolling to see the sticky header stay at the top.
Scroll content section 3. Keep scrolling to see the sticky header stay at the top.
Scroll content section 4. Keep scrolling to see the sticky header stay at the top.
Scroll content section 5. Keep scrolling to see the sticky header stay at the top.
Scroll content section 6. Keep scrolling to see the sticky header stay at the top.
Scroll content section 7. Keep scrolling to see the sticky header stay at the top.
Scroll content section 8. Keep scrolling to see the sticky header stay at the top.
Scroll content section 9. Keep scrolling to see the sticky header stay at the top.
Scroll content section 10. Keep scrolling to see the sticky header stay at the top.
1

position: sticky (Recommended)

Recommended

The 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%+)
2

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.

Takes up space in flow

stickyYes
fixedNo (needs padding)

Parent container aware

stickyYes
fixedNo

Browser support

sticky97%+
fixed99%+

Easier to implement

stickyYes
fixedNeeds offset

Works with overflow: hidden parent

stickyNo
fixedYes

Respects container boundaries

stickyYes
fixedNo

Step-by-Step Implementation

Follow these steps to create a sticky header from scratch using position: sticky (recommended).

1

Basic Header Structure

Create your header HTML with navigation or content.

2

Apply position: sticky

Add position: sticky to make the header stick when scrolling.

3

Set top: 0

Define where the element should stick. top: 0 means it sticks to the top of the viewport.

4

Add z-index

Ensure the header appears above other content when scrolling.

5

Add Background Color

Give the header a solid background so content doesn't show through.

6

(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.

position: sticky
Sticky Header
Content block 1
Content block 2
Content block 3
Content block 4
Content block 5
Content block 6
Content block 7
Content block 8
Content block 9
Content block 10
Content block 11
Content block 12
Content block 13
Content block 14
Content block 15
position: fixed (simulated)
Fixed Header
Content block 1
Content block 2
Content block 3
Content block 4
Content block 5
Content block 6
Content block 7
Content block 8
Content block 9
Content block 10
Content block 11
Content block 12
Content block 13
Content block 14
Content block 15

Common Issues and Fixes

Troubleshoot common problems when implementing sticky headers.

Sticky header not working

Cause:

Parent element has overflow: hidden or overflow: auto

Fix:

Remove overflow property from parent elements or use position: fixed instead.

Header doesn't stick

Cause:

Missing top property

Fix:

Always specify where the element should stick with top, bottom, left, or right.

Header disappears while scrolling

Cause:

Parent container doesn't have enough height

Fix:

Ensure the sticky element's parent has more content than the viewport height.

Content hidden behind fixed header

Cause:

Using position: fixed without body padding

Fix:

Add padding-top to body equal to header height.

Z-index not working

Cause:

Other elements have higher z-index or create new stacking contexts

Fix:

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

sticky

position: sticky

fixed

position: fixed

top-0

top: 0

z-50

z-index: 50

Frequently Asked Questions

What's the difference between sticky and fixed?

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.

Why isn't position: sticky working?

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.

How do I add shadow when header becomes sticky?

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.

What's the best z-index for sticky 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.

How do I make only the header sticky, not the whole nav?

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.