← Back to Bootstrap Course | Chapter 8: Components — Modals & Offcanvas | Lesson 4 of 8

Offcanvas

An offcanvas is a sidebar panel that slides in from the edge of the screen.

In this page:

  1. Offcanvas
Syntax
markup
<button class="btn btn-primary" data-bs-toggle="offcanvas" data-bs-target="#offcanvas-id">Open</button>

<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvas-id">
  <div class="offcanvas-header">Title</div>
  <div class="offcanvas-body">Content</div>
</div>

Offcanvas

Use .offcanvas with a placement class (.offcanvas-start, -end, -top or -bottom), a header and a body. Open it with data-bs-toggle="offcanvas" and data-bs-target. Backdrop and scrolling behaviour can be set with data-bs-backdrop and data-bs-scroll.

Note: Offcanvas is great for mobile navigation menus.

Example: Offcanvas

markup
<!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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="p-3">
<div class="container">
  <button class="btn btn-primary" data-bs-toggle="offcanvas" data-bs-target="#side">Open sidebar</button>
  <div class="offcanvas offcanvas-start" tabindex="-1" id="side" aria-labelledby="sideLabel">
    <div class="offcanvas-header"><h5 class="offcanvas-title" id="sideLabel">Menu</h5><button class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button></div>
    <div class="offcanvas-body"><ul class="list-unstyled"><li>Home</li><li>Reports</li><li>Settings</li></ul></div>
  </div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Missing placement class
  2. Forgetting tabindex and aria-labelledby
  3. No close button
Chapter Summary
  • offcanvas with a placement class
  • Header and body sections
  • data-bs-toggle="offcanvas" opens it
  • Backdrop and scroll options
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.