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)));
}
}
<BlossomCarousel class="carousel">
<div class="slide">
<div class="card" style="width: 60%"></div>
</div>
<div class="slide">
<div class="card" style="width: 90%"></div>
</div>
<div class="slide">
<div class="card" style="width: 45%"></div>
</div>
...
</BlossomCarousel>
.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));
}
.slide {
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 {
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)));
}
}
<BlossomCarousel
class="carousel grid! snap-x snap-mandatory auto-cols-[100%] grid-flow-col px-(--inset)!"
>
<div class="slide flex! snap-center justify-center">
<div class="card h-64" style="width: 60%"></div>
</div>
<div class="slide flex! snap-center justify-center">
<div class="card h-64" style="width: 90%"></div>
</div>
<div class="slide flex! snap-center justify-center">
<div class="card h-64" style="width: 45%"></div>
</div>
...
</BlossomCarousel>