CSS scroll-behavior Property

The scroll-behavior property sets the behavior for a scrolling box when scrolling is triggered by navigation or CSSOM scrolling APIs.

selector { scroll-behavior: auto | smooth; }
auto The scrolling box jumps to the new position immediately without any transition animation.
smooth The scrolling box moves to the new position using a smooth animated transition over a period of time defined by the browser.

Code Examples

A basic implementation applying smooth scrolling to the entire document for anchor link navigation.

<style>
  html {
    scroll-behavior: smooth;
  }
  section {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: sans-serif;
  }
  #one { background: #f4f4f4; }
  #two { background: #e2e2e2; }
  nav { position: fixed; top: 10px; left: 10px; }
</style>

<nav>
  <a href="#one">Section 1</a>
  <a href="#two">Section 2</a>
</nav>

<section id="one"><h1>First Section</h1></section>
<section id="two"><h1>Second Section</h1></section>

An advanced approach using JavaScript to dynamically enable smooth scrolling on a specific container while respecting accessibility settings.

<style>
  #scrollBox {
    width: 300px;
    height: 200px;
    overflow-y: auto;
    border: 2px solid #333333;
  }
  .content-block {
    height: 400px;
    padding: 20px;
    background-color: #ffffff;
  }
  /* Respect user motion preferences */
  @media (prefers-reduced-motion: no-preference) {
    .is-smooth {
      scroll-behavior: smooth;
    }
  }
</style>

<div id="scrollBox">
  <div id="item1" class="content-block">Start of content</div>
  <div id="item2" class="content-block">Middle of content</div>
  <div id="item3" class="content-block">End of content</div>
</div>

<button onclick="smartScroll()">Smooth Scroll to Bottom</button>

<script>
function smartScroll() {
  const box = document.getElementById("scrollBox");
  const target = document.getElementById("item3");
  
  // Add class to enable smooth behavior dynamically
  box.classList.add("is-smooth");
  
  // Trigger the scroll
  box.scrollTop = target.offsetTop;
  
  // Log status to console
  console.log("Scrolling to: " + target.offsetTop + "px");
}
</script>

Pro Tip

You can use JavaScript to toggle this property on the fly. If you want a specific button click to slide smoothly but want all other navigation to be instant, add a "smooth-scroll" class to your container just before triggering the scroll event, then remove it afterward using a timer or the "scrollend" event.

Deep Dive

Think of scroll-behavior as the difference between teleportation and a car ride. With the default "auto" value, your view teleports to the destination instantly. When set to "smooth", the browser handles the driving for you, sliding the viewport from point A to point B. This property is typically applied to the "html" element to affect the entire page, but it works on any container with overflow. It specifically targets scrolls triggered by anchor link clicks (like #top) or window.scrollTo calls in your scripts. It does not change the physics of manual scrolling performed via a mouse wheel or touch flick.

Best Practices

Apply scroll-behavior: smooth to the "html" element to give your site a modern, polished feel when users click internal navigation links. However, don't force it on everyone. Always wrap this behavior in a media query that checks for user motion preferences to ensure you aren't causing discomfort for people sensitive to screen movement.

Common Pitfalls

A common mistake is thinking this property affects how the mouse wheel feels; it doesn't. Another issue occurs when developers have very long pages; a "smooth" scroll might take several seconds to reach the bottom, which can frustrate users who just want to get to the footer quickly. Also, remember that this property is not inherited, so if you want it on a specific div with scrollbars, you must apply it directly to that div.

Accessibility

Some users suffer from vestibular disorders where sliding animations cause nausea or dizziness. It is critical to use the "prefers-reduced-motion" media query. If a user has indicated they want less motion in their operating system settings, you should respect that by setting the behavior back to "auto".

Dev Data Table: scroll-behavior property

default auto
animatable no
inherited no
experimental no
year_intro 2013
year_standard 2019
js_syntax_1 element.style.scrollBehavior = "smooth";
js_syntax_2 element.style.setProperty("scroll-behavior", "smooth");
js_note When using JavaScript to scroll an element, this property dictates if the movement is instant or animated, unless the scroll method itself specifies the behavior.
browsers { "Chrome": 61, "Edge": 79, "Firefox": 36, "Safari": 15.4, "Opera": 48, "Chrome Android": 61, "Safari on iOS": 15.4, "Samsung Internet": 8, "Opera Mobile": 45 }
results render here...