Bootstrap

Bootstrap Js Carousel

Building an Image Slider with Bootstrap JS Carousel

Carousels are one of the most popular UI components for showcasing images, testimonials, or featured content on a website. With Bootstrap’s built-in Carousel component, you can create one in minutes — no external libraries needed!

In this post, you’ll learn how to use the Bootstrap Carousel with JavaScript features to add sliding transitions, indicators, autoplay, and control buttons.


Step 1: Include Bootstrap

Make sure Bootstrap CSS and JS are included in your project.

✅ CDN Version (Bootstrap 5.3+):

html
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Bootstrap Bundle JS (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>


Step 2: Basic Carousel Markup

html
<div id="myCarousel" class="carousel slide" data-bs-ride="carousel">
<!-- Indicators -->
<div class="carousel-indicators">
<button type="button" data-bs-target="#myCarousel" data-bs-slide-to="0" class="active" aria-current="true"></button>
<button type="button" data-bs-target="#myCarousel" data-bs-slide-to="1"></button>
<button type="button" data-bs-target="#myCarousel" data-bs-slide-to="2"></button>
</div>

<!-- Slides -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="img1.jpg" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="img2.jpg" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="img3.jpg" class="d-block w-100" alt="...">
</div>
</div>

<!-- Controls -->
<button class="carousel-control-prev" type="button" data-bs-target="#myCarousel" data-bs-slide="prev">
<span class="carousel-control-prev-icon"></span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#myCarousel" data-bs-slide="next">
<span class="carousel-control-next-icon"></span>
</button>
</div>


⚙️ JavaScript Features

Bootstrap’s carousel uses data attributes for most functionality, but you can also control it with JavaScript.

Example: Start, Stop, Go to Slide

javascript
const myCarousel = document.querySelector('#myCarousel');
const carousel = new bootstrap.Carousel(myCarousel);

// Start autoplay (if stopped)
carousel.cycle();

// Pause the carousel
carousel.pause();

// Go to the 2nd slide
carousel.to(1);


Common Options

You can configure the carousel via data-bs-* attributes or JS options.

Option Description Example
data-bs-ride "carousel" to enable autoplay data-bs-ride="carousel"
data-bs-interval Time between slides (ms) data-bs-interval="3000"
data-bs-pause Pause on hover ("hover" or false) data-bs-pause="false"
data-bs-wrap Loop back to first slide data-bs-wrap="true"

Customization Tips

  • Use carousel-caption inside .carousel-item for overlay text.

  • Use carousel-fade class for a cross-fade effect instead of sliding.

  • Combine with lazy loading for performance in large galleries.

html
<div class="carousel-item">
<img src="img.jpg" class="d-block w-100" loading="lazy" alt="...">
</div>

Responsive & Touch-Enabled

Bootstrap’s Carousel works well on mobile:

  • Supports swipe gestures by default

  • Fully responsive out-of-the-box


Final Thoughts

Bootstrap’s Carousel component is a powerful, flexible way to build sliders — with zero dependencies and full JavaScript control if you need it. Whether you’re showcasing products, testimonials, or blog posts, you can get a clean, responsive UI with minimal effort.


Useful Links

Leave a Reply

Your email address will not be published. Required fields are marked *