๐Ÿ“– Mobile First Web Design /
Module: Form and Inputs

Handling touches

๐Ÿ“– Mobile First Web Design / Form and Inputs / Handling touches

Native CSS Carousel with Scroll Snap

CSS now comes with native support of carousel-like feature called Scroll Snap.
Given the following HTML code.

<!-- Using different image width to demonstrate the scroll-snal-align center -->
<div class="photo-gallery">
    <img src="https://source.unsplash.com/random/500x320">
    <img src="https://source.unsplash.com/random/400x320">
    <img src="https://source.unsplash.com/random/300x320">
    <img src="https://source.unsplash.com/random/450x320">
    <img src="https://source.unsplash.com/random/400x320">
</div>

We can use the scroll snap CSS as following:

.photo-gallery {
  width: 500px;
  max-width: 100vw;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.photo-gallery img {
  scroll-snap-align: center;
  max-width: 100vw;
  height: 100%;
  object-fit: cover;
}
You may try it in your mobile device by directly scanning the QR code below.

https://cdpn.io/makzan/debug/EzrYXv


https://cdpn.io/makzan/debug/EzrYXv