Carousel with full css - with buttons

by Julien Roche

HTML

<div class="container">
  <div class="carousel carousel--buttons">
    <div class="carousel__item">1</div>
    <div class="carousel__item">2</div>
    <div class="carousel__item">3</div>
    <div class="carousel__item">4</div>
    <div class="carousel__item">5</div>
  </div>
</div>

CSS

body {
  background: #0f0f0f;
  padding: 10rem;
  color: white;
  min-height: 100vh;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 100px;
}

.section {
  width: 100%;
}

.carousel {
  display: flex;
  overflow-x: auto;
  position: relative;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  scrollbar-width: none;
  border-radius: 20px;
  margin-bottom: 20px;

  .carousel__item {
    flex: 0 0 100%;
    scroll-snap-align: start;
    height: 15rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10rem;
    color: white;

    &:nth-child(1) {
      background: #22c55e;
    }

    &:nth-child(2) {
      background: #ef4444;
    }

    &:nth-child(3) {
      background: #f59e0b;
    }

    &:nth-child(4) {
      background: #8b5cf6;
    }

    &:nth-child(5) {
      background: #06b6d4;
    }
  }
}

.carousel--buttons {
  &::-webkit-scrollbar {
    display: none;
  }

  &::scroll-button(left) {
    content: '←';
    width: 60px;
    height: 60px;
    background: #1a1a1a;
    color: #fff;
    border-radius: 14px;
    font-size: 28px;
    cursor: pointer;
    border: 1px solid #333;
    font-weight: 300;
    bottom: 2rem;
    left: 10rem;
    position: absolute;
  }

  &::scroll-button(right) {
    content: '→';
    width: 60px;
    height: 60px;
    background: #1a1a1a;
    color: #fff;
    border-radius: 14px;
    font-size: 28px;
    cursor: pointer;
    border: 1px solid #333;
    font-weight: 300;
    bottom: 2rem;
    left: 14rem;
    position: absolute;
  }

  &::scroll-button-group {
    display: flex;
    gap: 16px;
    margin-top: 24px;
  }

  &::scroll-button(*):disabled {
    opacity: 0.2;
    cursor: not-allowed;
  }

  &::scroll-button(*):not(:disabled):hover {
    background: #333;
  }
}