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.
_10@media (prefers-reduced-motion: no-preference) {_10 html {_10 scroll-behavior: smooth;_10 }_10}_10_10:target {_10 scroll-margin-top: 5rem;_10}
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
_10@media (prefers-reduced-motion: no-preference) {_10 html {_10 scroll-behavior: smooth;_10 }_10}
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.
_14:root {_14 --header-height: 4rem;_14}_14_14.site-header {_14 position: sticky;_14 top: 0;_14 height: var(--header-height);_14}_14_14h2[id],_14h3[id] {_14 scroll-margin-top: calc(var(--header-height) + 1rem);_14}
Alternatively, set the offset once on the scroll container with scroll-padding-top. This applies to every anchor on the page.
_10html {_10 scroll-padding-top: calc(var(--header-height) + 1rem);_10}
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.
_10const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;_10_10document.querySelector("#pricing").scrollIntoView({_10 behavior: reduceMotion ? "auto" : "smooth",_10 block: "start",_10});
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. Onbodyit usually does nothing. - Ignoring reduced motion. Always wrap the rule in
prefers-reduced-motion: no-preference. - Every scripted scroll is affected. The rule on
htmlalso animateswindow.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 passbehavior: "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%withoverflow: autoonbodyturnbodyinto the scroll container, and the rule onhtmlstops working.
Related reading
#CSSquick question
Was this resource helpful?