CSS Academy · Lesson

scroll-behavior: smooth

Enable smooth scrolling for anchor links and programmatic scrolls with scroll-behavior.

Lesson 2 of 413 steps

scroll-behavior: smooth is a free CSS Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

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:

  <p>Scrolling on this page is smooth thanks to <code>html { scroll-behavior: smooth; }</code>.</p>

Instant vs Smooth

Values:

  • auto (default): instant jump
  • smooth: 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>
  <section id="about"><h3>About</h3></section>
  <section id="work"><h3>Work</h3></section>
  <section id="contact"><h3>Contact</h3></section>

JavaScript scrollTo() with Smooth

Pass behavior: "smooth" to JavaScript scroll methods for animated scrolling:

  <pre class="snippet">window.scrollTo({ top: 0, behavior: 'smooth' });

document.querySelector('#section').scrollIntoView({
  behavior: 'smooth',
  block: 'start'
});</pre>

prefers-reduced-motion Interaction

Smooth scrolling can trigger motion sickness. Always disable it for users who prefer reduced motion:

  <p>Respects <code>prefers-reduced-motion</code>.</p>

scroll-behavior on Containers

Apply to any scrollable container, not just the page:

  <div class="tabs-panel">
    <div class="tab">Tab 1</div><div class="tab">Tab 2</div><div class="tab">Tab 3</div>
  </div>

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:

  <p>Anchor targets offset by fixed header height.</p>

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:

  <div class="section">Section content</div>

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.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

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. 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. The CSS Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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.

All lessons in this course

  1. overflow: hidden scroll auto clip
  2. scroll-behavior: smooth
  3. scroll-snap-type and scroll-snap-align
  4. Scrollbar Styling with ::-webkit-scrollbar
← Back to CSS Academy