scroll-behavior: smooth
Enable smooth scrolling for anchor links and programmatic scrolls with scroll-behavior.
scroll-behavior: smooth is a free CSS Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is scroll-behavior?
scroll-behavior controls whether scrolling that is triggered by navigation (anchor links, JavaScript scrollTo()) should animate smoothly or jump instantly.
scroll-behavior: smooth
Setting scroll-behavior: smooth on the html or body element makes all page anchor link navigation animate smoothly:
html {
scroll-behavior: smooth;
}Instant vs Smooth
Values:
auto(default): instant jumpsmooth: animated scroll to the target
Anchor Link Navigation
With smooth scrolling enabled, clicking a link like <a href="#contact"> smoothly scrolls to the element with id="contact" instead of jumping instantly.
<nav>
<a href="#about">About</a>
<a href="#work">Work</a>
<a href="#contact">Contact</a>
</nav>JavaScript scrollTo() with Smooth
Pass behavior: "smooth" to JavaScript scroll methods for animated scrolling:
window.scrollTo({ top: 0, behavior: 'smooth' });
document.querySelector('#section').scrollIntoView({
behavior: 'smooth',
block: 'start'
});prefers-reduced-motion Interaction
Smooth scrolling can trigger motion sickness. Always disable it for users who prefer reduced motion:
html {
scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}scroll-behavior on Containers
Apply to any scrollable container, not just the page:
.tabs-panel {
overflow-x: auto;
scroll-behavior: smooth;
}Limitations
CSS scroll-behavior does not give control over scroll duration or easing. For precise animation control, use the JavaScript Web Animations API or libraries like GSAP with their scroll plugins.
scroll-padding-top for Fixed Headers
When a fixed header overlaps anchor targets, scroll-padding-top adds an offset so the target is not hidden behind the header:
html {
scroll-behavior: smooth;
scroll-padding-top: 80px; /* height of fixed nav */
}scroll-margin vs scroll-padding
scroll-margin adds offset directly on the target element; scroll-padding adds offset to the scroll container. Both solve the fixed-header problem:
/* On target elements: */
.section { scroll-margin-top: 80px; }
/* On the scroll container: */
html { scroll-padding-top: 80px; }Accessibility Note
Smooth scrolling is a UX enhancement. Always ensure anchor navigation works even without it — do not rely on smooth scrolling behavior for functionality, only for visual polish.
Quick Check
How do you ensure smooth scroll animation is disabled for users with vestibular motion sensitivity?
Recap
scroll-behavior: smooth on html enables smooth animated scrolling for anchor links and JavaScript scroll methods. Use scroll-padding-top to offset scroll targets from behind fixed headers. Always disable smooth scrolling inside a prefers-reduced-motion: reduce media query for accessibility.
Frequently asked questions
Is the “scroll-behavior: smooth” lesson free?
Yes — the full text of “scroll-behavior: smooth” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “scroll-behavior: smooth”?
Enable smooth scrolling for anchor links and programmatic scrolls with scroll-behavior. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “scroll-behavior: smooth” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.