Bootstrap Carousel
Reading time
6
Writer
David Johnson
Published date
Category
Bootstrap Carousel is a simple JavaScript component included right in the Bootstrap. It can be handy in many situations, mostly presenting images and their captions on your web page.
In this guide, I will walk you through the basics that will allow you using it on your page, describe its possible configuration options and I will show you how to easily modify its behaviour and make it full-screen or how to change the animation effect from slide to fade animation.
This tutorial was originally created for Bootstrap 3 in 2016.Now, in 2018, I have completely rewritten it for Bootstrap 4.If you still need to use the Bootstrap 3 version, you will find the original files in the download package and I created a new Bootstrap 3 section in this article where the main differences are outlined and I also mention few Bootstrap 3 carousel tweaks.
Demo 1 - Basic use Demo 2 - with few tweaks Download sources
1. Basic use
A basic page with a Bootstrap carousel might look like in this example.
Let's have a look at its code.
Html
Let's go through the code right now:
we include Bootstrap's stylesheet and also Bootstrap minified JavaScript library, in our case both from MaxCDN
Bootstrap's JS is dependent on jQuery and PopperJS, so we should not forget to include it too
We will place the carousel to a Bootstrap
.container
. As we want it to occupy the full width of the container, we don't need to use Bootstrap's.row
nor.col-*
classes.
Now, the carousel itself (main carousel element - div.carousel
) consists of 3 parts:
ol.carousel-indicators
(pagination-like navigation in the bottom of the carousel carousel)div.carousel-inner
(in which we will place the content to be presented)and two navigation links for the arrows -
a.carousel-control-prev
anda.carousel-control-next
.
Main carousel element
To tell Bootstrap to make a carousel from an element, just add class="carousel"
and a specific id, in our case id="carousel-example-generic"
, to a <div>
.
Notice, that we can pass options to the carousel via data attributes. In this case, we pass to it data-ride="carousel"
that tells Bootstrap to autoplay the carousel. For the complete list of options, have a look here.
Html
Carousel indicators
This is an optional part of the carousel navigation coded in <ol class="carousel-indicators">
.
Each carousel slide in .carousel-inner
needs one <li>
entry here.
Don't forget to add data-target
attribute containing the carousel's id and data-slide-to
attribute with the index of the slide it will be referring to (0 represents the first slide, 1 the second one, etc.).
The first <li>
also should have class="active"
.
Html
Carousel body
The main content of the carousel goes to <div class="carousel-inner">
.
Wrap each of the slides to a <div class="carousel-item">
and mark the first item with an .active
class too. The slide can contain an image or an image with a caption.
To create a caption, just place a <div class="carousel-caption">
with a text content into the slide.
Html
Carousel next and prev controls
For the next and previous arrow controls, you have to place two <a>
elements into your carousel's code.
Notice again that the href
attribute that should contain your carousel's id and the data-slide
attributes with either prev
or next
values.
Html
2. Options
From the previous part of this article, we should have a nice Bootstrap carousel. Now it is a great time to explore a bit its possible options.
The available options for the Bootstrap carousel are:
Nametypedefaultdescriptionintervalnumber5000The amount of time to delay between automatically cycling an item. If false, the carousel will not automatically cycle.pausestring | null"hover"If set to "hover"
, pauses the cycling of the carousel on mouseenter
and resumes the cycling of the carousel on mouseleave
. If set to null
, hovering over the carousel won't pause it.wrapbooleantrueWhether the carousel should cycle continuously or have hard stops.keyboardbooleantrueWhether the carousel should react to keyboard events.
Options can be passed via data attributes of the carousel element or via Javascript.
For configuring via data attributes, just prefix the option name with data- and use it on the carousel div.
Html
Using JavaScript:
JavaScript
3. Modifying the default look and behaviour
Fullscreen carousel with vertically-centred captions
The first of the modifications I would like to show you is how to make a nice fullscreen intro section with a Bootstrap carousel.
CSS
The solution below sets the item height using vh
units and will work in all modern browsers. In the carousel with a class="carousel-fullscreen"
, we set its slide height
to 100vh
(full height) and min-height
to 600px
(to have sufficient space for text on smaller devices).
CSS
HTML
In our HTML code, we need to change the markup a bit too. Instead of using the images in a <img>
tag, we will use them as backgrounds for the slide's .carousel-item
.
Html
Transparent overlay
Sometimes, you will need to darken the image to increase the contrast between the text and the background image. The solution is very simple, we will add just one more additional <div>
to carousel's <div class="item">
.
CSS
CSS
HTML
Html
Fade transition instead of sliding
To change the transition between slides to fade instead of sliding, all you need to do in Bootstrap 4 is to add .carousel-fade
class to your carousel.
The complete list of classes for the basic carousel would be class="carousel slide carousel-fade"
.
Change transition duration
The default duration of a slide transition is set to 0.6s. That might be a quick, especially when using the fade transition. To adjust the duration of the transition between the slides, add this definition into the CSS file. (in our case custom.css
)
CSS
Add touch and swipe support for the carousel
One of the biggest disadvantages of using Bootstrap 4 carousel is its lack of swipe and touch support.
Luckily, it is not a complicated thing to add this behaviour with jQuery.
To enable the touch and swipe support, add this snippet to a separate JS file or just inside the <script></script>
block just before the end </body>
tag.
This tweak is based on one of the SO's answers.
JavaScript
Demo
Have a look at the online demo featuring all these carousel upgrades.
Bootstrap 3
Differences between Bootstrap 4 and Bootstrap 3
The good news is that there are only a few differences in the HTML code of the carousels in Bootstrap 4 and Bootstrap 3.
items: .carousel-item (B4) = .item (B3)
fade transition: you have to code the fade transition in Bootstrap 3 yourselves (see my code below),
pagination: rectangles (B4) vs circles (B3)
Fade effect between the slides
To have the fade effect between the slides is not so straightforward in Bootstrap 3. We need to change the transition from default transform transition to an opacity transition.
This solution was published originally in this Codepen.
CSS
CSS
HTML
To use the fade effect, just add class="carouse-fade" to your carousel.
Html
Useful links
Bootstrap 4 Carousel official documentation
Bootstrap 3 Carousel official documentation
Unsplash - thank you for the beautiful images for the demos
Have a look at my other tutorials. You could like the tutorials on making a working contact form or creating lightweight share icons for your website.
Popular Templates
Meticulously crafted by pro designers and developers, so you get top-tier UI/UX and launch-ready functionality from the get-go.