Bootstrap Offcanvas: The Hidden Drawer That Slides Into View πͺ
The Magic Sliding Door Analogy
Imagine your house has a secret room behind a sliding door. When you push a button, the door slides open from the side, top, or bottom β revealing everything inside. When youβre done, you slide it back and it hides away again!
Thatβs exactly what Bootstrap Offcanvas does for websites. Itβs a hidden panel that slides into view when needed, then slides away when youβre done.
What is Offcanvas?
Think of Offcanvas like a drawer in your desk:
- π¦ The drawer is hidden until you pull it
- ποΈ You pull the drawer to see whatβs inside
- β You push it back when youβre finished
- π― It saves space by staying hidden when not needed
Real Life Examples:
- π± The menu that slides in from the side on mobile apps
- π Shopping cart that appears from the right on websites
- βοΈ Settings panel that slides down from the top
Offcanvas Components
Every Offcanvas has three parts β like a gift box!
1. The Container (The Box)
<div class="offcanvas offcanvas-start"
id="myDrawer">
<!-- Everything goes inside -->
</div>
2. The Header (The Lid with a Label)
<div class="offcanvas-header">
<h5 class="offcanvas-title">
Menu
</h5>
<button type="button"
class="btn-close"
data-bs-dismiss="offcanvas">
</button>
</div>
3. The Body (The Stuff Inside)
<div class="offcanvas-body">
<p>Your content here!</p>
<a href="#">Link 1</a>
<a href="#">Link 2</a>
</div>
4. The Trigger Button
<button data-bs-toggle="offcanvas"
data-bs-target="#myDrawer">
Open Drawer
</button>
Complete Example:
<!-- Button to open -->
<button class="btn btn-primary"
data-bs-toggle="offcanvas"
data-bs-target="#myDrawer">
Open Menu
</button>
<!-- The Offcanvas -->
<div class="offcanvas offcanvas-start"
id="myDrawer">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Menu</h5>
<button class="btn-close"
data-bs-dismiss="offcanvas">
</button>
</div>
<div class="offcanvas-body">
<p>Hello from the drawer!</p>
</div>
</div>
Offcanvas Placement
The drawer can slide in from four directions β like doors on all sides of a room!
graph TD A["π± Your Screen"] --> B["β¬ οΈ START<br>Left Side"] A --> C["β‘οΈ END<br>Right Side"] A --> D["β¬οΈ TOP<br>From Above"] A --> E["β¬οΈ BOTTOM<br>From Below"]
Left Side (Default)
<div class="offcanvas offcanvas-start">
<!-- Slides from LEFT -->
</div>
π Use For: Main navigation menus
Right Side
<div class="offcanvas offcanvas-end">
<!-- Slides from RIGHT -->
</div>
π Use For: Shopping carts, notifications
From Top
<div class="offcanvas offcanvas-top">
<!-- Slides from TOP -->
</div>
π Use For: Alerts, search bars
From Bottom
<div class="offcanvas offcanvas-bottom">
<!-- Slides from BOTTOM -->
</div>
π Use For: Action sheets, options menu
Offcanvas Backdrop
The backdrop is like a curtain behind the drawer. It dims the rest of the page so you focus on the drawer!
graph TD A["Backdrop Options"] --> B["π«οΈ Default<br>Dark overlay<br>Click closes"] A --> C["π« No Backdrop<br>No overlay<br>See everything"] A --> D[π Static<br>Dark overlay<br>Click won't close]
Default Backdrop
<div class="offcanvas offcanvas-start"
id="drawer1">
<!-- Has dark overlay -->
<!-- Clicking backdrop closes it -->
</div>
No Backdrop (Scroll Allowed)
<div class="offcanvas offcanvas-start"
data-bs-backdrop="false"
data-bs-scroll="true"
id="drawer2">
<!-- No dark overlay -->
<!-- Can scroll page behind it -->
</div>
Static Backdrop (Canβt Click to Close)
<div class="offcanvas offcanvas-start"
data-bs-backdrop="static"
id="drawer3">
<!-- Has dark overlay -->
<!-- Must click X button to close -->
</div>
Offcanvas Responsive
Sometimes you want the drawer only on small screens β like phones! On big screens, you want the content to show normally.
Think of it like a transformer toy π€:
- π± On phone β Hidden drawer that slides
- π» On computer β Normal visible content
How It Works
<div class="offcanvas-lg offcanvas-start"
id="responsiveDrawer">
<!-- Hidden drawer on screens < 992px -->
<!-- Normal visible content on >= 992px -->
</div>
Breakpoint Classes
| Class | Hidden Until | Shows Normally At |
|---|---|---|
offcanvas |
Always hidden | Never |
offcanvas-sm |
< 576px | β₯ 576px |
offcanvas-md |
< 768px | β₯ 768px |
offcanvas-lg |
< 992px | β₯ 992px |
offcanvas-xl |
< 1200px | β₯ 1200px |
offcanvas-xxl |
< 1400px | β₯ 1400px |
Complete Responsive Example
<!-- Only shows button on small screens -->
<button class="btn btn-primary d-lg-none"
data-bs-toggle="offcanvas"
data-bs-target="#sidebar">
β° Menu
</button>
<!-- Responsive offcanvas -->
<div class="offcanvas-lg offcanvas-start"
id="sidebar">
<div class="offcanvas-header">
<h5>Navigation</h5>
<button class="btn-close d-lg-none"
data-bs-dismiss="offcanvas">
</button>
</div>
<div class="offcanvas-body">
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</div>
</div>
π± On Phone: Shows hamburger button, menu slides from left π» On Desktop: Menu is always visible, no button needed
Quick Summary
| Feature | What It Does |
|---|---|
| Offcanvas | A hidden sliding panel |
| Components | Header, Body, Close button |
| Placement | Start, End, Top, Bottom |
| Backdrop | Dark overlay behind panel |
| Responsive | Hide/show based on screen size |
You Did It! π
Now you know how to:
- β Create a sliding drawer with Offcanvas
- β Add a header, body, and close button
- β Choose which direction it slides from
- β Control the backdrop behavior
- β Make it responsive for different screens
Offcanvas is your new best friend for saving space and creating smooth, mobile-friendly menus!
