Smooth scrolling
Also called smooth scroll or scroll-behavior: smooth
Animated scrolling that glides to a new position, such as the target of an anchor link, so readers can see where on the page they went.
import type { MouseEvent } from 'react';
import { useReducedMotion } from 'motion/react';
const sections = [
{ id: 'features', title: 'Features' },
{ id: 'pricing', title: 'Pricing' },
{ id: 'faq', title: 'FAQ' }
];
export function LandingPage() {
const reduceMotion = useReducedMotion();
function jump(event: MouseEvent<HTMLAnchorElement>, id: string) {
const target = document.getElementById(id);
if (!target) return;
event.preventDefault();
// Glide there, or jump instantly for people who prefer less motion.
target.scrollIntoView({ behavior: reduceMotion ? 'auto' : 'smooth' });
// Move focus as well so the next Tab starts from the section, without cutting the glide short.
target.focus({ preventScroll: true });
history.pushState(null, '', `#${id}`);
}
return (
<>
<nav className='sticky top-0 z-10 flex h-16 items-center gap-6 border-b bg-background px-6 text-sm font-medium'>
{sections.map((section) => (
<a
key={section.id}
href={`#${section.id}`}
onClick={(event) => jump(event, section.id)}
className='text-muted-foreground hover:text-foreground'
>
{section.title}
</a>
))}
</nav>
{sections.map((section) => (
// scroll-mt-20 (80px) keeps the 64px sticky nav from covering the heading.
<section
key={section.id}
id={section.id}
tabIndex={-1}
className='min-h-screen scroll-mt-20 px-6 py-16 outline-none'
>
<h2 className='text-2xl font-semibold'>{section.title}</h2>
</section>
))}
</>
);
}Turn on smooth scrolling for the in-page navigation: set scroll-behavior: smooth on html inside @media (prefers-reduced-motion: no-preference), add scroll-margin-top: 80px to each section so the sticky header doesn’t cover its heading, and move keyboard focus to the target section after the jump.
Paste into Layerwise or any AI coding tool.
What is Smooth scrolling?
By default, clicking a link to #pricing jumps there instantly, which can leave readers unsure whether they moved up or down the page. Smooth scrolling animates the trip: the page glides to the target over a few hundred milliseconds, and the reader keeps their bearings.
One line of CSS turns it on for anchor links and scripted scrolls: scroll-behavior: smooth on the scrolling element, usually html. In JavaScript, pass behavior: 'smooth' to scrollTo() or scrollIntoView(). Add scroll-margin-top to the targets so a sticky header doesn’t cover them.
Some sites also use libraries such as Lenis that smooth out mouse wheel scrolling itself. That changes how every scroll feels, so use it with care. Either way, respect prefers-reduced-motion and jump instantly for people who ask for less motion.
Related terms
All termsScroll snap
A CSS feature that makes a scroll container settle on set points, so carousels and full-screen sections come to rest neatly on an item.
Sticky header
A top bar that stays pinned to the top of the screen while the page scrolls underneath it, so navigation is always one click away.
Ease-in-out
An easing curve that starts slowly, speeds up in the middle, and slows down again at the end, tracing an S shape on a graph.
Reduced motion
An operating system setting that asks apps to minimize movement, which websites detect with the prefers-reduced-motion media query.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.