Skip to main content

Components

Offcanvas

Sidebars that slide in from any edge — a native popover opened by a button, with a :target fallback.

How it works

The offcanvas is a <div class="offcanvas offcanvas-start" popover>. A button with popovertarget pointing at its id opens it in the top layer with a backdrop, light-dismiss and Escape. Close buttons use popovertargetaction="hide". Page scrolling is locked while it is open.

  • Baseline: Popover API (Chrome 114, Safari 17, Firefox 125).
  • Enhancement: the slide transition. @starting-style starts the panel off-screen and transition-behavior: allow-discrete lets it slide out again on close. Engines without those show and hide instantly.
  • Fallback: a plain <div class="offcanvas" id="…"> opened by <a href="#…"> via :target. Works everywhere; the backdrop is a box-shadow and there is no light-dismiss.
Only one offcanvas can be open at a time — opening a second popover closes the first. That is the Popover API's rule, and it matches Bootstrap's behaviour.

Live demo

Offcanvas

Some text as placeholder. In real life you can have the elements you have chosen. Like text, images, lists, etc.
HTML
<button class="btn btn-primary" type="button" popovertarget="offcanvas-demo">Open offcanvas</button>

<div class="offcanvas offcanvas-start" popover id="offcanvas-demo" aria-labelledby="offcanvas-demo-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-demo-label">Offcanvas</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-demo" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      Some text as placeholder. In real life you can have the elements you have chosen. Like text, images,
      lists, etc.
    </div>
    <details class="dropdown mt-4">
      <summary class="btn btn-secondary dropdown-toggle">Dropdown button</summary>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#!">Action</a></li>
        <li><a class="dropdown-item" href="#!">Another action</a></li>
        <li><a class="dropdown-item" href="#!">Something else here</a></li>
      </ul>
    </details>
  </div>
</div>

Placement

There is no default placement; pick one. .offcanvas-start (left), .offcanvas-end (right), .offcanvas-top and .offcanvas-bottom. Side panels are 400px wide; top and bottom panels are 30vh tall.

Offcanvas end

Slides in from the right.

Offcanvas top

Slides down from the top.

Offcanvas bottom

Slides up from the bottom.
HTML
<button class="btn btn-primary" type="button" popovertarget="offcanvas-end">Toggle end offcanvas</button>
<button class="btn btn-primary" type="button" popovertarget="offcanvas-top">Toggle top offcanvas</button>
<button class="btn btn-primary" type="button" popovertarget="offcanvas-bottom">Toggle bottom offcanvas</button>

<div class="offcanvas offcanvas-end" popover id="offcanvas-end" aria-labelledby="offcanvas-end-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-end-label">Offcanvas end</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-end" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    Slides in from the right.
  </div>
</div>

<div class="offcanvas offcanvas-top" popover id="offcanvas-top" aria-labelledby="offcanvas-top-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-top-label">Offcanvas top</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-top" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    Slides down from the top.
  </div>
</div>

<div class="offcanvas offcanvas-bottom" popover id="offcanvas-bottom" aria-labelledby="offcanvas-bottom-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-bottom-label">Offcanvas bottom</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-bottom" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body small">
    Slides up from the bottom.
  </div>
</div>

Body scrolling

While an offcanvas is open the page cannot scroll: body:has(.offcanvas:popover-open) sets overflow: hidden. The backdrop is always shown. Bootstrap's data-bs-scroll and data-bs-backdrop="false" options have no equivalent here — a popover always has a ::backdrop, and we always dim it. Content inside the panel scrolls on its own when it is taller than the viewport.

Full card

The panel body scrolls; the page behind does not.

Match 1 · Match 2 · Match 3 · Match 4 · Match 5 · Match 6 · Match 7 · Match 8.

Match 9 · Match 10 · Match 11 · Match 12 · Match 13 · Match 14 · Match 15.

Match 16 · Match 17 · Match 18 · Match 19 · Match 20 · Match 21 · Match 22.

Match 23 · Match 24 · Match 25 · Match 26 · Match 27 · Match 28 · Match 29.

Match 30 · Match 31 · Match 32 · Match 33 · Match 34 · Match 35 · Match 36.

Match 37 · Match 38 · Match 39 · Match 40 · Match 41 · Match 42 · Match 43.

Match 44 · Match 45 · Match 46 · Match 47 · Match 48 · Match 49 · Match 50.

Match 51 · Match 52 · Match 53 · Match 54 · Match 55 · Match 56 · Match 57.

Match 58 · Match 59 · Match 60 · Match 61 · Match 62 · Match 63 · Match 64.

Match 65 · Match 66 · Match 67 · Match 68 · Match 69 · Match 70 · Match 71.

HTML
<button class="btn btn-primary" type="button" popovertarget="offcanvas-scroll">Long content</button>

<div class="offcanvas offcanvas-start" popover id="offcanvas-scroll" aria-labelledby="offcanvas-scroll-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-scroll-label">Full card</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-scroll" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>The panel body scrolls; the page behind does not.</p>
    <p>Match 1 · Match 2 · Match 3 · Match 4 · Match 5 · Match 6 · Match 7 · Match 8.</p>
    <p>Match 9 · Match 10 · Match 11 · Match 12 · Match 13 · Match 14 · Match 15.</p>
    <p>Match 16 · Match 17 · Match 18 · Match 19 · Match 20 · Match 21 · Match 22.</p>
    <p>Match 23 · Match 24 · Match 25 · Match 26 · Match 27 · Match 28 · Match 29.</p>
    <p>Match 30 · Match 31 · Match 32 · Match 33 · Match 34 · Match 35 · Match 36.</p>
    <p>Match 37 · Match 38 · Match 39 · Match 40 · Match 41 · Match 42 · Match 43.</p>
    <p>Match 44 · Match 45 · Match 46 · Match 47 · Match 48 · Match 49 · Match 50.</p>
    <p>Match 51 · Match 52 · Match 53 · Match 54 · Match 55 · Match 56 · Match 57.</p>
    <p>Match 58 · Match 59 · Match 60 · Match 61 · Match 62 · Match 63 · Match 64.</p>
    <p>Match 65 · Match 66 · Match 67 · Match 68 · Match 69 · Match 70 · Match 71.</p>
  </div>
</div>

Static backdrop

popover="manual" turns off light-dismiss and Escape. The panel closes only through a button with popovertargetaction="hide".

Offcanvas

I will not close if you click outside of me.
HTML
<button class="btn btn-primary" type="button" popovertarget="offcanvas-static">Toggle static offcanvas</button>

<div class="offcanvas offcanvas-start" popover="manual" id="offcanvas-static" aria-labelledby="offcanvas-static-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-static-label">Offcanvas</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-static" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      I will not close if you click outside of me.
    </div>
  </div>
</div>

Dark offcanvas

Add data-bs-theme="dark" to the .offcanvas. Every colour inside follows, including the close button, which inverts automatically in Bootstrap 5.3.

Offcanvas

Place offcanvas content here.

HTML
<button class="btn btn-primary" type="button" popovertarget="offcanvas-dark">Toggle dark offcanvas</button>

<div class="offcanvas offcanvas-start" popover id="offcanvas-dark" aria-labelledby="offcanvas-dark-label" data-bs-theme="dark">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-dark-label">Offcanvas</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-dark" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p>Place offcanvas content here.</p>
  </div>
</div>

Responsive

.offcanvas-{sm|md|lg|xl|xxl} replaces .offcanvas. Below the breakpoint it is a drawer; from the breakpoint up it renders inline where it sits, with the header hidden and no backdrop. Hide the toggle button at the same breakpoint with .d-lg-none.

Resize your browser below 992px to see the toggle button; above it the content is inline.

Responsive offcanvas

This is content within an .offcanvas-lg.

HTML
<button class="btn btn-primary d-lg-none" type="button" popovertarget="offcanvas-responsive">Toggle offcanvas</button>

<div class="alert alert-info d-none d-lg-block">Resize your browser below 992px to see the toggle button; above it the content is inline.</div>

<div class="offcanvas-lg offcanvas-end" popover id="offcanvas-responsive" aria-labelledby="offcanvas-responsive-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-responsive-label">Responsive offcanvas</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-responsive" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <p class="mb-0">This is content within an <code>.offcanvas-lg</code>.</p>
  </div>
</div>

Available at each breakpoint: .offcanvas-sm (576px), .offcanvas-md (768px), .offcanvas-lg (992px), .offcanvas-xl (1200px), .offcanvas-xxl (1400px).

:target fallback

For environments that cannot rely on the Popover API. Omit the popover attribute and open the panel with a link to its id. Close with href="#!". The dimmer is a large box-shadow and clicking it does nothing; the page scroll is still locked.

Open :target offcanvas

Fragment-driven

Opened by the URL fragment. Universal, but no light-dismiss and no Escape.

Close
HTML
<a class="btn btn-outline-primary" href="#offcanvas-target" role="button">Open :target offcanvas</a>

<div class="offcanvas offcanvas-start" id="offcanvas-target" aria-labelledby="offcanvas-target-label">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-target-label">Fragment-driven</h2>
    <a class="btn-close" href="#!" aria-label="Close"></a>
  </div>
  <div class="offcanvas-body">
    <p>Opened by the URL fragment. Universal, but no light-dismiss and no Escape.</p>
    <a class="btn btn-primary" href="#!">Close</a>
  </div>
</div>

Accessibility

  • Add aria-labelledby pointing at the title. For a navigation drawer, <aside> or <nav> are good element choices.
  • An autofocus element inside moves keyboard focus when the panel opens; otherwise focus stays on the button and Tab moves into the panel, which is in the top layer.
  • Escape closes an auto popover and returns focus to the button. For popover="manual", keep the close button visible.
  • A popover does not make the rest of the page inert. If the drawer demands a decision, use a modal with command="show-modal".
  • The slide transition respects prefers-reduced-motion.

CSS

All of Bootstrap's --bs-offcanvas-* variables apply:

.offcanvas, .offcanvas-sm, .offcanvas-md, .offcanvas-lg, .offcanvas-xl, .offcanvas-xxl {
  --bs-offcanvas-zindex: 195;
  --bs-offcanvas-width: 400px;
  --bs-offcanvas-height: 30vh;
  --bs-offcanvas-padding-x: 1rem;
  --bs-offcanvas-padding-y: 1rem;
  --bs-offcanvas-color: var(--bs-body-color);
  --bs-offcanvas-bg: var(--bs-body-bg);
  --bs-offcanvas-border-width: var(--bs-border-width);
  --bs-offcanvas-border-color: var(--bs-border-color-translucent);
  --bs-offcanvas-box-shadow: var(--bs-box-shadow-sm);
  --bs-offcanvas-transition: transform 0.3s ease-in-out;
  --bs-offcanvas-title-line-height: 1.5;
}

A narrower panel, inline:

260px

Set --bs-offcanvas-width inline.
HTML
<button class="btn btn-outline-primary" type="button" popovertarget="offcanvas-narrow">Narrow offcanvas</button>
<div class="offcanvas offcanvas-end" popover id="offcanvas-narrow" aria-labelledby="offcanvas-narrow-label" style="--bs-offcanvas-width: 260px;">
  <div class="offcanvas-header">
    <h2 class="offcanvas-title h5" id="offcanvas-narrow-label">260px</h2>
    <button type="button" class="btn-close" popovertarget="offcanvas-narrow" popovertargetaction="hide" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">Set <code>--bs-offcanvas-width</code> inline.</div>
</div>

Behavioural selectors added by the theme:

.offcanvas[popover]              // top-layer reset: no UA inset, margin, padding or border
.offcanvas:popover-open          // shown: display flex, visible, transform none
.offcanvas:target                // shown by URL fragment; backdrop via box-shadow
.offcanvas::backdrop             // 50 % black dimmer
@starting-style { .offcanvas-start:popover-open { transform: translateX(-100%) } }   // and end/top/bottom
body:has(.offcanvas:popover-open),
body:has(.offcanvas:target)      // overflow: hidden — scroll lock
.offcanvas-lg[popover]           // ≥ lg: display block, inline