Bootstrap ยท Chapter 30 of 43

Bootstrap Offcanvas

Offcanvas is a sidebar panel that slides in from the edge of the screen, commonly used for mobile navigation menus, filters, or shopping cart previews, without leaving the current page.

Similar to a modal, an offcanvas panel is triggered with data-bs-toggle="offcanvas" and can slide in from the start, end, top, or bottom of the viewport.

Syntax
<div class="offcanvas offcanvas-start" id="myOffcanvas">...</div>

Offcanvas structure

The offcanvas panel uses .offcanvas plus a placement class like .offcanvas-start (left) or .offcanvas-end (right), and contains .offcanvas-header and .offcanvas-body sections.

Triggering and closing

A button with data-bs-toggle="offcanvas" and data-bs-target opens the panel. A close button with data-bs-dismiss="offcanvas" closes it, similar to modals.

Example 1 (html)
<button class="btn btn-primary" data-bs-toggle="offcanvas" data-bs-target="#sidebar">
  Open Menu
</button>
<div class="offcanvas offcanvas-start" id="sidebar">
  <div class="offcanvas-header">
    <h5>Menu</h5>
    <button class="btn-close" data-bs-dismiss="offcanvas"></button>
  </div>
  <div class="offcanvas-body">
    <a href="#" class="d-block mb-2">Home</a>
    <a href="#" class="d-block mb-2">About</a>
  </div>
</div>
Output
Clicking 'Open Menu' slides a sidebar panel in from the left with navigation links

offcanvas-start makes the panel slide in from the left edge, and the button data-bs-dismiss closes it again.

Example 2 (html)
<div class="offcanvas offcanvas-end" id="cartPanel">...</div>
Output
A panel that slides in from the right side of the screen

offcanvas-end is often used for shopping cart previews or filter panels on the right.

Key points

  • Offcanvas creates a sliding sidebar panel.
  • Placement classes include offcanvas-start, offcanvas-end, offcanvas-top, offcanvas-bottom.
  • data-bs-toggle="offcanvas" and data-bs-target open the panel.
  • data-bs-dismiss="offcanvas" closes the panel.
๐Ÿ’ก Note: By default an offcanvas panel dims the rest of the page with a backdrop, similar to a modal.

๐Ÿ“ Quick Quiz

1. Which class slides a panel in from the left?

2. Which attribute opens an offcanvas panel?

3. What commonly uses offcanvas panels?