Add an is-scrolled class to your header after the visitor scrolls, so you can shrink it or add a shadow with CSS.
document.addEventListener( 'DOMContentLoaded', () => {
const header = document.querySelector( '.site-header, header' );
if ( ! header ) {
return;
}
const onScroll = () => {
header.classList.toggle( 'is-scrolled', window.scrollY > 10 );
};
window.addEventListener( 'scroll', onScroll, { passive: true } );
onScroll();
} );
/* Then style it in your CSS, for example:
.site-header.is-scrolled { box-shadow: 0 2px 12px rgba(0, 0, 0, .12); padding-block: 8px; }
*/
Sticky headers often look better with a subtle shadow or a slimmer height once the page scrolls. CSS can’t detect scrolling by itself, so this tiny script adds a class you can style.
The script finds your header, first by the common .site-header class and then any <header> element, and toggles is-scrolled whenever the page is more than 10 pixels from the top. The scroll listener is passive, so it doesn’t slow scrolling.
All the visual changes happen in CSS, which keeps the effect smooth and easy to adjust.
transition: padding .2s, box-shadow .2s; to the header for a smooth change.Scroll down a page and inspect the header in DevTools. It should gain the is-scrolled class.