Theme & Front End CSS Beginner

Smooth Scrolling for Anchor Links

One CSS rule for smooth jumps to page sections, which switches off for visitors who prefer reduced motion.

Tested on WordPress 7.1 No plugin needed Updated
style.css 5 lines
@media (prefers-reduced-motion: no-preference) {
	html {
		scroll-behavior: smooth;
	}
}

Where to add this

Recommended Additional CSS Appearance → Customize → Additional CSS, or your page builder's global CSS.
Child theme At the end of your child theme's style.css file.
A code snippets plugin As a CSS snippet set to load on the front end.

Clicking a “Jump to section” link or a table of contents normally snaps instantly to the target. Smooth scrolling animates the jump, so visitors can see where they’ve moved to on the page.

How it works

scroll-behavior: smooth on the html element tells the browser to animate every in-page jump: anchor links, #hash URLs and scripts that scroll to an element.

The rule sits inside prefers-reduced-motion: no-preference, so people who’ve asked their device to reduce motion, often because animation makes them feel unwell, still get instant jumps.

Things to watch

  • If your header is sticky, headings can end up hidden behind it. Pair this with the “Stop Sticky Headers Covering Anchor Targets” snippet.
  • Some themes and page builders add their own smooth scrolling in JavaScript. Use one or the other.

How to test it

Add a link like <a href="#comments"> to a post and click it. The page should glide down instead of jumping.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only