A sticky navigation bar scrolls with the page until it reaches the top of the window, then stays there. In 2022 the usual way to build one was a jQuery scroll handler that switched the bar to position: fixed. Today CSS does it on its own with position: sticky, which every current browser supports.
This guide covers three methods, from simplest to most flexible:
- CSS only with
position: sticky. Use this by default. - CSS plus a little JavaScript to add a class when the bar sticks, for example to add a shadow.
- jQuery, for older themes that already load jQuery and switch the bar to
position: fixed.
Method 1: CSS position: sticky (no JavaScript)
Start with a header, the navigation bar and the page content. The nav should be a direct child of <body> (or of a tall wrapper). Don’t nest it inside the short header, for the reason explained in the callout below.
<header class="site-header">
<h1>My site</h1>
</header>
<nav class="site-nav" id="site-nav" aria-label="Main">
<a href="/" aria-current="page">Home</a>
<a href="/about/">About</a>
<a href="/blog/">Blog</a>
<a href="/contact/">Contact</a>
</nav>
<main class="page-content">
<p>Long page content…</p>
</main>Now add two declarations to the nav: position: sticky and the offset where it should stop, top: 0.
.site-nav {
position: sticky;
top: 0;
z-index: 100;
display: flex;
gap: 16px;
justify-content: center;
padding: 12px 0;
background: #111;
}
.site-nav a {
color: #fff;
font-weight: 600;
text-decoration: none;
}That is the whole method. The bar scrolls normally until its top edge reaches the top of the window, then it stays there. It also keeps its space in the layout, so the content below doesn’t jump.
Sticky not working?
A sticky element only sticks inside its parent. If the nav is inside a header that is only as tall as the nav, it has nowhere to stick. It also stops working when any ancestor has overflow: hidden, auto or scroll. Move the nav out of those containers, or remove the overflow rule.
Method 2: add a class when the bar sticks
CSS can’t tell you when a sticky element is stuck. If you want the bar to change once it sticks, for example by adding a shadow or shrinking the logo, you need a small script. Put an empty “sentinel” element just above the nav, then watch it with IntersectionObserver. When the sentinel scrolls out of view, the nav is stuck.
<div class="nav-sentinel" aria-hidden="true"></div>
<nav class="site-nav" id="site-nav" aria-label="Main">
…
</nav>const nav = document.querySelector('.site-nav');
const sentinel = document.querySelector('.nav-sentinel');
if (nav && sentinel && 'IntersectionObserver' in window) {
const observer = new IntersectionObserver(([entry]) => {
// The sentinel sits just above the nav. Once it leaves the
// viewport, the nav is stuck to the top.
nav.classList.toggle('is-stuck', !entry.isIntersecting);
});
observer.observe(sentinel);
}.site-nav.is-stuck {
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
}IntersectionObserver runs only when the sentinel crosses the edge of the viewport, not on every scroll event, so it is cheaper than a scroll listener. If the script fails, the nav still sticks because the CSS from Method 1 does the work. You only lose the shadow.
Method 3: the jQuery version
Many older WordPress themes already load jQuery. If yours does and you can’t change the CSS approach, this is a safer version of the classic technique. It works with jQuery 3 and jQuery 4. For this method, leave position: sticky out of your CSS and add a class that fixes the bar instead:
/* No position: sticky here: jQuery adds .is-fixed instead. */
.site-nav.is-fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
}jQuery(function ($) {
var $nav = $('#site-nav');
var $win = $(window);
if (!$nav.length) {
return;
}
var navTop = $nav.offset().top;
function update() {
var fixed = $win.scrollTop() >= navTop;
$nav.toggleClass('is-fixed', fixed);
// Reserve the nav's height so the content doesn't jump up.
$('body').css('padding-top', fixed ? $nav.outerHeight() : '');
}
$win.on('scroll', update);
update();
});Three changes from the version this article used to show:
- No content jump. A fixed element leaves the page flow, so the content below moves up by the nav’s height. Padding the body by that height stops the jump.
jQuery(function ($) { … })instead of$(document).ready(). WordPress loads jQuery in no-conflict mode, where$isn’t defined globally. This wrapper gives you$safely..on('scroll', …)instead of the.scroll()shorthand, which has been deprecated since jQuery 3.3.
In a WordPress theme, load the file with wp_enqueue_script() and list jquery as a dependency. Don’t paste a <script> tag into the header. Our guide to enqueueing JavaScript and CSS in WordPress explains each argument.
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script(
'mytheme-sticky-nav',
get_theme_file_uri( 'assets/js/sticky-nav.js' ),
array( 'jquery' ),
filemtime( get_theme_file_path( 'assets/js/sticky-nav.js' ) ),
true
);
} );Which method should you use?
| Method | JavaScript | Content jump | Best for |
|---|---|---|---|
CSS position: sticky |
None | No | Almost every site |
Sticky + IntersectionObserver |
About 10 lines | No | Changing the bar's style once it sticks |
jQuery + position: fixed |
jQuery plus about 15 lines | Only if you skip the padding | Legacy themes that already load jQuery |
Fixing common problems
- The bar slides under the WordPress admin bar. Logged-in users see a 32px admin bar (46px on small screens). Offset the nav for them, as shown below.
- Dropdowns or page content cover the bar. Give the nav a
z-indexand a solid background colour. - Clicking an in-page link hides the heading under the bar. Add
scroll-margin-topto your headings, set to the height of the bar, so anchor links stop below it.
/* Keep the nav below the WordPress admin bar for logged-in users. */
body.admin-bar .site-nav {
top: 32px;
}
@media (max-width: 782px) {
body.admin-bar .site-nav {
top: 46px;
}
}Before you go
Key takeaways
position: stickywithtop: 0makes a nav sticky with no JavaScript.- Sticky stops working inside a short parent or an ancestor with
overflowset. - Use
IntersectionObserverand a sentinel element when you need to know the bar is stuck. - With jQuery and
position: fixed, pad the body by the nav's height so the content doesn't jump. - In WordPress, enqueue the script with
jqueryas a dependency and usejQuery(function ($) { … }).
Official documentation
- position (including sticky), MDN Web Docs.
- Intersection Observer API, MDN Web Docs.
- .scrollTop(), jQuery API.
Comments