carousel.md 13.7 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
Mark Otto's avatar
Mark Otto committed
3
title: Carousel
Mark Otto's avatar
Mark Otto committed
4
description: A slideshow component for cycling through elements—images or slides of text—like a carousel.
5
group: components
Mark Otto's avatar
Mark Otto committed
6
toc: true
Mark Otto's avatar
Mark Otto committed
7
8
---

Mark Otto's avatar
Mark Otto committed
9
10
## How it works

Mark Otto's avatar
Mark Otto committed
11
12
13
14
The carousel is a slideshow for cycling through a series of content, built with CSS 3D transforms and a bit of JavaScript. It works with a series of images, text, or custom markup. It also includes support for previous/next controls and indicators.

In browsers where the [Page Visibility API](https://www.w3.org/TR/page-visibility/) is supported, the carousel will avoid sliding when the webpage is not visible to the user (such as when the browser tab is inactive, the browser window is minimized, etc.).

15
Please be aware that nested carousels are not supported, and carousels are generally not compliant with accessibility standards.
Mark Otto's avatar
Mark Otto committed
16

17
Lastly, if you're building our JavaScript from source, it [requires `util.js`]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/javascript/#util).
18

Mark Otto's avatar
Mark Otto committed
19
## Example
Mark Otto's avatar
Mark Otto committed
20

21
Carousels don't automatically normalize slide dimensions. As such, you may need to use additional utilities or custom styles to appropriately size content. While carousels support previous/next controls and indicators, they're not explicitly required. Add and customize as you see fit.
22

23
**The `.active` class needs to be added to one of the slides** otherwise the carousel will not be visible. Also be sure to set a unique id on the `.carousel` for optional controls, especially if you're using multiple carousels on a single page.
24
25
26
27
28
29
30

### Slides only

Here's a carousel with slides only. Note the presence of the `.d-block` and `.img-fluid` on carousel images to prevent browser default image alignment.

{% example html %}
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
31
  <div class="carousel-inner">
32
    <div class="carousel-item active">
Mark Otto's avatar
Mark Otto committed
33
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
34
35
    </div>
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
36
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
37
38
    </div>
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
39
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
40
41
42
43
44
45
46
47
48
49
50
    </div>
  </div>
</div>
{% endexample %}

### With controls

Adding in the previous and next controls:

{% example html %}
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
51
  <div class="carousel-inner">
52
    <div class="carousel-item active">
Mark Otto's avatar
Mark Otto committed
53
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
54
55
    </div>
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
56
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
57
58
    </div>
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
59
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
60
61
    </div>
  </div>
62
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
Mark Otto's avatar
Mark Otto committed
63
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
64
65
    <span class="sr-only">Previous</span>
  </a>
66
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
Mark Otto's avatar
Mark Otto committed
67
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
68
69
70
71
72
73
74
75
    <span class="sr-only">Next</span>
  </a>
</div>
{% endexample %}

### With indicators

You can also add the indicators to the carousel, alongside the controls, too.
76

Mark Otto's avatar
Mark Otto committed
77
{% example html %}
78
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
Mark Otto's avatar
Mark Otto committed
79
  <ol class="carousel-indicators">
80
81
82
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
Mark Otto's avatar
Mark Otto committed
83
  </ol>
84
  <div class="carousel-inner">
Chris Rebert's avatar
Chris Rebert committed
85
    <div class="carousel-item active">
Mark Otto's avatar
Mark Otto committed
86
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
Mark Otto's avatar
Mark Otto committed
87
    </div>
Chris Rebert's avatar
Chris Rebert committed
88
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
89
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
Mark Otto's avatar
Mark Otto committed
90
    </div>
Chris Rebert's avatar
Chris Rebert committed
91
    <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
92
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
93
    </div>
Mark Otto's avatar
Mark Otto committed
94
  </div>
95
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
Mark Otto's avatar
Mark Otto committed
96
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
97
    <span class="sr-only">Previous</span>
Mark Otto's avatar
Mark Otto committed
98
  </a>
99
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
Mark Otto's avatar
Mark Otto committed
100
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
101
    <span class="sr-only">Next</span>
Mark Otto's avatar
Mark Otto committed
102
103
  </a>
</div>
Mark Otto's avatar
Mark Otto committed
104
{% endexample %}
Mark Otto's avatar
Mark Otto committed
105

106
### With captions
Mark Otto's avatar
Mark Otto committed
107

Mark Otto's avatar
Mark Otto committed
108
Add captions to your slides easily with the `.carousel-caption` element within any `.carousel-item`. They can be easily hidden on smaller viewports, as shown below, with optional [display utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/display/). We hide them initially with `.d-none` and bring them back on medium-sized devices with `.d-md-block`.
Mark Otto's avatar
Mark Otto committed
109

110
<div class="bd-example">
111
  <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
Mark Otto's avatar
Mark Otto committed
112
    <ol class="carousel-indicators">
113
114
115
      <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
      <li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
Mark Otto's avatar
Mark Otto committed
116
    </ol>
117
    <div class="carousel-inner">
Chris Rebert's avatar
Chris Rebert committed
118
      <div class="carousel-item active">
Mark Otto's avatar
Mark Otto committed
119
        <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
120
        <div class="carousel-caption d-none d-md-block">
121
          <h5>First slide label</h5>
Mark Otto's avatar
Mark Otto committed
122
          <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
Mark Otto's avatar
Mark Otto committed
123
        </div>
Mark Otto's avatar
Mark Otto committed
124
      </div>
Chris Rebert's avatar
Chris Rebert committed
125
      <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
126
        <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
127
        <div class="carousel-caption d-none d-md-block">
128
          <h5>Second slide label</h5>
Mark Otto's avatar
Mark Otto committed
129
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
Mark Otto's avatar
Mark Otto committed
130
        </div>
Mark Otto's avatar
Mark Otto committed
131
      </div>
Chris Rebert's avatar
Chris Rebert committed
132
      <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
133
        <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
134
        <div class="carousel-caption d-none d-md-block">
135
          <h5>Third slide label</h5>
Mark Otto's avatar
Mark Otto committed
136
          <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
Mark Otto's avatar
Mark Otto committed
137
138
139
        </div>
      </div>
    </div>
Quy's avatar
Quy committed
140
141
    <a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
142
143
      <span class="sr-only">Previous</span>
    </a>
Quy's avatar
Quy committed
144
145
    <a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
146
147
148
149
150
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

Mark Otto's avatar
Mark Otto committed
151
{% highlight html %}
Chris Rebert's avatar
Chris Rebert committed
152
<div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
153
  <img src="..." alt="...">
154
  <div class="carousel-caption d-none d-md-block">
155
    <h5>...</h5>
Mark Otto's avatar
Mark Otto committed
156
157
158
159
160
    <p>...</p>
  </div>
</div>
{% endhighlight %}

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
### Crossfade

Add `.carousel-fade` to your carousel to animate slides with a fade transition instead of a slide.

{% example html %}
<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleFade" 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="#carouselExampleFade" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
{% endexample %}


Mark Otto's avatar
Mark Otto committed
190
191
192
## Usage

### Via data attributes
Mark Otto's avatar
Mark Otto committed
193

Mark Otto's avatar
Mark Otto committed
194
195
196
197
198
199
200
Use data attributes to easily control the position of the carousel. `data-slide` accepts the keywords `prev` or `next`, which alters the slide position relative to its current position. Alternatively, use `data-slide-to` to pass a raw slide index to the carousel `data-slide-to="2"`, which shifts the slide position to a particular index beginning with `0`.

The `data-ride="carousel"` attribute is used to mark a carousel as animating starting at page load. **It cannot be used in combination with (redundant and unnecessary) explicit JavaScript initialization of the same carousel.**

### Via JavaScript

Call carousel manually with:
Mark Otto's avatar
Mark Otto committed
201
202
203
204
205

{% highlight js %}
$('.carousel').carousel()
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
206
207
208
209
### Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-interval=""`.

Mark Otto's avatar
Mark Otto committed
210
<table class="table table-bordered table-striped">
211
  <thead>
212
213
214
215
216
217
    <tr>
      <th style="width: 100px;">Name</th>
      <th style="width: 50px;">Type</th>
      <th style="width: 50px;">Default</th>
      <th>Description</th>
    </tr>
218
219
  </thead>
  <tbody>
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    <tr>
      <td>interval</td>
      <td>number</td>
      <td>5000</td>
      <td>The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.</td>
    </tr>
    <tr>
      <td>keyboard</td>
      <td>boolean</td>
      <td>true</td>
      <td>Whether the carousel should react to keyboard events.</td>
    </tr>
    <tr>
      <td>pause</td>
      <td>string | boolean</td>
      <td>"hover"</td>
      <td><p>If set to <code>"hover"</code>, pauses the cycling of the carousel on <code>mouseenter</code> and resumes the cycling of the carousel on <code>mouseleave</code>. If set to <code>false</code>, hovering over the carousel won't pause it.</p>
      <p>On touch-enabled devices, when set to <code>"hover"</code>, cycling will pause on <code>touchend</code> (once the user finished interacting with the carousel) for two intervals, before automatically resuming. Note that this is in addition to the above mouse behavior.</p></td>
    </tr>
    <tr>
      <td>ride</td>
      <td>string</td>
      <td>false</td>
      <td>Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.</td>
    </tr>
    <tr>
      <td>wrap</td>
      <td>boolean</td>
      <td>true</td>
      <td>Whether the carousel should cycle continuously or have hard stops.</td>
    </tr>
251
252
  </tbody>
</table>
Mark Otto's avatar
Mark Otto committed
253
254
255

### Methods

256
257
258
{% capture callout-include %}{% include callout-danger-async-methods.md %}{% endcapture %}
{{ callout-include | markdownify }}

259
#### `.carousel(options)`
Mark Otto's avatar
Mark Otto committed
260
261
262

Initializes the carousel with an optional options `object` and starts cycling through items.

Mark Otto's avatar
Mark Otto committed
263
264
265
266
267
268
{% highlight js %}
$('.carousel').carousel({
  interval: 2000
})
{% endhighlight %}

269
#### `.carousel('cycle')`
Mark Otto's avatar
Mark Otto committed
270
271
272

Cycles through the carousel items from left to right.

273
#### `.carousel('pause')`
Mark Otto's avatar
Mark Otto committed
274
275
276

Stops the carousel from cycling through items.

277
#### `.carousel(number)`
Mark Otto's avatar
Mark Otto committed
278

279
Cycles the carousel to a particular frame (0 based, similar to an array). **Returns to the caller before the target item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
Mark Otto's avatar
Mark Otto committed
280

281
#### `.carousel('prev')`
Mark Otto's avatar
Mark Otto committed
282

283
Cycles to the previous item. **Returns to the caller before the previous item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
Mark Otto's avatar
Mark Otto committed
284

285
#### `.carousel('next')`
Mark Otto's avatar
Mark Otto committed
286

287
Cycles to the next item. **Returns to the caller before the next item has been shown** (i.e. before the `slid.bs.carousel` event occurs).
Mark Otto's avatar
Mark Otto committed
288

289
#### `.carousel('dispose')`
290
291
292

Destroys an element's carousel.

Mark Otto's avatar
Mark Otto committed
293
294
295
296
297
298
### Events

Bootstrap's carousel class exposes two events for hooking into carousel functionality. Both events have the following additional properties:

- `direction`: The direction in which the carousel is sliding (either `"left"` or `"right"`).
- `relatedTarget`: The DOM element that is being slid into place as the active item.
299
300
- `from`: The index of the current item
- `to`: The index of the next item
Mark Otto's avatar
Mark Otto committed
301

Mark Otto's avatar
Mark Otto committed
302
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
Mark Otto's avatar
Mark Otto committed
303

Mark Otto's avatar
Mark Otto committed
304
<table class="table table-bordered table-striped">
305
  <thead>
306
307
308
309
    <tr>
      <th style="width: 150px;">Event Type</th>
      <th>Description</th>
    </tr>
310
311
  </thead>
  <tbody>
312
313
314
315
316
317
318
319
    <tr>
      <td>slide.bs.carousel</td>
      <td>This event fires immediately when the <code>slide</code> instance method is invoked.</td>
    </tr>
    <tr>
      <td>slid.bs.carousel</td>
      <td>This event is fired when the carousel has completed its slide transition.</td>
    </tr>
320
321
  </tbody>
</table>
Mark Otto's avatar
Mark Otto committed
322

Mark Otto's avatar
Mark Otto committed
323
324
325
326
327
{% highlight js %}
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})
{% endhighlight %}
328
329
330
331

### Change transition duration

The transition duration of `.carousel-item` can be changed with the `$carousel-transition` Sass variable before compiling or custom styles if you're using the compiled CSS. If multiple transitions are applied, make sure the transform transition is defined first (eg. `transition: transform 2s ease, opacity .5s ease-out`). The transition duration must be the same for each carousel item.