
CSS line-clamp: Limit Text to a Set Number of Lines
04/05/2024
CSS scroll-behavior: Smooth Scrolling Between Anchors
05/05/2024
CSS Scroll Snap makes a scrolling region settle at defined positions. Use it when aligning sections, cards, or other repeated content makes navigation easier.
This behavior can be implemented with CSS using scroll-snap-type on the scroll container and an alignment rule on its children.
Snap positions can be arranged horizontally or vertically. They make it easier to land at the start, center, or end of each item.
1. What CSS Scroll Snap does
In ordinary scrolling, content moves continuously in the direction you scroll and can stop at an arbitrary position.
With Scroll Snap, the browser can adjust the final scroll position to align with a snap point, for example the start of a section. The snapping behavior is separate from scroll animation.
2. Implementing Scroll Snap in CSS
Set scroll-snap-type on the scrolling container, then set the alignment on each item that should contribute a snap position.
Syntax:
.snap-container {
scroll-snap-type: [x | y | block | inline] [mandatory | proximity];
}
.snap-section {
scroll-snap-align: [start | center | end | none];
}
scroll-snap-typetakes an axis such asx,y,block,inline, orboth. Usemandatoryto require snapping when scrolling ends orproximityto snap when the browser considers the position close enough.- Use
scroll-snap-alignon the child items. Values includestart,end,center, andnone. For example,startaligns the item’s start edge with the corresponding snapport edge.
Example:
The snap-container has several snap-section items. Use scroll-snap-type: y mandatory for vertical snapping and scroll-snap-align: start to align sections at their start edge.
HTML:
<div class="snap-container">
<div class="snap-section">Section 1</div>
<div class="snap-section">Section 2</div>
<div class="snap-section">Section 3</div>
<div class="snap-section">Section 4</div>
</div>
CSS:
.snap-container {
height: 300px; /* Container height */
overflow-y: scroll; /* Enable vertical scrolling */
scroll-snap-type: y mandatory; /* Require vertical snapping */
}
.snap-section {
height: 300px; /* Height of each section */
scroll-snap-align: start; /* Align each section at the start */
}
Try it in CodePen:
3. Practical Scroll Snap examples
The following examples use snapping for a one-page layout, a product gallery, and a vertical image gallery.
3.1. Full-height sections
Make each section fill the viewport and snap to its start. This can suit a short one-page presentation. Check that long sections can still be read from start to finish.
CSS:
body, html {
height: 100%;
}
.scroll-container {
width: 100%;
height: 100%;
overflow-y: auto;
scroll-snap-type: y mandatory; /* Require vertical snapping */
}
.scroll-item {
height: 100vh; /* Fill the viewport height */
scroll-snap-align: start; /* Align items at the start of the scroll area */
display: flex;
justify-content: center;
align-items: center;
}
Try it in CodePen:
3.2. Horizontal product galleries
Put each product in its own card and make the row scroll horizontally. Align each card to the start of the scroll area so readers can compare the items more easily.
CSS:
.product-gallery {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}
.product-card {
flex: 0 0 auto;
width: 200px;
margin-right: 10px;
scroll-snap-align: start;
}
Try it in CodePen:
3.3. A vertical gallery with captions
Arrange images and descriptive text in a vertical scrolling gallery. Align each item with the start of the container. This can work for a product presentation or portfolio.
CSS:
.gallery {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
.image {
scroll-snap-align: start; /* Align each image at the start of the scroll area */
}
Try it in CodePen:
4. Browser support
Check browser support for CSS Scroll Snap against your project targets. Test scrolling with touch and a keyboard. The layout should still work with normal scrolling when snapping is unavailable.
Choose an axis and alignment that fit the content. Use a stronger snap requirement only when it helps navigation and leaves all content reachable.