carousel.md 11.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
7
---

Mark Otto's avatar
Mark Otto committed
8
9
10
11
12
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.).

**Nested carousels are not supported.**
Mark Otto's avatar
Mark Otto committed
13

Mark Otto's avatar
Mark Otto committed
14
15
16
17
18
## Contents

* Will be replaced with the ToC, excluding the "Contents" header
{:toc}

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

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
While carousels support previous/next controls and indicators, they're not explicitly required. Add and customize as you see fit.

Please be aware the carousel doesn't automatically normalize carousel slide dimensions. As such, you may need to use additional utilities or custom styles to appropriately size content.

### 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">
  <div class="carousel-inner" role="listbox">
    <div class="carousel-item active">
      <img class="d-block img-fluid" 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 img-fluid" 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 img-fluid" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
</div>
{% endexample %}

### With controls

Adding in the previous and next controls:

{% example html %}
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner" role="listbox">
    <div class="carousel-item active">
      <img class="d-block img-fluid" 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 img-fluid" 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 img-fluid" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control carousel-control-left" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="icon-prev" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control carousel-control-right" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="icon-next" aria-hidden="true"></span>
    <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" role="listbox">
Chris Rebert's avatar
Chris Rebert committed
85
    <div class="carousel-item active">
86
      <img class="d-block img-fluid" 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">
89
      <img class="d-block img-fluid" 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">
92
      <img class="d-block img-fluid" 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 carousel-control-left" href="#carouselExampleIndicators" role="button" data-slide="prev">
Mark Otto's avatar
Mark Otto committed
96
    <span class="icon-prev" 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 carousel-control-right" href="#carouselExampleIndicators" role="button" data-slide="next">
Mark Otto's avatar
Mark Otto committed
100
    <span class="icon-next" 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

107
108
{% callout warning %}
#### Transition animations not supported in Internet Explorer 9
Mark Otto's avatar
Mark Otto committed
109

110
111
112
113
114
115
116
117
Bootstrap exclusively uses CSS3 for its animations, but Internet Explorer 9 doesn't support the necessary CSS properties. Thus, there are no slide transition animations when using that browser. We have intentionally decided not to include jQuery-based fallbacks for the transitions.
{% endcallout %}

{% callout warning %}
#### Initial active element required

The `.active` class needs to be added to one of the slides. Otherwise, the carousel will not be visible.
{% endcallout %}
Mark Otto's avatar
Mark Otto committed
118

Mark Otto's avatar
Mark Otto committed
119
120
### Optional captions

Chris Rebert's avatar
Chris Rebert committed
121
Add captions to your slides easily with the `.carousel-caption` element within any `.carousel-item`. Place just about any optional HTML within there and it will be automatically aligned and formatted.
Mark Otto's avatar
Mark Otto committed
122

123
<div class="bd-example">
Mark Otto's avatar
Mark Otto committed
124
125
126
127
128
129
130
  <div id="carousel-example-captions" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carousel-example-captions" data-slide-to="0" class="active"></li>
      <li data-target="#carousel-example-captions" data-slide-to="1"></li>
      <li data-target="#carousel-example-captions" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
Chris Rebert's avatar
Chris Rebert committed
131
      <div class="carousel-item active">
Mark Otto's avatar
Mark Otto committed
132
133
134
135
        <img data-src="holder.js/900x500/auto/#777:#777" alt="First slide image">
        <div class="carousel-caption">
          <h3>First slide label</h3>
          <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
Mark Otto's avatar
Mark Otto committed
136
        </div>
Mark Otto's avatar
Mark Otto committed
137
      </div>
Chris Rebert's avatar
Chris Rebert committed
138
      <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
139
140
141
142
        <img data-src="holder.js/900x500/auto/#666:#666" alt="Second slide image">
        <div class="carousel-caption">
          <h3>Second slide label</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
Mark Otto's avatar
Mark Otto committed
143
        </div>
Mark Otto's avatar
Mark Otto committed
144
      </div>
Chris Rebert's avatar
Chris Rebert committed
145
      <div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
146
        <img data-src="holder.js/900x500/auto/#555:#555" alt="Third slide image">
Mark Otto's avatar
Mark Otto committed
147
148
149
        <div class="carousel-caption">
          <h3>Third slide label</h3>
          <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
Mark Otto's avatar
Mark Otto committed
150
151
152
        </div>
      </div>
    </div>
Mark Otto's avatar
Mark Otto committed
153
    <a class="left carousel-control" href="#carousel-example-captions" role="button" data-slide="prev">
Mark Otto's avatar
Mark Otto committed
154
      <span class="icon-prev" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
155
156
157
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-captions" role="button" data-slide="next">
Mark Otto's avatar
Mark Otto committed
158
      <span class="icon-next" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
159
160
161
162
163
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

Mark Otto's avatar
Mark Otto committed
164
{% highlight html %}
Chris Rebert's avatar
Chris Rebert committed
165
<div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
166
167
168
169
170
171
172
173
  <img src="..." alt="...">
  <div class="carousel-caption">
    <h3>...</h3>
    <p>...</p>
  </div>
</div>
{% endhighlight %}

174
175
176
177
178
{% callout danger %}
#### Accessibility issue

The carousel component is generally not compliant with accessibility standards. If you need to be compliant, please consider other options for presenting your content.
{% endcallout %}
Mark Otto's avatar
Mark Otto committed
179
180
181

## Usage

182
### Multiple carousels
Mark Otto's avatar
Mark Otto committed
183

Mark Otto's avatar
Mark Otto committed
184
Carousels require the use of an `id` on the outermost container (the `.carousel`) for carousel controls to function properly. When adding multiple carousels, or when changing a carousel's `id`, be sure to update the relevant controls.
Mark Otto's avatar
Mark Otto committed
185

Mark Otto's avatar
Mark Otto committed
186
### Via data attributes
Mark Otto's avatar
Mark Otto committed
187

Mark Otto's avatar
Mark Otto committed
188
189
190
191
192
193
194
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
195
196
197
198
199

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

Mark Otto's avatar
Mark Otto committed
200
201
202
203
204
205
206
207
208
### Options

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

<div class="table-responsive">
  <table class="table table-bordered table-striped">
    <thead>
     <tr>
       <th style="width: 100px;">Name</th>
209
210
211
       <th style="width: 50px;">Type</th>
       <th style="width: 50px;">Default</th>
       <th>Description</th>
Mark Otto's avatar
Mark Otto committed
212
213
214
215
216
217
218
219
220
     </tr>
    </thead>
    <tbody>
     <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>
221
222
223
224
225
226
     <tr>
       <td>keyboard</td>
       <td>boolean</td>
       <td>true</td>
       <td>Whether the carousel should react to keyboard events.</td>
     </tr>
Mark Otto's avatar
Mark Otto committed
227
228
     <tr>
       <td>pause</td>
Pvanhesteren's avatar
Pvanhesteren committed
229
       <td>string | null</td>
Mark Otto's avatar
Mark Otto committed
230
       <td>"hover"</td>
Pvanhesteren's avatar
Pvanhesteren committed
231
       <td>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>null</code>, hovering over the carousel won't pause it.</td>
Mark Otto's avatar
Mark Otto committed
232
233
     </tr>
     <tr>
234
235
236
237
       <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>
Mark Otto's avatar
Mark Otto committed
238
     </tr>
Mark Otto's avatar
Mark Otto committed
239
     <tr>
240
       <td>wrap</td>
Mark Otto's avatar
Mark Otto committed
241
242
       <td>boolean</td>
       <td>true</td>
243
       <td>Whether the carousel should cycle continuously or have hard stops.</td>
Mark Otto's avatar
Mark Otto committed
244
     </tr>
Mark Otto's avatar
Mark Otto committed
245
246
247
248
249
250
    </tbody>
  </table>
</div>

### Methods

251
#### `.carousel(options)`
Mark Otto's avatar
Mark Otto committed
252
253
254

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

Mark Otto's avatar
Mark Otto committed
255
256
257
258
259
260
{% highlight js %}
$('.carousel').carousel({
  interval: 2000
})
{% endhighlight %}

261
#### `.carousel('cycle')`
Mark Otto's avatar
Mark Otto committed
262
263
264

Cycles through the carousel items from left to right.

265
#### `.carousel('pause')`
Mark Otto's avatar
Mark Otto committed
266
267
268

Stops the carousel from cycling through items.

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

Cycles the carousel to a particular frame (0 based, similar to an array).

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

Cycles to the previous item.

277
#### `.carousel('next')`
Mark Otto's avatar
Mark Otto committed
278
279
280
281
282
283
284
285
286
287

Cycles to the next item.

### 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.

Mark Otto's avatar
Mark Otto committed
288
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
Mark Otto's avatar
Mark Otto committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

<div class="table-responsive">
  <table class="table table-bordered table-striped">
    <thead>
     <tr>
       <th style="width: 150px;">Event Type</th>
       <th>Description</th>
     </tr>
    </thead>
    <tbody>
     <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>
    </tbody>
  </table>
</div>

Mark Otto's avatar
Mark Otto committed
311
312
313
314
315
{% highlight js %}
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})
{% endhighlight %}