document.querySelector('a[href^="#"]').addEventListener('click', (e) => {
e.preventDefault()
document
.querySelector(e.currentTarget.getAttribute('href'))
.scrollIntoView({
behavior: 'smooth',
block: 'center'
})
})
.filler {
/* CSS Pattern by Logan McBroom (http://logan.mcbroom.me/) */
background: linear-gradient(63deg, #999 23%, transparent 23%) 7px 0, linear-gradient(63deg, transparent 74%, #999 78%), linear-gradient(63deg, transparent 34%, #999 38%, #999 58%, transparent 62%), #444;
background-size: 16px 48px;
height: 5000px;
}
<a id="firstlink" href="#thespot">Scroll <i>smooth</i> and centered.</a>
<br/><br/>
<a href="#thespot">Scroll classic <b>janky headbutt</b> style.</a>
<br/><br/>
<div class="filler"></div>
<p id="thespot">jump to me!</p>
<div class="filler"></div>