CSS scroll-snap-type Property
The CSS scroll-snap-type property controls whether a scroll container snaps to designated positions and how strictly those positions are enforced.
It defines the snapping axis using physical or logical directions and can require snapping with mandatory or allow optional snapping with proximity. It is commonly used for carousels, image galleries, horizontally scrolling cards, and vertically scrolling sections.
scroll-snap-type Syntax
The scroll-snap-type property accepts none or a snapping axis followed by an optional strictness keyword.
Basic Syntax
selector {
scroll-snap-type: x mandatory;
}
Available Values
.container {
scroll-snap-type: none;
}
.container {
scroll-snap-type: x mandatory;
}
.container {
scroll-snap-type: y mandatory;
}
.container {
scroll-snap-type: both mandatory;
}
.container {
scroll-snap-type: block proximity;
}
.container {
scroll-snap-type: inline proximity;
}
Axis Without Explicit Strictness
.container {
scroll-snap-type: x;
}
When strictness is omitted, proximity is used.
CSS-Wide Keywords
.container {
scroll-snap-type: initial;
}
.container {
scroll-snap-type: inherit;
}
.container {
scroll-snap-type: unset;
}
.container {
scroll-snap-type: revert;
}
.container {
scroll-snap-type: revert-layer;
}
The property does not accept lengths, percentages, or multiple snapping axes in one declaration.
Property Values
| Value | Description |
|---|---|
none |
Disables scroll snapping in the container. |
x |
Enables snapping along the physical horizontal axis. |
y |
Enables snapping along the physical vertical axis. |
block |
Enables snapping along the logical block axis. |
inline |
Enables snapping along the logical inline axis. |
both |
Enables snapping along both physical axes. |
mandatory |
Requires snapping to an appropriate position when applicable snap positions exist. |
proximity |
Allows snapping when the browser determines that a snap position is sufficiently close. |
initial |
Resets the property to none. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Scroll containers |
| Computed value | Specified axis and strictness keywords |
| Animation type | Discrete |
| Percentages | Not applicable |
| Default strictness | proximity |
| Related target property | scroll-snap-align |
Valid Values
.container {
scroll-snap-type: none;
}
.container {
scroll-snap-type: x mandatory;
}
.container {
scroll-snap-type: y proximity;
}
.container {
scroll-snap-type: both mandatory;
}
.container {
scroll-snap-type: inline;
}
.container {
scroll-snap-type: block proximity;
}
Invalid Values
.container {
scroll-snap-type: mandatory;
}
.container {
scroll-snap-type: x y;
}
.container {
scroll-snap-type: horizontal mandatory;
}
.container {
scroll-snap-type: x always;
}
.container {
scroll-snap-type: 100px;
}
A snapping axis is required unless the value is none. The always keyword belongs to scroll-snap-stop, not scroll-snap-type.
How It Works
CSS Scroll Snap allows a scroll container to settle at defined positions after a scrolling operation. The container establishes the snapping behavior, and its descendants define potential snap positions.
Step 1: Create a Scroll Container
.carousel {
display: flex;
overflow-x: auto;
}
The container can scroll horizontally when its content exceeds its available width.
Step 2: Enable Scroll Snapping
.carousel {
scroll-snap-type: x mandatory;
}
The browser now uses mandatory snapping along the horizontal axis.
Step 3: Define Snap Targets
.carousel .slide {
flex: 0 0 250px;
scroll-snap-align: start;
}
Each slide establishes a start-aligned snap position.
Step 4: Scroll the Container
When scrolling occurs, the browser considers eligible snap positions and the configured strictness to determine where scrolling should settle.
The Snapport
The snapport is the alignment region within the scroll container. By default, it corresponds to the scrollport, but scroll-padding can adjust its boundaries.
The Scroll Snap Area
Each target has a scroll snap area based on its transformed border box. The scroll-margin property can expand or contract this area.
Browser-Controlled Snap Selection
The browser selects snap positions based on the scrolling operation, direction, available targets, and snapping rules. The specification does not require identical target-selection behavior for every input method.
No Layout Changes
The property does not move elements, add spacing, or change their dimensions. It affects how the container settles after scrolling.
Mandatory Snapping
The mandatory keyword requires the browser to settle at an appropriate snap position when snapping is applicable.
Horizontal Mandatory Snapping
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.carousel .slide {
flex: 0 0 250px;
scroll-snap-align: start;
}
After horizontal scrolling, the container settles at a valid snap position.
Vertical Mandatory Snapping
.sections {
height: 400px;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.sections .section {
min-height: 300px;
scroll-snap-align: start;
}
Sections can snap to the top of the container after vertical scrolling.
Mandatory Does Not Mean Every Item
Mandatory snapping does not require the browser to stop at every intermediate snap target. A large scrolling gesture may pass over multiple items before settling.
Using Protected Snap Stops
.carousel .slide {
scroll-snap-align: start;
scroll-snap-stop: always;
}
The always value prevents applicable scrolling operations from passing over protected snap positions.
Oversized Content
Mandatory snapping can make content difficult to navigate when snap targets are larger than the snapport. Browsers must allow scrolling within oversized snap areas, but the layout should still be tested carefully.
When to Use mandatory
Use mandatory snapping when settling at a defined position is central to the interface, such as a slide carousel or a sequence of similarly sized panels.
Proximity Snapping
The proximity keyword allows the browser to snap when a suitable position is sufficiently close. It does not require snapping after every scrolling operation.
Horizontal Proximity Snapping
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x proximity;
}
.carousel .slide {
flex: 0 0 250px;
scroll-snap-align: start;
}
Slides may snap when the scrolling position is near a valid snap point.
Vertical Proximity Snapping
.sections {
height: 400px;
overflow-y: auto;
scroll-snap-type: y proximity;
}
.sections .section {
scroll-snap-align: start;
}
Users can stop between sections when the browser does not consider a snap position sufficiently close.
Default Strictness
.carousel {
scroll-snap-type: x;
}
This is equivalent to:
.carousel {
scroll-snap-type: x proximity;
}
Browser-Defined Threshold
The specification leaves the exact proximity threshold to the browser. Different browsers or input methods may therefore produce slightly different results.
When to Use proximity
Use proximity snapping when scrolling should remain flexible, such as in article sections, card collections, or galleries where users may want to stop between items.
Physical Scroll Axes
The x and y keywords select physical scrolling directions that remain unchanged by writing mode or text direction.
Horizontal Axis
.carousel {
overflow-x: auto;
scroll-snap-type: x mandatory;
}
The x keyword enables snapping along the physical horizontal axis.
Vertical Axis
.sections {
overflow-y: auto;
scroll-snap-type: y mandatory;
}
The y keyword enables snapping along the physical vertical axis.
Physical Axes and Writing Modes
| Value | Physical Direction | Changes With Writing Mode |
|---|---|---|
x |
Horizontal | No |
y |
Vertical | No |
Important Consideration
Physical axes are useful when the interface must scroll horizontally or vertically regardless of the document's writing mode.
Logical Scroll Axes
The block and inline keywords follow the element's writing mode rather than a fixed physical direction.
Inline-Axis Snapping
.container {
scroll-snap-type: inline mandatory;
}
In horizontal writing, the inline axis runs horizontally. In vertical writing, it runs vertically.
Block-Axis Snapping
.container {
scroll-snap-type: block mandatory;
}
In horizontal writing, the block axis runs vertically. In vertical writing, it runs horizontally.
Writing Mode Comparison
| Writing Mode | Block Axis | Inline Axis |
|---|---|---|
horizontal-tb |
Vertical | Horizontal |
vertical-rl |
Horizontal | Vertical |
vertical-lr |
Horizontal | Vertical |
Logical Snapping Example
.scroll-container {
writing-mode: horizontal-tb;
overflow: auto;
scroll-snap-type: inline mandatory;
}
.scroll-container .item {
scroll-snap-align: start;
}
The container snaps along the horizontal inline axis.
Changing the Writing Mode
.scroll-container {
writing-mode: vertical-rl;
overflow: auto;
scroll-snap-type: inline mandatory;
}
The inline axis is now vertical, so the snapping direction changes accordingly.
Important Consideration
Use logical axes when scroll snapping should adapt to different writing modes. Use physical axes when the scrolling direction must remain fixed.
Snapping on Both Axes
The both keyword enables snapping along the horizontal and vertical axes.
Basic Example
.grid-container {
width: 400px;
height: 300px;
overflow: auto;
scroll-snap-type: both mandatory;
}
The container can snap along either axis when appropriate snap positions exist.
Two-Dimensional Grid
.grid-container {
display: grid;
grid-template-columns: repeat(4, 200px);
grid-auto-rows: 180px;
width: 400px;
height: 300px;
overflow: auto;
scroll-snap-type: both proximity;
}
.grid-container .cell {
scroll-snap-align: start;
}
Grid cells establish snap positions along both axes.
Different Alignment on Each Axis
.grid-container .cell {
scroll-snap-align: start center;
}
In horizontal writing, the cell uses start alignment vertically and center alignment horizontally.
Mandatory Snapping on Both Axes
.grid-container {
scroll-snap-type: both mandatory;
}
The browser must select appropriate snap positions for applicable scrolling operations on the enabled axes.
Important Consideration
Two-dimensional mandatory snapping can feel restrictive. Test diagonal scrolling and ensure that all content remains reachable.
Using scroll-snap-align
The scroll-snap-align property defines the positions where individual elements can snap.
Start Alignment
.carousel {
scroll-snap-type: x mandatory;
}
.carousel .slide {
scroll-snap-align: start;
}
In left-to-right horizontal writing, slides can snap toward the left edge.
Center Alignment
.carousel .slide {
scroll-snap-align: center;
}
Slides can snap to the center of the snapport.
End Alignment
.carousel .slide {
scroll-snap-align: end;
}
In left-to-right horizontal writing, slides can snap toward the right edge.
Two-Value Alignment
.grid-container .cell {
scroll-snap-align: start center;
}
The first value controls the block axis, and the second controls the inline axis.
Disabling a Target's Snap Position
.carousel .decorative-item {
scroll-snap-align: none;
}
The decorative item does not establish a snap position.
Important Consideration
Setting scroll-snap-type alone does not create snap targets. Elements need appropriate scroll-snap-align values.
Using scroll-snap-stop
The scroll-snap-stop property determines whether applicable snap positions may be passed over during a scrolling operation.
Normal Snap Stops
.carousel .slide {
scroll-snap-align: start;
scroll-snap-stop: normal;
}
A large scrolling gesture may pass over multiple snap positions before settling.
Protected Snap Stops
.carousel .slide {
scroll-snap-align: start;
scroll-snap-stop: always;
}
Applicable snap positions cannot be passed over by a scrolling operation that would otherwise move beyond them.
Complete Example
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.carousel .slide {
flex: 0 0 250px;
scroll-snap-align: start;
scroll-snap-stop: always;
}
Important Consideration
The always value does not guarantee exactly one item per scroll operation. Direct navigation to a particular target may also bypass intermediate stops.
Scroll Padding and Scroll Margin
Scroll padding and scroll margin adjust the regions used when snap positions are calculated.
| Property | Applied To | Purpose |
|---|---|---|
scroll-snap-type |
Scroll container | Controls the snapping axis and strictness. |
scroll-snap-align |
Snap target | Defines the target's snap position. |
scroll-padding |
Scroll container | Adjusts the snapport used for alignment. |
scroll-margin |
Snap target | Adjusts the target's scroll snap area. |
Scroll Padding Example
.carousel {
scroll-snap-type: x mandatory;
scroll-padding-inline: 30px;
}
.carousel .slide {
scroll-snap-align: start;
}
Slides can snap inward from the logical inline-start edge.
Scroll Margin Example
.carousel .featured {
scroll-margin-inline-start: 20px;
scroll-snap-align: start;
}
The featured slide receives additional alignment spacing at its inline-start edge.
Combining Both Properties
.carousel {
scroll-snap-type: x mandatory;
scroll-padding-inline-start: 40px;
}
.carousel .featured {
scroll-margin-inline-start: 20px;
scroll-snap-align: start;
}
For inline-start alignment, the combined settings can position the featured slide's border edge approximately 60 pixels inward from the scrollport edge.
Important Consideration
Scroll padding and scroll margin modify alignment. They do not enable snapping independently.
Practical Examples
Horizontal Image Gallery
.image-gallery {
display: flex;
gap: 16px;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.image-gallery img {
flex: 0 0 280px;
width: 280px;
height: 200px;
object-fit: cover;
scroll-snap-align: center;
}
Images snap to centered positions during horizontal scrolling.
Horizontal Card Collection
.card-list {
display: flex;
gap: 16px;
overflow-x: auto;
scroll-snap-type: x proximity;
scroll-padding-inline: 20px;
}
.card-list .card {
flex: 0 0 220px;
scroll-snap-align: start;
}
Cards snap near the inline-start edge when the browser considers a position sufficiently close.
Full-Screen Sections
.page-sections {
height: 100vh;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.page-sections section {
min-height: 100vh;
scroll-snap-align: start;
}
Sections snap vertically. Ensure that content taller than the viewport remains accessible.
Product Carousel
.product-carousel {
display: flex;
gap: 20px;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.product-carousel .product {
flex: 0 0 240px;
scroll-snap-align: center;
}
Product cards settle at centered snap positions.
Two-Dimensional Scrolling Grid
.grid-scroll {
width: 400px;
height: 300px;
overflow: auto;
scroll-snap-type: both proximity;
}
.grid-scroll .grid-item {
scroll-snap-align: start center;
}
Grid items define different snap alignments on the block and inline axes.
Responsive Snapping
.carousel {
scroll-snap-type: x mandatory;
}
@media (min-width: 992px) {
.carousel {
scroll-snap-type: x proximity;
}
}
The carousel uses mandatory snapping on smaller screens and proximity snapping on wider screens.
scroll-snap-type Example
This interactive example compares mandatory and proximity snapping. Both panels contain the same horizontally scrolling items. Scroll each panel manually or use the buttons to request a partial scroll.
HTML Example
<div class="snap-type-demo">
<div class="snap-example mandatory">
<h3>Mandatory Snapping</h3>
<div class="snap-panel">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<button type="button" class="advance">Scroll Forward</button>
<button type="button" class="reset">Reset</button>
</div>
<div class="snap-example proximity">
<h3>Proximity Snapping</h3>
<div class="snap-panel">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<button type="button" class="advance">Scroll Forward</button>
<button type="button" class="reset">Reset</button>
</div>
</div>
CSS Example
.snap-type-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 20px;
}
.snap-type-demo .snap-panel {
display: flex;
gap: 12px;
overflow-x: auto;
padding: 10px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.snap-type-demo .item {
flex: 0 0 100px;
padding: 25px 10px;
text-align: center;
background-color: #e2e8f0;
scroll-snap-align: start;
}
.snap-type-demo .mandatory .snap-panel {
scroll-snap-type: x mandatory;
}
.snap-type-demo .proximity .snap-panel {
scroll-snap-type: x proximity;
}
.snap-type-demo button {
margin-top: 10px;
padding: 8px 12px;
cursor: pointer;
}
JavaScript Example
document.querySelectorAll(".snap-type-demo .snap-example").forEach((example) => {
const panel = example.querySelector(".snap-panel");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
example.querySelector(".advance").addEventListener("click", () => {
panel.scrollBy({
left: 65,
behavior: reduceMotion.matches ? "instant" : "smooth"
});
});
example.querySelector(".reset").addEventListener("click", () => {
panel.scrollTo({
left: 0,
behavior: "instant"
});
});
});
Mandatory Snapping
Proximity Snapping
Click Scroll Forward or manually scroll each panel. Mandatory snapping requires a suitable final snap position, while proximity snapping allows the browser to stop between snap positions when they are not sufficiently close.
Changing to Mandatory Snapping
.snap-type-demo .proximity .snap-panel {
scroll-snap-type: x mandatory;
}
Both panels now use mandatory snapping.
Disabling Snapping
.snap-type-demo .snap-panel {
scroll-snap-type: none;
}
The panels scroll without CSS scroll snap constraints.
Browser Compatibility Note
The visible difference depends on browser snapping thresholds, the scrolling method, and the available scroll range. Try both the buttons and manual scrolling to observe the behavior.
Accessibility Considerations
Scroll snapping can improve navigation, but overly restrictive snapping may interfere with reading and interaction.
Avoid Inaccessible Content
Mandatory snapping should not prevent users from reaching content between snap positions, particularly when sections exceed the snapport dimensions.
Provide Keyboard Access
Ensure scrolling controls and interactive elements remain accessible by keyboard and have visible focus indicators.
Respect Reduced Motion Preferences
Scroll snapping does not require smooth animation. When smooth scrolling is added, provide an alternative for users who prefer reduced motion.
Avoid Unexpected Scrolling
Do not repeatedly force users back to snap positions through custom scripts.
Provide Navigation Controls
Carousels may benefit from labeled previous and next controls, especially when touch gestures are not available.
Test Zoom and Text Scaling
Ensure all content remains accessible when browser zoom or text size increases.
Consider Proximity Snapping
Use proximity when users need greater freedom to stop between snap positions.
Common Mistakes
Applying scroll-snap-type Only to the Children
.carousel .slide {
scroll-snap-type: x mandatory;
}
Apply scroll-snap-type to the scroll container rather than its individual snap targets.
Forgetting scroll-snap-align
.carousel {
scroll-snap-type: x mandatory;
}
Without appropriate snap targets, the container has no defined positions to snap to.
Using mandatory Without an Axis
.carousel {
scroll-snap-type: mandatory;
}
Specify an axis such as x, y, block, inline, or both.
Using x and y Together
.container {
scroll-snap-type: x y mandatory;
}
Use both mandatory when snapping is needed along both axes.
Confusing mandatory With always
mandatory belongs to scroll-snap-type, while always belongs to scroll-snap-stop.
Expecting Mandatory Snapping to Stop at Every Item
Mandatory snapping requires a valid final snap position but does not prevent skipping intermediate targets.
Assuming Proximity Uses a Fixed Distance
The browser determines how close a snap position must be before proximity snapping occurs.
Using the Wrong Axis
Use x for horizontal snapping and y for vertical snapping. Logical axes follow the writing mode.
Ignoring Available Scroll Range
Targets near the beginning or end of a container may not reach every requested alignment position.
Creating Overly Restrictive Layouts
Mandatory snapping can make oversized sections difficult to navigate. Test with realistic content and different viewport sizes.
Important Considerations
Keep the following points in mind when using scroll-snap-type:
- It controls the snapping axis and strictness of a scroll container.
- Its initial value is
none, and it is not inherited. - It accepts physical axes
xandy. - It accepts logical axes
blockandinline. - The
bothkeyword enables snapping along both axes. - The
mandatorykeyword requires snapping when applicable. - The
proximitykeyword allows optional snapping near suitable positions. - Omitting strictness defaults to
proximity. - The
nonevalue disables snapping. - Snap targets require appropriate
scroll-snap-alignvalues. scroll-snap-stopcan protect eligible positions from being passed over.scroll-paddingadjusts the snapport.scroll-marginadjusts individual snap areas.- The property does not create overflow or change normal layout spacing.
- Physical axes remain fixed regardless of writing mode.
- Logical axes follow the writing mode.
- Browser algorithms determine which snap position is selected.
- Mandatory snapping should not prevent access to oversized content.
- Test mouse, touch, keyboard, and programmatic scrolling.
Browser Support
The CSS scroll-snap-type property is widely supported in current versions of major browsers, including Chrome, Edge, Firefox, and Safari.
Older browsers may support earlier scroll snapping implementations or have differences in snap selection behavior.
For current compatibility information, visit Can I Use? and search for scroll-snap-type.
When unsupported, content generally remains scrollable, but the requested snap behavior may not be applied.
Specifications
The scroll-snap-type property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines scroll snapping axes, strictness, and the rules used to select snap positions.
For additional documentation and examples, consult MDN Web Docs.
