How to style scrollbars and custom scroll snapping
· Category: HTML & CSS
Short answer
Use ::-webkit-scrollbar for custom scrollbar styling and scroll-snap-type with scroll-snap-align to create scrollable sections that lock into place.
Steps
- Style the scrollbar track and thumb with pseudo-elements.
- Set
scroll-snap-type: x mandatoryon the container. - Set
scroll-snap-align: starton child items.
Example
.container {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scrollbar-width: thin;
scrollbar-color: #888 #f1f1f1;
}
.item {
flex: 0 0 100%;
scroll-snap-align: start;
}
Tips
- Use
scroll-paddingto account for fixed headers. - Prefer
scroll-behavior: smoothfor animated scrolling. - Test on touch devices; snap behavior can vary.