Carousel is a popular component used on many different types of websites. We're going to build a Carousel in the Blog Post template of our project. Let's start by opening up blog-post.ejs from the project directory in your text editor. After the page title block of code, insert the following markup:
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img src="..." alt="First slide">
</div>
<div class="carousel-item">
<img src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img src="..." alt="Third slide">
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="icon-prev" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="icon-next" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
This is a larger component like the Accordion so let's go through it section by section:
The Carousel component starts with a <div> and it needs a unique ID. In this case, #carouselOne. Also include the following classes: .carousel and .slide. Finally, you need to add the attribute data-ride with a value of carousel.
The first thing we need to add to the Carousel is the bullet or indicator navigation. It's made up of an ordered list. Here's the code, then we'll break it down:
<ol class="carousel-indicators"> <li data-target="#carouselOne" data-slide-to="0" class="active"></li> <li data-target="#carouselOne" data-slide-to="1"></li> <li data-target="#carouselOne" data-slide-to="2"></li> </ol>
Here's how the Carousel navigation works:
<ol> tag allocate a class of .carousel-indicators.<li> in the list needs to have a few things:data-target needs to be the same ID that you gave to your root Carousel <div>, in this case, #carouselOne.data-slide-to attribute and the first value should be 0. Increase it by one for each list item after the first.
The next step is to include the actual Carousel slides. I'm not going to include images in the code, that will be up to you to insert, but don't worry, I'll show you where to put them. Here's the code for the section that wraps the slides:
<div class="carousel-inner" role="listbox"> .. </div>
Give that <div> a class of .carousel-inner and add the role attribute with a value of listbox. Inside this <div> you're going to add another section for each image slide in the Carousel. Here's the code for one slide in the Carousel:
<div class="carousel-item active"> <img src="..." alt="First slide"> </div>
Let's breakdown what's happening here in the code:
<div> tag with the classes .carousel-item and .active<div>, insert an img tag with the following attributes:src attribute and the value should be the path to the image file for the slidealt attribute with a value for the image
The last thing we need to add to the Carousel is the arrow navigation. Here's the code for rendering the arrows:
<a class="left carousel-control" href="#carouselOne" role="button" data-slide="prev"> <span class="icon-prev" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#carouselOne" role="button" data-slide="next"> <span class="icon-next" aria-hidden="true"></span> <span class="sr-only">Next</span> </a>
Let me explain how the arrow navigation works:
href tags..left and .carousel-control.href for the link should be set to the main ID for the Carousel, in this case, #carouselOne.role attribute to button.data-slide attribute to prev.<span> with a class of .icon-prev on it. This will render the arrow icon. Include the aria-hidden attribute and set it to true.<span> for accessibility reasons. If you want to include it, give it a class of .sr-only. Within the <span> include the text Previous..left class to .right.data-slide attribute value to next.<span> tag change the class value to .icon-next.<span> tag change the text to Next.
That completes the setup of the Carousel component. Fire up the project server and view the Blog Post page in the browser, and it should look like this:

That concludes the chapter on JavaScript components in Bootstrap. In this chapter, I taught you how to code up the following components: Modals, Tooltips, Popovers, Collapse, Accordion, and the Carousel. In the next chapter, I'll teach you how to use Sass in Bootstrap.