💨Super Quick CSS: Smooth Scrolling
Carl Saunders

Carl Saunders @deadlybyte

About: I'm a full stack software developer specialising in React. With 20+ years’ experience, I'm passionate about following best practices and standards.

Location:
Poole, United Kingdom
Joined:
Jul 31, 2018

💨Super Quick CSS: Smooth Scrolling

Publish Date: Jan 22 '20
139 12

Transform Your Website's UX With This One Liner

html {
  scroll-behavior: smooth;
}
Enter fullscreen mode Exit fullscreen mode

If like me you've got a single page website and your using bookmarks (anchors tags that reference an element by id), allowing user to navigate to different sections of the website. Then you need to add this CSS property scroll-behavior: smooth;, which will smoothly scroll the page content into view.

Working Example

Below the CodePen demonstrates the smooth scrolling of the content when you click one of the navigation links.

Demo

Before - Without Smooth Scrolling

Before - Without Smooth Scrolling

After - With Smooth Scrolling

After - With Smooth Scrolling

Things To Note

The scroll-behavior property doesn't seem to work on the body element, so best to add it to the html element.

This property isn't currently support by all browsers, but this shouldn't stop you using it as it won't break and defaults to the standard behavior.

https://caniuse.com/#feat=mdn-css_properties_scroll-behavior

Comments 12 total

  • Mohammad Fazel
    Mohammad FazelJan 22, 2020

    That was nice, thank you!

    • Carl Saunders
      Carl SaundersJan 22, 2020

      No worries, wish everything was this simple to implement. There's really no excuse not to implement this.

  • lamka02sk
    lamka02skJan 23, 2020

    Thank you for the article! Never heard of this cool feature before.

  • Beicker
    BeickerJan 23, 2020

    I was playing with your code (on codepen.io) and i didn't see any difference between using that line and not using it. I don't know, maybe i am too noob or my browser (chrome) is the problem.

    In the second case, ¿what browser can i use for it? Thanks for repplying me.

    • Nejc
      NejcJan 23, 2020

      I tried chrome mobile and desktop and it works - I wonder why the effect persists even if you remove it though

      • Carl Saunders
        Carl SaundersJan 24, 2020

        Did you make the changes via CodePen or locally? As could be a caching issue. My other CodePen without the property works as expected.

    • Hung Nguyen Manh
      Hung Nguyen ManhJan 23, 2020

      You should try again <3
      scroll-behavior supported Chrome and Firefox.

      caniuse.com/#search=scroll-behavior

      developer.mozilla.org/en-US/docs/W...

    • Carl Saunders
      Carl SaundersJan 24, 2020

      I've added some animated gifs to show the before and after UX of applying the scroll-behavior: smooth property. Hopefully this'll make it clearer if your browser doesn't support this feature.

      After - With Smooth Scrolling

      After - With Smooth Scrolling
      • Beicker
        BeickerJan 25, 2020

        Oh i get it. What i was doing is scrolling with my mouse. I didn't clicked the links. I already see the difference. Thanks for the GIFs.

        • Carl Saunders
          Carl SaundersJan 25, 2020

          Glad adding the GIFs cleared up what was happening.

  • ohmyzense
    ohmyzenseFeb 13, 2020

    I love this! Thanks so much for the share on this little tip.

Add comment