{"id":253758,"date":"2017-04-18T05:33:03","date_gmt":"2017-04-18T12:33:03","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=253758"},"modified":"2020-01-30T09:03:43","modified_gmt":"2020-01-30T16:03:43","slug":"scroll-behavior","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/s\/scroll-behavior\/","title":{"rendered":"scroll-behavior"},"content":{"rendered":"

The scroll-behavior<\/code> property in CSS allows us to define whether the scroll location of the browser jumps to a new location or smoothly animates<\/em> the transition when a user clicks a link that targets an anchored position within a scrolling box.<\/p>\n

html {\r\n  scroll-behavior: smooth;\r\n}<\/code><\/pre>\n

<\/p>\n

Deeper Explanation<\/h3>\n

Wait, wait, what’s this scrolling box we speak of?<\/em> It’s an element with content that overflows its bounds.<\/p>\n

See the Pen Scrolling Box<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n

Notice how the box in the demo above has a fixed height of 200px<\/code>? Any content that exceeds that height is outside the bounds of the box and we’ve added overflow-y: scroll<\/code> to make that additional content accessible with vertical scrolling. That is what we mean by a scrolling box.<\/p>\n

Now let’s say we add a navigation to the top of the box with each link targeting the three sections of content:<\/p>\n

See the Pen Scrolling Box w\/Nav<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n

Each link takes the user directly to the different sections of content inside of the scrolling box. By default, that transition between is an abrupt jump.<\/p>\n

\"\"
The jump between content is abrupt and sudden by default.<\/figcaption><\/figure>\n

That sort of a jump can be jarring. That’s where scroll-behavior<\/code> comes in and allows us to set a smooth scrolling transition. This sort of thing used to take fancy Javascript<\/a> but scroll-behavior<\/code> will give us the ability to do that natively in CSS, once browser support improves.<\/p>\n

\"\"
The jump between content is animated in a smooth transition.<\/figcaption><\/figure>\n

Syntax<\/h3>\n
.module {\r\n  scroll-behavior: [ auto | smooth ];\r\n}<\/code><\/pre>\n

Values<\/h3>\n

The scroll-behavior<\/code> property accepts two values, which essentially toggle the smooth scrolling feature on and off.<\/p>\n