Bootstrap carousel is a flexible, responsive way to add a slideshow to your webpage. It can be used to display a sequence of images, video, or text. Bootstrap's carousel class exposes two events for hooking into carousel functionality.
To create a basic carousel in Bootstrap, you need to make a div with the class carousel
and slide
, and an id attribute. Inside this div, you will create an ordered list with the class carousel-indicators
. Each list item in this ordered list will represent a slide in the carousel.
Each slide is represented by a div with the class carousel-item
. Inside this div, you can put whatever you want to display on the slide, but typically you will add an image.
To make the carousel function, you need to initialize it with JavaScript. You can use either jQuery or vanilla JavaScript.
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Slides -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="img1.jpg" alt="Image 1">
</div>
<div class="carousel-item">
<img src="img2.jpg" alt="Image 2">
</div>
<div class="carousel-item">
<img src="img3.jpg" alt="Image 3">
</div>
</div>
<!-- Controls -->
<a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
In this example, #myCarousel
is the id of the carousel. We have three slides, each with a different image.
$('.carousel').carousel({
interval: 2000
});
This example shows how to initialize the carousel with jQuery. The interval
option specifies how long to delay between automatically cycling an item in milliseconds.
Solutions
1. This is similar to the example given. Just add two more slides to the carousel.
2. Inside each carousel-item
div, add a div
with the class carousel-caption
and put your caption inside this div.
3. Use the following code to initialize your carousel: $('.carousel').carousel({ interval: 1000 });
.