Pure CSS Carousel
This is based on the video from the Code2Go Youtube channel.
It uses:
- The
::scroll-button()pseudo element for left and right buttons. These pseudo elements are attached to the element you wish to scroll:.carousel::scroll-button(left) { .. }. There is acontentproperty like the pseudo elements::beforeand:;after. - scroll snapping to keep
- Anchor positioning is used to fix the buttons to the right spot on the carousel. There are several properties:
anchor-namefor anchor positioning. This is the element you wish to anchor something to. It’s like a custom property name:anchor-name: --carouselposition-anchoradded to the buttons defines the element you’re attaching to:position-anchor: --carouselposition-areais where you wish to position the buttons to. There are 9 options. These can be fine tuned usingtranslate.
- the
align-contentproperty used for vertical centering on adisplay: blockelement. - Scroll snapping is used to make the scrolling snap to the start of a card.
scroll-snap-type: x mandatoryis added to the container. The scrolling is along the x axis and the the snapping is enforced.scroll-snap-align: startis added to the cards so the scrolling snaps to the start of a card.
card 1
card 2
card 3
card 4
card 5
card 6
card 7
card 8
card 9
card 10
card 11
card 12
See also the post on scrolling which covers some this there.