In this example, we demonstrate how to create scroll markers on a CSS carousel.
HTML
We have a basic HTML <ul> list with several <li> list items.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
</ul>
CSS
We convert our <ul> into a scroll snapping overflow container by setting the display to flex, creating a single, non-wrapping row of <li> elements. The overflow-x property is set to auto, meaning if the items overflow their container on the x-axis, the content will scroll horizontally. We then convert the <ul> into a scroll-snap container, ensuring that items always snap into place when the container is scrolled with a scroll-snap-type value of mandatory.
We create a scroll marker group with the scroll-marker-group property, placing the group after all the content.
ul {
display: flex;
gap: 4vw;
padding-left: 0;
overflow-x: auto;
overscroll-behavior-x: contain;
scroll-snap-type: x mandatory;
scroll-marker-group: after;
}
Next, we style the <li> elements, using the flex property to make them 33% of the width of the container. The scroll-snap-align value of start causes the left-hand side of the left-most visible item to snap to the left edge of the container when the content is scrolled.
li {
list-style-type: none;
background-color: #eee;
flex: 0 0 33%;
height: 100px;
padding-top: 20px;
scroll-snap-align: start;
text-align: center;
}
We then use the ::scroll-marker pseudo-element to create a square marker for each list item with a red border:
li::scroll-marker {
content: "";
border: 1px solid red;
height: 1em;
width: 1em;
}
We also apply styles to the ::scroll-marker-group pseudo-element to lay out the scroll markers in the center of the row with a 0.4em gap between each one:
::scroll-marker-group {
display: flex;
gap: 0.4em;
place-content: center;
}
Finally, we style the marker of the currently-scrolled element differently from the others, targeting the marker with the :target-current pseudo-class.
::scroll-marker:target-current {
background: red;
}
Result