Infinite Marquee Carousel With Pure CSS

Category: CSS & CSS3 , Slider | July 14, 2023
Author:kenballon
Views Total:920 views
Official Page:Go to website
Last Update:July 14, 2023
License:MIT

Preview:

Infinite Marquee Carousel With Pure CSS

Description:

A lightweight project that allows you to build automatic, infinitely scrolling carousels using only CSS.

It comes with an optional JS extension that provides enhanced interactivity, such as pause-on-hover functionality and mouse drag & touch gesture support, elevating user experience.

How to use it:

1. Build the HTML structure for the marquee carousel.

<div class="marquee-wrapper" style="user-select: none;">
  <div class="marquee-content scrollingX">
    <div class="card-testimonial">
      <article>
        <picture>
          <source media="(min-width: 768px)" srcset="1.jpg">
          <img src="1.jpg" alt="">
        </picture>
        <h4>Title 1</h4>
        <article class="short-description">
          <p>Description 1</p>
        </article>
      </article>
    </div>
    <div class="card-testimonial">
      <article>
        <picture>
          <source media="(min-width: 768px)" srcset="2.jpg">
          <img src="2.jpg" alt="">
        </picture>
        <h4>Title 2</h4>
        <article class="short-description">
          <p>Description 2</p>
        </article>
      </article>
    </div>
    ... more carousel items here
  </div>
</div>

2. The necessary CSS styles for the marquee carousel.

.marquee-wrapper {
  position: relative;
  display: flex;
  overflow: hidden;
  gap: 1rem;
  border-radius: 1rem;
}
.marquee-wrapper .marquee-content {
  position: relative;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  gap: 1rem;
  cursor: grab;
}
.marquee-wrapper .marquee-content .card-testimonial {
  max-width: 400px;
  background-color: #333;
  padding: 1rem;
  border-radius: 1rem;
  color: #f5f5f5;
}
.marquee-wrapper .marquee-content .card-testimonial article picture {
  position: relative;
  min-width: 200px;
}
.marquee-wrapper .marquee-content .card-testimonial article picture img {
  width: 100%;
  height: 340px;
  border-radius: 0.5rem;
  -o-object-fit: cover;
  object-fit: cover;
  -o-object-position: 50% 15%;
  object-position: 50% 15%;
}
.marquee-wrapper .marquee-content .card-testimonial article h4 {
  font-size: 20px;
  text-transform: capitalize;
  margin-block: 1rem;
}
.marquee-wrapper .marquee-content .card-testimonial article article.short-description p {
  font-family: var(--ff-heading);
  font-size: 14px;
  font-weight: 300;
  line-height: 20px;
  margin-bottom: 1.2rem;
}
@media (max-width: 1024px) {
  .marquee-wrapper {
    overflow: auto;
  }
}
.scrollingX {
  animation: scroll 45s linear infinite;
}
.marquee-wrapper:hover .scrollingX {
  animation-play-state: paused;
}
@keyframes scroll {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(calc(-100% - 1rem));
  }
}

3. To enhance the carousel with the JS extension:

<script src="js/main.js" defer></script>

You Might Be Interested In:


Leave a Reply