TExcelTAD
Home › Bootstrap › BS5 Offcanvas

Bootstrap 5 Offcanvas

An offcanvas is a hidden side panel that slides in from an edge of the screen — like the menu drawer in many mobile apps. It's great for navigation on small screens, filters, shopping carts and settings. It needs Bootstrap's JavaScript.

Basic offcanvas

A trigger uses data-bs-toggle="offcanvas" and points to the panel. The panel has .offcanvas, a placement class like .offcanvas-start, and an optional header and body.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#menu" aria-controls="menu">
    ☰ Open menu
  </button>

  <div class="offcanvas offcanvas-start" tabindex="-1" id="menu" aria-labelledby="menuLabel" style="--bs-offcanvas-width: 240px;">
    <div class="offcanvas-header">
      <h5 class="offcanvas-title" id="menuLabel">Menu</h5>
      <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
      <div class="list-group list-group-flush">
        <a href="#" class="list-group-item list-group-item-action">🏠 Home</a>
        <a href="#" class="list-group-item list-group-item-action">📚 Courses</a>
        <a href="#" class="list-group-item list-group-item-action">⚙️ Settings</a>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

By default the page behind is dimmed by a backdrop, clicking the backdrop or pressing Esc closes the panel, and the page can't scroll while it's open. The example sets the CSS variable --bs-offcanvas-width to make the panel narrower than the default 400px, which suits the small preview frame.

Placement

Class Slides in from
.offcanvas-start Left (start)
.offcanvas-end Right (end)
.offcanvas-top Top
.offcanvas-bottom Bottom
html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-outline-dark" data-bs-toggle="offcanvas" data-bs-target="#cart">🛒 Cart (end)</button>
  <button class="btn btn-outline-dark" data-bs-toggle="offcanvas" data-bs-target="#cookie">🍪 Bottom</button>

  <div class="offcanvas offcanvas-end" tabindex="-1" id="cart" aria-labelledby="cartLabel" style="--bs-offcanvas-width: 260px;">
    <div class="offcanvas-header">
      <h5 class="offcanvas-title" id="cartLabel">Your cart</h5>
      <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
      <p>☕ Coffee beans × 2</p>
      <p>🥐 Croissant × 1</p>
      <button class="btn btn-success w-100">Checkout</button>
    </div>
  </div>

  <div class="offcanvas offcanvas-bottom" tabindex="-1" id="cookie" aria-labelledby="cookieLabel" style="--bs-offcanvas-height: 130px;">
    <div class="offcanvas-body d-flex align-items-center gap-3">
      <span id="cookieLabel">We use cookies to remember your progress.</span>
      <button class="btn btn-primary btn-sm ms-auto" data-bs-dismiss="offcanvas">OK</button>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Backdrop and scrolling options

Attribute Default Effect
data-bs-backdrop="false" true No dimmed backdrop
data-bs-backdrop="static" — Backdrop that doesn't close the panel on click
data-bs-scroll="true" false Allow the page to scroll while the panel is open
data-bs-keyboard="false" true Esc doesn't close it
html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-secondary" data-bs-toggle="offcanvas" data-bs-target="#notes">Notes panel</button>
  <p class="mt-3">With <code>data-bs-scroll="true"</code> and no backdrop you can still scroll and read this page while the panel is open.</p>
  <p>Line</p><p>Line</p><p>Line</p><p>Line</p><p>Line</p><p>Line</p>

  <div class="offcanvas offcanvas-end" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="notes"
       aria-labelledby="notesLabel" style="--bs-offcanvas-width: 200px;" data-bs-theme="dark">
    <div class="offcanvas-header">
      <h6 class="offcanvas-title text-white" id="notesLabel">📝 Notes</h6>
      <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body small text-white">Remember to review the grid lesson.</div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const notes = document.getElementById('notes');
    notes.addEventListener('shown.bs.offcanvas', () => console.log('notes opened'));
    notes.addEventListener('hidden.bs.offcanvas', () => console.log('notes closed'));
  </script>
</body>
</html>

data-bs-theme="dark" on the panel gives it a dark background, and the close button turns light automatically.

Responsive offcanvas

The classes .offcanvas-sm, -md, -lg, -xl and -xxl make an offcanvas behave like a normal, always-visible block from that breakpoint up, and like a sliding panel below it. That's perfect for a sidebar that becomes a drawer on phones. The Bootstrap Mini Project lesson uses this pattern for its dashboard sidebar.

html
<div class="offcanvas-lg offcanvas-start" tabindex="-1" id="sidebar">
  <!-- visible inline on ≥992px, a drawer below that -->
</div>

Tip: An offcanvas is a good fit for the mobile navbar too: put .offcanvas inside a .navbar instead of .collapse.navbar-collapse and the menu slides in from the side.

Test yourself

Which class makes an offcanvas slide in from the right?

  1. .offcanvas-right
  2. .offcanvas-end
  3. .offcanvas-start
  4. .offcanvas-side

How do you let the page scroll while an offcanvas is open?

  1. data-bs-scroll="true"
  2. data-bs-backdrop="scroll"
  3. .offcanvas-scroll
  4. overflow-auto on body

What does .offcanvas-lg do?

  1. Makes the panel large
  2. Shows the content inline from lg up and as an offcanvas below lg
  3. Only works on large screens
  4. Opens automatically on large screens