The scroll-snap-align
property is a crucial part of the CSS Scroll Snap module, designed to create well-controlled scrolling experiences. This property specifies the alignment of the snap area of an element within the container's snapport (the visible area) when the scrolling action ceases.
Example 1: scroll-snap-align: start
/* CSS for the parent container */
.snap-container {
width: 100%;
height: 300px;
overflow-x: scroll; /* Enable horizontal scrolling */
display: flex; /* Lay out children in a row */
scroll-snap-type: x mandatory; /* Enforce snapping on the horizontal axis */
}
/* CSS for the child items */
.snap-item {
flex: 0 0 100%; /* Each item takes the full width of the container */
height: 300px;
scroll-snap-align: start; /* Align the start of the item with the start of the container */
}
Explanation
This code creates a horizontal scrolling container where each child item will snap into place. The scroll-snap-align: start;
rule ensures that the left edge of the scrolling item will align precisely with the left edge of the visible scroll container when the user stops scrolling.
Example 2: scroll-snap-align: center
/* CSS for the parent container */
.gallery-container {
width: 100%;
height: 400px;
overflow-x: scroll;
display: flex;
scroll-snap-type: x mandatory;
}
/* CSS for the gallery images */
.gallery-image {
flex: 0 0 80%; /* Each image takes up 80% of the container width */
margin: 0 10%; /* Center the image with some spacing */
height: 400px;
scroll-snap-align: center; /* Align the center of the image with the center of the container */
}
Explanation
In this example, as the user scrolls through a gallery of images, each image will snap to the center of the container. The scroll-snap-align: center;
is ideal for focusing on the main content of each section.
Example 3: scroll-snap-align: end
/* CSS for the vertical scrolling container */
.vertical-snapper {
height: 500px;
overflow-y: scroll; /* Enable vertical scrolling */
scroll-snap-type: y mandatory; /* Enforce snapping on the vertical axis */
}
/* CSS for the sections to snap */
.snap-section {
height: 500px;
scroll-snap-align: end; /* Align the end of the section with the end of the container */
}
Explanation
This code demonstrates a full-page vertical scrolling effect. scroll-snap-align: end;
aligns the bottom edge of each section with the bottom edge of the viewport as the user scrolls down.
Example 4: scroll-snap-align: none
/* CSS for the mixed behavior container */
.mixed-snap-container {
height: 400px;
overflow-y: scroll;
scroll-snap-type: y proximity; /* Looser snapping on the vertical axis */
}
/* CSS for the snap and non-snap items */
.snap-item-regular {
height: 400px;
scroll-snap-align: center;
}
.no-snap-item {
height: 200px;
scroll-snap-align: none; /* This item will not have a snap point */
}
Explanation
The scroll-snap-align: none;
value is used to explicitly prevent an element from being a snap target. In this container, "snap-item-regular" elements will snap to the center, while the "no-snap-item" will not, allowing for more complex scroll interactions.
Example 5: Two-axis alignment (start end)
/* CSS for a 2D grid container */
.grid-container {
width: 100%;
height: 600px;
overflow: scroll; /* Enable both horizontal and vertical scrolling */
display: grid;
grid-template-columns: repeat(3, 100%);
scroll-snap-type: both mandatory;
}
/* CSS for grid items */
.grid-item {
scroll-snap-align: start end; /* Align to start on x-axis, end on y-axis */
}
Explanation
This example shows a two-dimensional grid where items snap. scroll-snap-align: start end;
sets the alignment for the block (vertical) and inline (horizontal) axes respectively, meaning the item will snap to the top of the container vertically and the right edge horizontally.
Example 6: Combining with scroll-padding
/* CSS for the container with padding */
.padded-container {
width: 100%;
height: 350px;
overflow-x: scroll;
display: flex;
scroll-snap-type: x mandatory;
scroll-padding: 0 50px; /* Add 50px padding to the left and right of the snapport */
}
/* CSS for the snapped items */
.padded-item {
flex: 0 0 90%;
height: 350px;
scroll-snap-align: center; /* Center the item within the padded area */
}
Explanation
Here, scroll-padding
is used to create an offset within the scroll container. scroll-snap-align: center;
will then align the snap item to the center of the snapport, respecting the defined padding for a visually balanced layout.
Example 7: Aligning Different Sized Items
/* CSS for the varied content container */
.varied-container {
width: 100%;
height: 450px;
overflow-x: scroll;
display: flex;
scroll-snap-type: x mandatory;
}
/* CSS for different sized items */
.item-small {
flex: 0 0 60%;
height: 450px;
scroll-snap-align: center;
}
.item-large {
flex: 0 0 95%;
height: 450px;
scroll-snap-align: center; /* Both item types will center align */
}
Explanation
This demonstrates that scroll-snap-align
works effectively even with items of different sizes within the same scroll container. Regardless of their width, each item with scroll-snap-align: center;
will snap to the horizontal center of the viewport.