You can do it using JavaScript but it's not necessary.
<a class="navigate" href="#section-1">Scroll</a>
<script>
document.querySelector('.navigate').forEach(( a ) => {
a.addEventListener('click', (ev) => {
ev.preventDefault();
const id = ev.target.id;
document.getElementById(id).scrollIntoView();
})
})
Here we prevent the default action when clicking the link by using preventDefault
. Then using JavaScript we get the ID of the element and scroll to the view using JavaScript.
Note that to make that function reusable, we add the class navigate
to all the anchors that have the same behavior