How to scroll to a specific Element using HTML and CSS?

Spread the love

To scroll to a specific Element using HTML and CSS, we add an ID to the element we want to scroll to and set the href of a link to the selector for the element.

For instance, we write

<a href="#example">click me</a>
<div id="example"></div>

to add a link that scrolls to the div with ID example by setting its href attribute to #example.

Then we add

html {
  scroll-behavior: smooth;
}

to make the scroll behavior smooth when we click on the link.

Leave a Reply

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