Categories
JavaScript Answers

How to make anchor link go some pixels above where it’s linked to with CSS?

Spread the love

Sometimes, we want to make anchor link go some pixels above where it’s linked to with CSS.

In this article, we’ll look at how to make anchor link go some pixels above where it’s linked to with CSS.

How to make anchor link go some pixels above where it’s linked to with CSS?

To make anchor link go some pixels above where it’s linked to with CSS, we set the scroll-margin-top style.

For instance, we write

#anchor {
  scroll-margin-top: 100px;
}

to set the scroll-margin-top to 100px to make the browser scroll 100px above the element where the anchor element is when the link for the anchor element is clicked.

Conclusion

To make anchor link go some pixels above where it’s linked to with CSS, we set the scroll-margin-top style.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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