Sticky Header That Shrinks on Scroll
Keeps your theme's header pinned to the top of the screen and adds a shadow and smaller padding once the visitor scrolls down.
Opens WPIDE on
Code
<style>
.wpide-sticky-header{position:sticky;top:0;z-index:999;transition:box-shadow .2s ease,padding .2s ease;}
.admin-bar .wpide-sticky-header{top:32px;}
@media (max-width:782px){.admin-bar .wpide-sticky-header{top:46px;}}
.wpide-sticky-header.is-scrolled{box-shadow:0 2px 12px rgba(0,0,0,.12);padding-top:6px !important;padding-bottom:6px !important;}
</style>
<script>
(function () {
var header = document.querySelector('.site-header, header.wp-block-template-part, body > header, #masthead');
if (!header) { return; }
header.classList.add('wpide-sticky-header');
var toggle = function () { header.classList.toggle('is-scrolled', window.pageYOffset > 40); };
window.addEventListener('scroll', toggle, { passive: true });
toggle();
})();
</script>
Notes
Change the selector in the script if your theme's header is not a <header> element or .site-header.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.