The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Create vertical scrolling.panel { max-block-size: 24rem; overflow-y: auto; }View examples
Reserve scrollbar space.panel { scrollbar-gutter: stable; }View examples
Smooth programmatic scrollhtml { scroll-behavior: smooth; }View examples
Respect reduced motion@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; }; }View examples
Enable proximity snap.carousel { scroll-snap-type: inline proximity; }View examples
Align a snap target.slide { scroll-snap-align: start; }View examples
Require a stop.critical-step { scroll-snap-stop: always; }View examples
Offset the scrollporthtml { scroll-padding-block-start: 5rem; }View examples
Offset one targetsection { scroll-margin-block-start: 5rem; }View examples
Contain scroll chaining.modal-body { overscroll-behavior: contain; }View examples
Suppress boundary effects.canvas { overscroll-behavior: none; }View examples
Stable target link<a href="#installation">Installation</a>View examples

Scrolling is a core navigation mechanism, not merely an animation surface. Constrain only intentional regions, preserve keyboard and touch access, use proximity snapping for flexible content, offset anchored targets from sticky UI, and avoid trapping users by suppressing overscroll too broadly.

Step by step

Detailed examples

01

Create the fewest nested scroll regions possible

overflow:auto exposes scrolling only when needed; the box needs a constrained size. Nested regions can make wheel, touch, and keyboard navigation confusing. stable scrollbar gutters reduce width shifts without forcing an always-visible scrollbar.

Scrollable log with stable geometry
.log { max-block-size: 24rem; overflow-y: auto; scrollbar-gutter: stable; border: 1px solid #94a3b8; }
Back to quick reference ↑
02

Apply smooth scrolling as a preference

scroll-behavior affects navigation and programmatic scroll, not direct user wheel or touch behavior. The user agent may ignore it. Large animated travel can disorient, so disable it under reduced-motion preference and do not delay focus updates to imitate scrolling.

Motion-aware document navigation
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
Back to quick reference ↑
03

Prefer proximity snapping for variable content

scroll-snap-type defines an axis and strictness; children opt in with scroll-snap-align. mandatory snapping can make oversized content impossible to reach because the viewport keeps returning to a snap position. Test keyboard, touch, zoom, and dynamic sizes.

Horizontal proximity carousel
.carousel { display: flex; gap: 1rem; overflow-x: auto; scroll-snap-type: inline proximity; }
.slide { flex: 0 0 min(80%, 24rem); scroll-snap-align: start; }
Back to quick reference ↑
04

Keep anchored content clear of sticky headers

scroll-padding applies to the scroll container and scroll-margin to a target. Both influence snapping and scroll-into-view geometry without fake spacer elements. Preserve native fragment links because they support URLs, history, and no-script navigation.

Anchor offset for a sticky site header
html { scroll-padding-block-start: 5rem; }
[id] { scroll-margin-block-start: 5rem; }
.site-header { position: sticky; inset-block-start: 0; }
Back to quick reference ↑
05

Contain scroll chaining only at genuine boundaries

overscroll-behavior:contain stops an inner region from scrolling its ancestor at the boundary while retaining platform boundary feedback. none suppresses both chaining and effects such as pull-to-refresh. Restrict it to dialogs, maps, or canvases with a specific interaction requirement.

Contained modal body
.dialog-body { max-block-size: 70dvb; overflow-y: auto; overscroll-behavior-block: contain; }
Back to quick reference ↑

Local code tester

Try scroll snapping and offsets

Scroll a proximity carousel and adjust snap alignment, gutter, and overscroll behavior.

Runs in your browser
Preview

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. World Wide Web ConsortiumCSS Scroll Snap Module Level 1w3.org
  2. World Wide Web ConsortiumCSS Overflow Module Level 3w3.org
  3. World Wide Web ConsortiumCSS Overscroll Behavior Module Level 1w3.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback