cssscrollscroll-snap-points

No scroll snap with scroll-snap-type and -align / Simple Example not working Chrome/Firefox /


Using Chrome / Firefox, I created a container (.container) for a couple of picture (list items with class img). I added scroll-snap-type on the container. I added scroll-snap-align on the items with class img.

What am I missing here?

HTML

<div class="container">
  <ul>
  <li class="img"><img src="https://picsum.photos/500"></li>
  <li class="img"><img src="https://picsum.photos/500"></li>
  <li class="img"><img src="https://picsum.photos/500"></li>
  <li class="img"><img src="https://picsum.photos/500"></li>
  <li class="img"><img src="https://picsum.photos/500"></li>
  <li class="img"><img src="https://picsum.photos/500"></li>
  </ul>
</div>

CSS

.img {
  margin: 10px 5px;
  scroll-snap-align: center;
  -webkit-overflow-scrolling: touch;
}

.container {
  display:  grid; 
  justify-content: center; 
  grid-gap: 10px;
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
}

Solution

  • As it turns out, scroll-snap-type requires that you define a height for the container. I am not exactly sure how to explain why it is required, however, defining a height does take care of the issue.

    .img {
      margin: 10px 5px;
      scroll-snap-align: center;
      -webkit-overflow-scrolling: touch;
    }
    
    .container {
      display:  grid; 
      justify-content: center; 
      grid-gap: 10px;
      scroll-snap-type: y mandatory;
      overflow-y: scroll;
      height: 100vh;
    }