{"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 <\/p>\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 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 That sort of a jump can be jarring. That’s where The The following demo will only work on Chrome 61+, Firefox 36+, and Opera 48+ at the time of this writing.<\/p>\n See the Pen Scrolling Box w\/ `scroll-behavior`<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>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
Deeper Explanation<\/h3>\n
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
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
Syntax<\/h3>\n
.module {\r\n scroll-behavior: [ auto | smooth ];\r\n}<\/code><\/pre>\n
Values<\/h3>\n
scroll-behavior<\/code> property accepts two values, which essentially toggle the smooth scrolling feature on and off.<\/p>\n
\n
auto<\/code> (default): This value allows the abrupt jump between elements within the scrolling box.<\/li>\n
smooth<\/code>: True to its name, this value is the smooth animated transition between elements within the scrolling box.<\/li>\n<\/ul>\n
Demo<\/h3>\n
Browser Support<\/h3>\n
Desktop<\/h4>