Scroll horizontally to see how each slide snaps neatly into place.
This is powered by scroll-snap-type and scroll-snap-align.
<div class="carousel">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
</div>
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
gap: 1rem;
}
.slide {
flex: 0 0 100%;
scroll-snap-align: start;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
font-weight: bold;
color: white;
height: 300px;
border-radius: 8px;
}
.slide:nth-child(1) { background: #e63946; }
.slide:nth-child(2) { background: #457b9d; }
.slide:nth-child(3) { background: #2a9d8f; }
.carousel { display: flex; }overflow-x: auto;scroll-snap-type: x mandatory;mandatory means the scroll always snaps to a slide.scroll-behavior: smooth;gap: 1rem;.slide { flex: 0 0 100%; }scroll-snap-align: start;display: flex; align-items: center; justify-content: center;font-size: 2rem; font-weight: bold; color: white;height: 300px; border-radius: 8px;.slide:nth-child(1/2/3).slide { flex: 0 0 100%; }flex-grow: 0 → The slide will not grow larger than its set width.flex-shrink: 0 → The slide will not shrink smaller than its set width.flex-basis: 100% → Each slide takes up 100% of the container’s width.display: flex; align-items: center; justify-content: center;display: flex → Activates Flexbox layout inside the slide.align-items: center → Vertically centers the content within the slide.justify-content: center → Horizontally centers the content within the slide.