---
title: "How do I add smooth scrolling to anchor links?"
description: "Add scroll-behavior: smooth to the html element, use scroll-margin-top so sticky headers do not hide the target, and turn it off for people who prefer reduced motion."
url: https://www.danny.engineering/answer/css/how-do-i-add-smooth-scrolling-to-anchor-links
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["CSS"]
---

# How do I add smooth scrolling to anchor links?

## The short answer


You do not need JavaScript. Set `scroll-behavior: smooth` on the `html` element, but only for people who have not asked for reduced motion. If you have a sticky header, give the targets a `scroll-margin-top` so they do not end up hidden underneath it.


```css
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

:target {
  scroll-margin-top: 5rem;
}
```


## Why it happens


When someone clicks a link like `href="#pricing"`, the browser jumps instantly to the element with `id="pricing"`. That jump is the default because `scroll-behavior` starts as `auto`.


Setting `scroll-behavior: smooth` on a scroll container changes how that container scrolls when the scroll is triggered by navigation or by script, such as anchor links, `element.scrollIntoView()` and `window.scrollTo()`. It does not change scrolling with the mouse wheel, trackpad or keyboard. For the page itself, the scroll container is the root element, so the property goes on `html`, not `body`.


The sticky header problem happens because the browser scrolls the target to the very top of the viewport. A header with `position: sticky` or `position: fixed` sits on top of that area and covers the heading you just linked to.


## How to fix it


### Add smooth scrolling on the root


```css
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}
```


The media query means the smooth effect only applies when the person has not turned on a reduced motion setting in their operating system. Long animated scrolls can cause nausea or dizziness for people with vestibular disorders, so treat this as the default, not an extra.


If you scroll a different element, such as a chat panel or a sidebar with `overflow: auto`, put `scroll-behavior` on that element instead.


### Offset targets for a sticky header


Use `scroll-margin-top` on the elements you link to. It adds space above the target when it is scrolled into view, without changing the layout.


```css
:root {
  --header-height: 4rem;
}

.site-header {
  position: sticky;
  top: 0;
  height: var(--header-height);
}

h2[id],
h3[id] {
  scroll-margin-top: calc(var(--header-height) + 1rem);
}
```


Alternatively, set the offset once on the scroll container with `scroll-padding-top`. This applies to every anchor on the page.


```css
html {
  scroll-padding-top: calc(var(--header-height) + 1rem);
}
```


Use `scroll-padding-top` when the offset is the same everywhere, and `scroll-margin-top` when certain targets need different spacing.


### Smooth scrolling from JavaScript


When you scroll from code, pass the behaviour directly and check the same preference, so you do not need the CSS rule for it.


```javascript
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

document.querySelector("#pricing").scrollIntoView({
  behavior: reduceMotion ? "auto" : "smooth",
  block: "start",
});
```


`scrollIntoView()` also respects `scroll-margin-top`, so your sticky header offset still works.


## Common pitfalls

- **Putting it on body.** For page scrolling the property must go on `html`. On `body` it usually does nothing.
- **Ignoring reduced motion.** Always wrap the rule in `prefers-reduced-motion: no-preference`.
- **Every scripted scroll is affected.** The rule on `html` also animates `window.scrollTo()` calls made by your own code or a library, such as a "back to top" button. On very long pages that can feel slow, so pass `behavior: "instant"` where you need a jump.
- **Focus does not move.** Scrolling to an anchor does not always move keyboard focus to the target. If the target is not focusable, add `tabindex="-1"` to it, or move focus with JavaScript, so keyboard users continue from the right place.
- **A height on html or body.** Styles like `height: 100%` with `overflow: auto` on `body` turn `body` into the scroll container, and the rule on `html` stops working.

## Related reading

- [MDN: scroll-behavior](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior)
- [MDN: scroll-margin-top](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-margin-top)
