CSS-Only Carousel
It’s kind of amazing how far HTML and CSS will take you when building a carousel/slideshow.
- Setting some boxes in a horizontal row with flexbox is easy.
- Showing only one box at a time with
overflow
and making it swipable with-webkit-overflow-scrolling
is easy. - You can make the “slides” line up nicely with
scroll-snap-type
. - A couple of
#jump-links
is all you need to make navigation for it, which you can make all nice and smooth withscroll-behavior
.
See the Pen
Real Simple Slider by Chris Coyier (@chriscoyier)
on CodePen.
Christian Schaefer has taken it a little further with next and previous buttons, plus an auto-play feature that stops playing once interaction starts.
See the Pen
A CSS-only Carousel Slider by Christian Schaefer (@Schepp)
on CodePen.
About that auto-play thing — it’s a bonafide CSS trick:
- First I slowly offset the scroll snap points to the right, making the scroll area follow along due to being snapped to them.
- After having scrolled the width of a whole slide, I deactivate the snapping. The scroll area is now untied from the scroll snap points.
- Now I let the scroll snap points jump back to their initial positions without them “snap-dragging” the scroll area back with them
- Then I re-engage the snapping which now lets the scroll area snap to a different snap point ?
Cool.
JavaScript-powered slideshows (e.g. with Flickty) can be real nice, too. There is just something neat about getting it done with so little code.
See the Pen
Flickity – wrapAround by Dave DeSandro (@desandro)
on CodePen.
The post CSS-Only Carousel appeared first on CSS-Tricks.