carousel.md 11.8 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
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.).

12
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
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
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
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
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

### 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>
Mark Otto's avatar
Mark Otto committed
62
63
  <a class="carousel-control carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
64
65
    <span class="sr-only">Previous</span>
  </a>
Mark Otto's avatar
Mark Otto committed
66
67
  <a class="carousel-control carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <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" 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>
Mark Otto's avatar
Mark Otto committed
95
96
  <a class="carousel-control carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <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>
Mark Otto's avatar
Mark Otto committed
99
100
  <a class="carousel-control carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <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
107
{% callout warning %}
#### Transition animations not supported in Internet Explorer 9
Mark Otto's avatar
Mark Otto committed
108

109
110
111
112
113
114
115
116
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
117

118
### With captions
Mark Otto's avatar
Mark Otto committed
119

120
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 }}/utilities/display-property/). 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
121

122
<div class="bd-example">
123
  <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
Mark Otto's avatar
Mark Otto committed
124
    <ol class="carousel-indicators">
125
126
127
      <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
128
129
    </ol>
    <div class="carousel-inner" role="listbox">
Chris Rebert's avatar
Chris Rebert committed
130
      <div class="carousel-item active">
131
132
        <img class="d-block img-fluid" data-src="holder.js/800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
        <div class="carousel-caption d-none d-md-block">
Mark Otto's avatar
Mark Otto committed
133
134
          <h3>First slide label</h3>
          <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
Mark Otto's avatar
Mark Otto committed
135
        </div>
Mark Otto's avatar
Mark Otto committed
136
      </div>
Chris Rebert's avatar
Chris Rebert committed
137
      <div class="carousel-item">
138
139
        <img class="d-block img-fluid" data-src="holder.js/800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
        <div class="carousel-caption d-none d-md-block">
Mark Otto's avatar
Mark Otto committed
140
141
          <h3>Second slide label</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
Mark Otto's avatar
Mark Otto committed
142
        </div>
Mark Otto's avatar
Mark Otto committed
143
      </div>
Chris Rebert's avatar
Chris Rebert committed
144
      <div class="carousel-item">
145
146
        <img class="d-block img-fluid" data-src="holder.js/800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
        <div class="carousel-caption d-none d-md-block">
Mark Otto's avatar
Mark Otto committed
147
148
          <h3>Third slide label</h3>
          <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
Mark Otto's avatar
Mark Otto committed
149
150
151
        </div>
      </div>
    </div>
152
    <a class="carousel-control carousel-control-left" href="#carouselExampleCaptions" role="button" data-slide="prev">
Mark Otto's avatar
Mark Otto committed
153
      <span class="icon-prev" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
154
155
      <span class="sr-only">Previous</span>
    </a>
156
    <a class="carousel-control carousel-control-right" href="#carouselExampleCaptions" role="button" data-slide="next">
Mark Otto's avatar
Mark Otto committed
157
      <span class="icon-next" aria-hidden="true"></span>
Mark Otto's avatar
Mark Otto committed
158
159
160
161
162
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

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

Mark Otto's avatar
Mark Otto committed
173
174
175
## Usage

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

Mark Otto's avatar
Mark Otto committed
177
178
179
180
181
182
183
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
184
185
186
187
188

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

Mark Otto's avatar
Mark Otto committed
189
190
191
192
193
194
195
196
197
### 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>
198
199
200
       <th style="width: 50px;">Type</th>
       <th style="width: 50px;">Default</th>
       <th>Description</th>
Mark Otto's avatar
Mark Otto committed
201
202
203
204
205
206
207
208
209
     </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>
210
211
212
213
214
215
     <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
216
217
     <tr>
       <td>pause</td>
Pvanhesteren's avatar
Pvanhesteren committed
218
       <td>string | null</td>
Mark Otto's avatar
Mark Otto committed
219
       <td>"hover"</td>
Pvanhesteren's avatar
Pvanhesteren committed
220
       <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
221
222
     </tr>
     <tr>
223
224
225
226
       <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
227
     </tr>
Mark Otto's avatar
Mark Otto committed
228
     <tr>
229
       <td>wrap</td>
Mark Otto's avatar
Mark Otto committed
230
231
       <td>boolean</td>
       <td>true</td>
232
       <td>Whether the carousel should cycle continuously or have hard stops.</td>
Mark Otto's avatar
Mark Otto committed
233
     </tr>
Mark Otto's avatar
Mark Otto committed
234
235
236
237
238
239
    </tbody>
  </table>
</div>

### Methods

240
#### `.carousel(options)`
Mark Otto's avatar
Mark Otto committed
241
242
243

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

Mark Otto's avatar
Mark Otto committed
244
245
246
247
248
249
{% highlight js %}
$('.carousel').carousel({
  interval: 2000
})
{% endhighlight %}

250
#### `.carousel('cycle')`
Mark Otto's avatar
Mark Otto committed
251
252
253

Cycles through the carousel items from left to right.

254
#### `.carousel('pause')`
Mark Otto's avatar
Mark Otto committed
255
256
257

Stops the carousel from cycling through items.

258
#### `.carousel(number)`
Mark Otto's avatar
Mark Otto committed
259
260
261

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

262
#### `.carousel('prev')`
Mark Otto's avatar
Mark Otto committed
263
264
265

Cycles to the previous item.

266
#### `.carousel('next')`
Mark Otto's avatar
Mark Otto committed
267
268
269
270
271
272
273
274
275
276

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
277
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
Mark Otto's avatar
Mark Otto committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

<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
300
301
302
303
304
{% highlight js %}
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})
{% endhighlight %}