Blossom Carousel Logo
GitHub
Advanced

Gallery

Slides of different widths, with a sliver of the previous and next slide on either side.
.carousel {
  /* How much of the previous and next card stays visible. */
  --peek: 40px;
  /* The smallest space between two cards. */
  --gap: 12px;
  --inset: calc(var(--peek) + var(--gap));

  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100%;
  padding-inline: var(--inset);
  scroll-snap-type: x mandatory;
}

.slide {
  display: flex;
  justify-content: center;
  scroll-snap-align: center;

  container-type: inline-size;
  /* The inset makes the timeline run from one slide before to one slide after. */
  view-timeline: --slide inline var(--inset);
}

.card {
  height: 16rem;

  animation: gallery linear both;
  animation-timeline: --slide;
  animation-range: cover;
}

/* 50cqi - 50% is the empty space beside the card, so narrow cards move further. */
@keyframes gallery {
  from {
    transform: translateX(calc(50% - 50cqi + var(--gap)));
  }
  to {
    transform: translateX(calc(50cqi - 50% - var(--gap)));
  }
}