Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I use CSS to create a sticky header that remains at the top during scrolling?
Asked on Mar 09, 2026
Answer
To create a sticky header using CSS, you can utilize the "position: sticky" property. This allows an element to stick to a defined position within its parent container as you scroll.
<!-- BEGIN COPY / PASTE -->
<style>
header {
position: sticky;
top: 0;
background-color: #f8f9fa;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
</style>
<header>
<h1>Sticky Header</h1>
</header>
<div style="height: 2000px;">
<p>Scroll down to see the sticky header in action.</p>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The "position: sticky" property combines the characteristics of "relative" and "fixed" positioning.
- Setting "top: 0" ensures the header sticks to the top of the viewport.
- Ensure the parent element has a height greater than the viewport for the sticky effect to work.
- Use "z-index" to keep the header above other content.
Recommended Links:
