Automatically adjust scroll-padding-top for “Skip to main content” links (useful for .sticky-top headers)

window.addEventListener('resize', () => {
    document.documentElement.style.scrollPaddingTop = document.getElementById('content').getBoundingClientRect().top
})

Tip: Set default using CSS in case JavaScript is disabled.

html {
    scroll-padding-top: 50px;
}

Leave a Reply

Your email address will not be published. Required fields are marked *