carousel.md 11.2 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>
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" 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-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
107
108
109
110
{% 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
111

112
### With captions
Mark Otto's avatar
Mark Otto committed
113

114
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
115

116
<div class="bd-example">
117
  <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
Mark Otto's avatar
Mark Otto committed
118
    <ol class="carousel-indicators">
119
120
121
      <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
122
123
    </ol>
    <div class="carousel-inner" role="listbox">
Chris Rebert's avatar
Chris Rebert committed
124
      <div class="carousel-item active">
125
126
        <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
127
128
          <h3>First slide label</h3>
          <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
Mark Otto's avatar
Mark Otto committed
129
        </div>
Mark Otto's avatar
Mark Otto committed
130
      </div>
Chris Rebert's avatar
Chris Rebert committed
131
      <div class="carousel-item">
132
133
        <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
134
135
          <h3>Second slide label</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</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">
139
140
        <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
141
142
          <h3>Third slide label</h3>
          <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
Mark Otto's avatar
Mark Otto committed
143
144
145
        </div>
      </div>
    </div>
Quy's avatar
Quy committed
146
147
    <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
148
149
      <span class="sr-only">Previous</span>
    </a>
Quy's avatar
Quy committed
150
151
    <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
152
153
154
155
156
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

Mark Otto's avatar
Mark Otto committed
157
{% highlight html %}
Chris Rebert's avatar
Chris Rebert committed
158
<div class="carousel-item">
Mark Otto's avatar
Mark Otto committed
159
  <img src="..." alt="...">
160
  <div class="carousel-caption d-none d-md-block">
Mark Otto's avatar
Mark Otto committed
161
162
163
164
165
166
    <h3>...</h3>
    <p>...</p>
  </div>
</div>
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
167
168
169
## Usage

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

Mark Otto's avatar
Mark Otto committed
171
172
173
174
175
176
177
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
178
179
180
181
182

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

Mark Otto's avatar
Mark Otto committed
183
184
185
186
### Options

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

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<table class="table table-bordered table-striped table-responsive">
  <thead>
   <tr>
     <th style="width: 100px;">Name</th>
     <th style="width: 50px;">Type</th>
     <th style="width: 50px;">Default</th>
     <th>Description</th>
   </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>
   <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 | null</td>
     <td>"hover"</td>
     <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>
   </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>
  </tbody>
</table>
Mark Otto's avatar
Mark Otto committed
229
230
231

### Methods

232
#### `.carousel(options)`
Mark Otto's avatar
Mark Otto committed
233
234
235

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

Mark Otto's avatar
Mark Otto committed
236
237
238
239
240
241
{% highlight js %}
$('.carousel').carousel({
  interval: 2000
})
{% endhighlight %}

242
#### `.carousel('cycle')`
Mark Otto's avatar
Mark Otto committed
243
244
245

Cycles through the carousel items from left to right.

246
#### `.carousel('pause')`
Mark Otto's avatar
Mark Otto committed
247
248
249

Stops the carousel from cycling through items.

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

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

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

Cycles to the previous item.

258
#### `.carousel('next')`
Mark Otto's avatar
Mark Otto committed
259
260
261
262
263
264
265
266
267
268

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
269
All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`).
Mark Otto's avatar
Mark Otto committed
270

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<table class="table table-bordered table-striped table-responsive">
  <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>
Mark Otto's avatar
Mark Otto committed
289

Mark Otto's avatar
Mark Otto committed
290
291
292
293
294
{% highlight js %}
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})
{% endhighlight %}