Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 58 ยท Components

Carousel, Offcanvas and Toasts

Build a Bootstrap 5.3 carousel with indicators and controls, slide-in offcanvas panels from any edge, and stacked toast notifications, with accessibility notes.

This lesson covers three components that move or appear on top of the page: the carousel (a slideshow), the offcanvas (a panel that slides in from an edge) and toasts (small notifications that fade away). They complete the set of interactive components that began with Accordion and Collapse and Modals. All of them use the same recipe: structure in HTML, behavior from data-bs-* attributes, and the Bootstrap bundle script.

A carousel cycles through slides: images, banners or any content. Use it sparingly. In practice, many visitors never move past the first slide, so never hide your most important message on slide three. Still, for a product gallery or a testimonial strip, it is the right tool.

The structure has three parts: the outer .carousel with an id, the .carousel-inner holding .carousel-item slides (exactly one has active), and optional controls.

flowchart TD
  R[".carousel.slide#id"] --> I[".carousel-indicators<br/>(buttons, optional)"]
  R --> N[".carousel-inner"]
  R --> P[".carousel-control-prev / next<br/>(buttons, optional)"]
  N --> S1[".carousel-item.active"]
  N --> S2[".carousel-item"]
  N --> S3[".carousel-item"]

A carousel is a wrapper with a track of slides plus optional indicator dots and previous/next buttons.

Previous and next buttons use data-bs-slide="prev" or "next", and indicators use data-bs-slide-to="0" (a zero-based slide number). Both need data-bs-target="#id" to say which carousel they steer. Because the arrows are drawn by CSS icons, give each control a visually-hidden text label and give indicators an aria-label.

The example below uses colored panels instead of photos, so it works anywhere. In a real page you would put <img class="d-block w-100" ...> inside each slide, with a meaningful alt text.

A carousel with indicators, controls and captions

Captions use d-none d-md-block so they are hidden on narrow screens, where text on top of a small image is hard to read.

Autoplay, fade and timing

By default the slideshow moves only when someone clicks. These options change that:

  • data-bs-ride="carousel" starts cycling as soon as the page loads. data-bs-ride="true" waits until the visitor has interacted once.
  • data-bs-interval="2000" on the carousel (or on a single .carousel-item) sets the pause per slide in milliseconds. The default is 5000.
  • data-bs-pause="hover" (the default) pauses while the pointer is over the carousel; data-bs-pause="false" keeps running.
  • data-bs-touch="false" turns off swipe gestures.
  • carousel-fade on the wrapper replaces the slide movement with a cross-fade.
  • data-bs-wrap="false" stops at the last slide instead of looping.

Automatic movement is an accessibility problem. People with attention or reading difficulties may not finish reading a slide before it leaves, and WCAG requires a way to pause content that moves for more than five seconds. If you autoplay, add a visible pause button. Bootstrap also respects prefers-reduced-motion: the slide animation is switched off for visitors who ask their system for less motion.

Fade, autoplay and a pause button

The script uses the plugin's JavaScript methods pause() and cycle(). Plugin Events and Programmatic Control explains this way of working in depth.

The offcanvas

An offcanvas is a panel that slides in from the left, right, top or bottom of the viewport. It behaves like a modal (dimmed backdrop, focus handling, Escape to close) but is shaped like a drawer. Typical uses: a mobile navigation menu, a shopping cart, a filter panel.

The markup mirrors a modal, but with simpler parts: .offcanvas plus a placement class, .offcanvas-header and .offcanvas-body. The placement classes are:

Class Slides in from
offcanvas-start the left (right in RTL)
offcanvas-end the right
offcanvas-top the top
offcanvas-bottom the bottom

Trigger it with data-bs-toggle="offcanvas" and data-bs-target="#id". Useful options as data attributes: data-bs-scroll="true" lets the page behind scroll, data-bs-backdrop="false" removes the dimming, and data-bs-backdrop="static" stops clicks on the backdrop from closing it.

Offcanvas from the left and the bottom

Responsive offcanvas

Replace offcanvas with offcanvas-lg (or -sm, -md, -xl, -xxl) and the panel behaves like normal inline content from that breakpoint upward, but slides in as a drawer below it. This is how the navbar can show a full menu on a desktop and a drawer on a phone, using one copy of the markup.

Toasts

A toast is a lightweight notification: "Saved", "New message", "Link copied". It appears, stays a few seconds and disappears on its own. Unlike an alert, a toast is not part of the page flow; it floats in a corner inside a .toast-container, and several toasts stack there.

A toast has the classes toast, role="alert", aria-live="assertive" and aria-atomic="true", so screen readers announce it. Inside it, toast-header (optional) holds a title, a time and a btn-close with data-bs-dismiss="toast", and toast-body holds the message. Use position-fixed bottom-0 end-0 p-3 on the container to pin it to the bottom-right corner.

Toasts are opt-in. You create one with new bootstrap.Toast(element, options) and call .show(). Options:

Option Default Meaning
animation true fade in and out
autohide true hide automatically
delay 5000 milliseconds before auto-hide

Content that arrives dynamically (a message sent by the user) is best created from a template: clone it, set the text with textContent, append it to the container, show it, and remove it from the DOM when it is hidden. Otherwise old toasts pile up as invisible nodes.

Stacked toasts

Do not rely on toasts alone. A toast vanishes after a few seconds, so someone who is reading slowly or using a screen magnifier may miss it. Use toasts for confirmations, and keep errors that need action (a failed payment, a validation problem) visible in the page, for example in an alert.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you make a carousel indicator jump to the third slide?

    Slide numbers start at zero, so the third slide is 2, and the button needs data-bs-target to name the carousel.
  2. 2. Which is the most accessible way to run an autoplaying carousel?

    Moving content that lasts more than five seconds needs a way to pause it. Bootstrap also respects prefers-reduced-motion.
  3. 3. Which class makes an offcanvas panel slide in from the right edge?

    Bootstrap 5 uses start and end so layouts flip in right-to-left languages. offcanvas-right does not exist.
  4. 4. What must you do before a toast element can appear on the page?

    Toasts are opt-in. The plugin has to be initialized and told to show the toast.
  5. 5. What does the class offcanvas-lg do?

    Responsive offcanvas classes turn the panel into ordinary content at the chosen breakpoint and above.

Summary

  • A carousel is .carousel > .carousel-inner > .carousel-item (one active), with optional indicators and prev/next controls tied to its id.
  • Autoplay uses data-bs-ride, timing uses data-bs-interval, and carousel-fade swaps sliding for a cross-fade; always offer a pause control if it moves on its own.
  • An offcanvas is a drawer with placement classes offcanvas-start, -end, -top and -bottom, and responsive forms like offcanvas-lg.
  • Toasts live in a .toast-container, are opt-in (new bootstrap.Toast(el, { delay })) and should be removed from the DOM after hidden.bs.toast.
  • Hide nothing essential in a carousel or a toast, label every control, and keep errors visible in the page.

Next lesson: Bootstrap JavaScript Plugins.