| commit | author | age | ||
| 83c3f6 | 1 | --- |
| SP | 2 | layout: docs |
| 3 | title: Carousel | |
| 4 | description: A slideshow component for cycling through elements—images or slides of text—like a carousel. | |
| 5 | group: components | |
| 6 | toc: true | |
| 7 | --- | |
| 8 | ||
| 9 | ## How it works | |
| 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. | |
| 12 | ||
| 13 | 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.). | |
| 14 | ||
| 15 | {% include callout-info-prefersreducedmotion.md %} | |
| 16 | ||
| 17 | Please be aware that nested carousels are not supported, and carousels are generally not compliant with accessibility standards. | |
| 18 | ||
| 19 | Lastly, if you're building our JavaScript from source, it [requires `util.js`]({{ site.baseurl }}/docs/{{ site.docs_version }}/getting-started/javascript/#util). | |
| 20 | ||
| 21 | ## Example | |
| 22 | ||
| 23 | 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. | |
| 24 | ||
| 25 | **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. Control and indicator elements must have a `data-target` attribute (or `href` for links) that matches the id of the `.carousel` element. | |
| 26 | ||
| 27 | ### Slides only | |
| 28 | ||
| 29 | Here's a carousel with slides only. Note the presence of the `.d-block` and `.w-100` on carousel images to prevent browser default image alignment. | |
| 30 | ||
| 31 | {% capture example %} | |
| 32 | <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel"> | |
| 33 | <div class="carousel-inner"> | |
| 34 | <div class="carousel-item active"> | |
| 35 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 36 | </div> | |
| 37 | <div class="carousel-item"> | |
| 38 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 39 | </div> | |
| 40 | <div class="carousel-item"> | |
| 41 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 42 | </div> | |
| 43 | </div> | |
| 44 | </div> | |
| 45 | {% endcapture %} | |
| 46 | {% include example.html content=example %} | |
| 47 | ||
| 48 | ### With controls | |
| 49 | ||
| 50 | Adding in the previous and next controls: | |
| 51 | ||
| 52 | {% capture example %} | |
| 53 | <div id="carouselExampleControls" class="carousel slide" data-ride="carousel"> | |
| 54 | <div class="carousel-inner"> | |
| 55 | <div class="carousel-item active"> | |
| 56 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 57 | </div> | |
| 58 | <div class="carousel-item"> | |
| 59 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 60 | </div> | |
| 61 | <div class="carousel-item"> | |
| 62 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 63 | </div> | |
| 64 | </div> | |
| 65 | <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev"> | |
| 66 | <span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
| 67 | <span class="sr-only">Previous</span> | |
| 68 | </a> | |
| 69 | <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next"> | |
| 70 | <span class="carousel-control-next-icon" aria-hidden="true"></span> | |
| 71 | <span class="sr-only">Next</span> | |
| 72 | </a> | |
| 73 | </div> | |
| 74 | {% endcapture %} | |
| 75 | {% include example.html content=example %} | |
| 76 | ||
| 77 | ### With indicators | |
| 78 | ||
| 79 | You can also add the indicators to the carousel, alongside the controls, too. | |
| 80 | ||
| 81 | {% capture example %} | |
| 82 | <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> | |
| 83 | <ol class="carousel-indicators"> | |
| 84 | <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li> | |
| 85 | <li data-target="#carouselExampleIndicators" data-slide-to="1"></li> | |
| 86 | <li data-target="#carouselExampleIndicators" data-slide-to="2"></li> | |
| 87 | </ol> | |
| 88 | <div class="carousel-inner"> | |
| 89 | <div class="carousel-item active"> | |
| 90 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 91 | </div> | |
| 92 | <div class="carousel-item"> | |
| 93 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 94 | </div> | |
| 95 | <div class="carousel-item"> | |
| 96 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 97 | </div> | |
| 98 | </div> | |
| 99 | <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev"> | |
| 100 | <span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
| 101 | <span class="sr-only">Previous</span> | |
| 102 | </a> | |
| 103 | <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next"> | |
| 104 | <span class="carousel-control-next-icon" aria-hidden="true"></span> | |
| 105 | <span class="sr-only">Next</span> | |
| 106 | </a> | |
| 107 | </div> | |
| 108 | {% endcapture %} | |
| 109 | {% include example.html content=example %} | |
| 110 | ||
| 111 | ### With captions | |
| 112 | ||
| 113 | 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`. | |
| 114 | ||
| 115 | <div class="bd-example"> | |
| 116 | <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel"> | |
| 117 | <ol class="carousel-indicators"> | |
| 118 | <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li> | |
| 119 | <li data-target="#carouselExampleCaptions" data-slide-to="1"></li> | |
| 120 | <li data-target="#carouselExampleCaptions" data-slide-to="2"></li> | |
| 121 | </ol> | |
| 122 | <div class="carousel-inner"> | |
| 123 | <div class="carousel-item active"> | |
| 124 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 125 | <div class="carousel-caption d-none d-md-block"> | |
| 126 | <h5>First slide label</h5> | |
| 127 | <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p> | |
| 128 | </div> | |
| 129 | </div> | |
| 130 | <div class="carousel-item"> | |
| 131 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 132 | <div class="carousel-caption d-none d-md-block"> | |
| 133 | <h5>Second slide label</h5> | |
| 134 | <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> | |
| 135 | </div> | |
| 136 | </div> | |
| 137 | <div class="carousel-item"> | |
| 138 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 139 | <div class="carousel-caption d-none d-md-block"> | |
| 140 | <h5>Third slide label</h5> | |
| 141 | <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p> | |
| 142 | </div> | |
| 143 | </div> | |
| 144 | </div> | |
| 145 | <a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev"> | |
| 146 | <span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
| 147 | <span class="sr-only">Previous</span> | |
| 148 | </a> | |
| 149 | <a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next"> | |
| 150 | <span class="carousel-control-next-icon" aria-hidden="true"></span> | |
| 151 | <span class="sr-only">Next</span> | |
| 152 | </a> | |
| 153 | </div> | |
| 154 | </div> | |
| 155 | ||
| 156 | {% highlight html %} | |
| 157 | <div class="carousel-item"> | |
| 158 | <img src="..." alt="..."> | |
| 159 | <div class="carousel-caption d-none d-md-block"> | |
| 160 | <h5>...</h5> | |
| 161 | <p>...</p> | |
| 162 | </div> | |
| 163 | </div> | |
| 164 | {% endhighlight %} | |
| 165 | ||
| 166 | ### Crossfade | |
| 167 | ||
| 168 | Add `.carousel-fade` to your carousel to animate slides with a fade transition instead of a slide. | |
| 169 | ||
| 170 | {% capture example %} | |
| 171 | <div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel"> | |
| 172 | <div class="carousel-inner"> | |
| 173 | <div class="carousel-item active"> | |
| 174 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 175 | </div> | |
| 176 | <div class="carousel-item"> | |
| 177 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 178 | </div> | |
| 179 | <div class="carousel-item"> | |
| 180 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 181 | </div> | |
| 182 | </div> | |
| 183 | <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev"> | |
| 184 | <span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
| 185 | <span class="sr-only">Previous</span> | |
| 186 | </a> | |
| 187 | <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next"> | |
| 188 | <span class="carousel-control-next-icon" aria-hidden="true"></span> | |
| 189 | <span class="sr-only">Next</span> | |
| 190 | </a> | |
| 191 | </div> | |
| 192 | {% endcapture %} | |
| 193 | {% include example.html content=example %} | |
| 194 | ||
| 195 | ### Individual `.carousel-item` interval | |
| 196 | ||
| 197 | Add `data-interval=""` to a `.carousel-item` to change the amount of time to delay between automatically cycling to the next item. | |
| 198 | ||
| 199 | {% capture example %} | |
| 200 | <div id="carouselExampleInterval" class="carousel slide" data-ride="carousel"> | |
| 201 | <div class="carousel-inner"> | |
| 202 | <div class="carousel-item active" data-interval="10000"> | |
| 203 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#555" background="#777" text="First slide" %} | |
| 204 | </div> | |
| 205 | <div class="carousel-item" data-interval="2000"> | |
| 206 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#444" background="#666" text="Second slide" %} | |
| 207 | </div> | |
| 208 | <div class="carousel-item"> | |
| 209 | {% include icons/placeholder.svg width="800" height="400" class="bd-placeholder-img-lg d-block w-100" color="#333" background="#555" text="Third slide" %} | |
| 210 | </div> | |
| 211 | </div> | |
| 212 | <a class="carousel-control-prev" href="#carouselExampleInterval" role="button" data-slide="prev"> | |
| 213 | <span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
| 214 | <span class="sr-only">Previous</span> | |
| 215 | </a> | |
| 216 | <a class="carousel-control-next" href="#carouselExampleInterval" role="button" data-slide="next"> | |
| 217 | <span class="carousel-control-next-icon" aria-hidden="true"></span> | |
| 218 | <span class="sr-only">Next</span> | |
| 219 | </a> | |
| 220 | </div> | |
| 221 | {% endcapture %} | |
| 222 | {% include example.html content=example %} | |
| 223 | ||
| 224 | ||
| 225 | ## Usage | |
| 226 | ||
| 227 | ### Via data attributes | |
| 228 | ||
| 229 | 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`. | |
| 230 | ||
| 231 | 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.** | |
| 232 | ||
| 233 | ### Via JavaScript | |
| 234 | ||
| 235 | Call carousel manually with: | |
| 236 | ||
| 237 | {% highlight js %} | |
| 238 | $('.carousel').carousel() | |
| 239 | {% endhighlight %} | |
| 240 | ||
| 241 | ### Options | |
| 242 | ||
| 243 | Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-interval=""`. | |
| 244 | ||
| 245 | <table class="table table-bordered table-striped"> | |
| 246 | <thead> | |
| 247 | <tr> | |
| 248 | <th style="width: 100px;">Name</th> | |
| 249 | <th style="width: 50px;">Type</th> | |
| 250 | <th style="width: 50px;">Default</th> | |
| 251 | <th>Description</th> | |
| 252 | </tr> | |
| 253 | </thead> | |
| 254 | <tbody> | |
| 255 | <tr> | |
| 256 | <td>interval</td> | |
| 257 | <td>number</td> | |
| 258 | <td>5000</td> | |
| 259 | <td>The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.</td> | |
| 260 | </tr> | |
| 261 | <tr> | |
| 262 | <td>keyboard</td> | |
| 263 | <td>boolean</td> | |
| 264 | <td>true</td> | |
| 265 | <td>Whether the carousel should react to keyboard events.</td> | |
| 266 | </tr> | |
| 267 | <tr> | |
| 268 | <td>pause</td> | |
| 269 | <td>string | boolean</td> | |
| 270 | <td>"hover"</td> | |
| 271 | <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> | |
| 272 | <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> | |
| 273 | </tr> | |
| 274 | <tr> | |
| 275 | <td>ride</td> | |
| 276 | <td>string</td> | |
| 277 | <td>false</td> | |
| 278 | <td>Autoplays the carousel after the user manually cycles the first item. If "carousel", autoplays the carousel on load.</td> | |
| 279 | </tr> | |
| 280 | <tr> | |
| 281 | <td>wrap</td> | |
| 282 | <td>boolean</td> | |
| 283 | <td>true</td> | |
| 284 | <td>Whether the carousel should cycle continuously or have hard stops.</td> | |
| 285 | </tr> | |
| 286 | <tr> | |
| 287 | <td>touch</td> | |
| 288 | <td>boolean</td> | |
| 289 | <td>true</td> | |
| 290 | <td>Whether the carousel should support left/right swipe interactions on touchscreen devices.</td> | |
| 291 | </tr> | |
| 292 | </tbody> | |
| 293 | </table> | |
| 294 | ||
| 295 | ### Methods | |
| 296 | ||
| 297 | {% include callout-danger-async-methods.md %} | |
| 298 | ||
| 299 | #### `.carousel(options)` | |
| 300 | ||
| 301 | Initializes the carousel with an optional options `object` and starts cycling through items. | |
| 302 | ||
| 303 | {% highlight js %} | |
| 304 | $('.carousel').carousel({ | |
| 305 | interval: 2000 | |
| 306 | }) | |
| 307 | {% endhighlight %} | |
| 308 | ||
| 309 | #### `.carousel('cycle')` | |
| 310 | ||
| 311 | Cycles through the carousel items from left to right. | |
| 312 | ||
| 313 | #### `.carousel('pause')` | |
| 314 | ||
| 315 | Stops the carousel from cycling through items. | |
| 316 | ||
| 317 | #### `.carousel(number)` | |
| 318 | ||
| 319 | 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). | |
| 320 | ||
| 321 | #### `.carousel('prev')` | |
| 322 | ||
| 323 | 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). | |
| 324 | ||
| 325 | #### `.carousel('next')` | |
| 326 | ||
| 327 | 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). | |
| 328 | ||
| 329 | #### `.carousel('dispose')` | |
| 330 | ||
| 331 | Destroys an element's carousel. | |
| 332 | ||
| 333 | ### Events | |
| 334 | ||
| 335 | Bootstrap's carousel class exposes two events for hooking into carousel functionality. Both events have the following additional properties: | |
| 336 | ||
| 337 | - `direction`: The direction in which the carousel is sliding (either `"left"` or `"right"`). | |
| 338 | - `relatedTarget`: The DOM element that is being slid into place as the active item. | |
| 339 | - `from`: The index of the current item | |
| 340 | - `to`: The index of the next item | |
| 341 | ||
| 342 | All carousel events are fired at the carousel itself (i.e. at the `<div class="carousel">`). | |
| 343 | ||
| 344 | <table class="table table-bordered table-striped"> | |
| 345 | <thead> | |
| 346 | <tr> | |
| 347 | <th style="width: 150px;">Event Type</th> | |
| 348 | <th>Description</th> | |
| 349 | </tr> | |
| 350 | </thead> | |
| 351 | <tbody> | |
| 352 | <tr> | |
| 353 | <td>slide.bs.carousel</td> | |
| 354 | <td>This event fires immediately when the <code>slide</code> instance method is invoked.</td> | |
| 355 | </tr> | |
| 356 | <tr> | |
| 357 | <td>slid.bs.carousel</td> | |
| 358 | <td>This event is fired when the carousel has completed its slide transition.</td> | |
| 359 | </tr> | |
| 360 | </tbody> | |
| 361 | </table> | |
| 362 | ||
| 363 | {% highlight js %} | |
| 364 | $('#myCarousel').on('slide.bs.carousel', function () { | |
| 365 | // do something… | |
| 366 | }) | |
| 367 | {% endhighlight %} | |
| 368 | ||
| 369 | ### Change transition duration | |
| 370 | ||
| 371 | 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`). | |