Offcanvas is similar to modals except that is often used as a sidebar navigation menu.
Offcanvas is a sidebar component that can be toggled via JavaScript to appear from the left, right, or bottom edge of the viewport.
Offcanvas shares some of the same JavaScript code as modals. Conceptually, they are quite similar, but they are separate plugins.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example bd-example-offcanvas p-0 bg-light overflow-hidden"><divclass="offcanvas offcanvas-start show"tabindex="-1"id="offcanvas"aria-labelledby="offcanvasLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasLabel">Offcanvas</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"aria-label="Close"></button></div><divclass="offcanvas-body">
The word Markup means a language with particular syntax which.
</div></div></body></html>
Output
Bootstrap Offcanvas Live demo
.offcanvas Class hides content
.offcanvas.show Class shows content
You can use a link with the href attribute, or A button with the data-bs-target attribute. In both cases, the data-bs-toggle="offcanvas" is required.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example"><aclass="btn btn-primary"data-bs-toggle="offcanvas"href="#offcanvasExample"role="button"aria-controls="offcanvasExample">
Link with href
</a><buttonclass="btn btn-primary"type="button"data-bs-toggle="offcanvas"data-bs-target="#offcanvasExample"aria-controls="offcanvasExample">
Button with data-bs-target
</button><divclass="offcanvas offcanvas-start"tabindex="-1"id="offcanvasExample"aria-labelledby="offcanvasExampleLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasExampleLabel">Offcanvas</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"aria-label="Close"></button></div><divclass="offcanvas-body"><divclass="">
The word "Markup" means a language with particular syntax which.
</div><divclass="dropdown mt-3"><buttonclass="btn btn-secondary dropdown-toggle"type="button"data-bs-toggle="dropdown"aria-expanded="false">
Dropdown button
</button><ulclass="dropdown-menu"style=""><li><aclass="dropdown-item"href="#">KaashivInfotech</a></li><li><aclass="dropdown-item"href="#">Wikitechy</a></li><li><aclass="dropdown-item"href="#">Kaashiv</a></li></ul></div></div></div></body></html>
Output
Bootstrap Offcanvas Body Scrolling
Scrolling the <body> element is disabled when an offcanvas and its backdrop are visible. Use the data-bs-scroll attribute to toggle <body> scrolling and data-bs-backdrop to toggle the backdrop. Use the data-bs-scroll attribute to enable <body> scrolling.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example"><buttonclass="btn btn-primary"type="button"data-bs-toggle="offcanvas"data-bs-target="#offcanvasScrolling"aria-controls="offcanvasScrolling">Enable body scrolling</button><divclass="offcanvas offcanvas-start"data-bs-scroll="true"data-bs-backdrop="false"tabindex="-1"id="offcanvasScrolling"aria-labelledby="offcanvasScrollingLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasScrollingLabel">Offcanvas with body scrolling</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"aria-label="Close"></button></div><divclass="offcanvas-body"><p>HTML is a typical markup language used for building a web pages.</p></div></div></body></html>
Output
Bootstrap Offcanvas Body scrolling and backdrop
You can also enable <body> scrolling with a visible backdrop.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example"><buttonclass="btn btn-primary"type="button"data-bs-toggle="offcanvas"data-bs-target="#offcanvasWithBothOptions"aria-controls="offcanvasWithBothOptions">Enable both scrolling & backdrop</button><divclass="offcanvas offcanvas-start"data-bs-scroll="true"tabindex="-1"id="offcanvasWithBothOptions"aria-labelledby="offcanvasWithBothOptionsLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasWithBothOptionsLabel">Backdrop with scrolling</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"aria-label="Close"></button></div><divclass="offcanvas-body"><p>HTML is a typical markup language used for building a web pages.</p></div></div></body></html>
Output
Bootstrap Offcanvas Static Backdrop
When backdrop is set to static, the offcanvas will not close when clicking outside of it.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example"><buttonclass="btn btn-primary"type="button"data-bs-toggle="offcanvas"data-bs-target="#staticBackdrop"aria-controls="staticBackdrop">
Toggle static offcanvas
</button><divclass="offcanvas offcanvas-start"data-bs-backdrop="static"tabindex="-1"id="staticBackdrop"aria-labelledby="staticBackdropLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="staticBackdropLabel">Offcanvas</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"aria-label="Close"></button></div><divclass="offcanvas-body"><div>
HTML is a typical markup language used for building a web pages..
</div></div></div></body></html>
Output
Bootstrap Dark offcanvas
Here we add .text-bg-dark to the .offcanvas and .btn-close-white to .btn-close for proper styling with a dark offcanvas.
If you have dropdowns within, consider also adding .dropdown-menu-dark to .dropdown-menu.
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example bd-example-offcanvas p-0 bg-light overflow-hidden"><divclass="offcanvas offcanvas-start show text-bg-dark"tabindex="-1"id="offcanvasDark"aria-labelledby="offcanvasDarkLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasDarkLabel">Offcanvas</h5><buttontype="button"class="btn-close btn-close-white"data-bs-dismiss="offcanvasDark"aria-label="Close"></button></div><divclass="offcanvas-body"><p>HTML - Hyper Text Markup Language.</p></div></div></body></html>
Output
Bootstrap Offcanvas Responsive
Responsive offcanvas classes hide content outside the viewport from a specified breakpoint and down.
Above that breakpoint, the contents within will behave as usual.For example, .offcanvas-lg hides content in an offcanvas below the lg breakpoint, but shows the content above the lg breakpoint.
Responsive offcanvas classes are available across for each breakpoint.
.offcanvas
.offcanvas-sm
.offcanvas-md
.offcanvas-lg
.offcanvas-xl
.offcanvas-xxl
Sample Code
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"rel="stylesheet"><linkhref="https://getbootstrap.com/docs/5.2/assets/css/docs.css"rel="stylesheet"><title>Bootstrap Example</title><scriptsrc="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script></head><bodyclass="p-3 m-0 border-0 bd-example"><buttonclass="btn btn-primary d-lg-none"type="button"data-bs-toggle="offcanvas"data-bs-target="#offcanvasResponsive"aria-controls="offcanvasResponsive">Toggle offcanvas</button><divclass="alert alert-info d-none d-lg-block">Resize your browser to show the responsive offcanvas toggle.</div><divclass="offcanvas-lg offcanvas-end"tabindex="-1"id="offcanvasResponsive"aria-labelledby="offcanvasResponsiveLabel"><divclass="offcanvas-header"><h5class="offcanvas-title"id="offcanvasResponsiveLabel">Responsive offcanvas</h5><buttontype="button"class="btn-close"data-bs-dismiss="offcanvas"data-bs-target="#offcanvasResponsive"aria-label="Close"></button></div><divclass="offcanvas-body"><pclass="mb-0">This is content within an <code>.offcanvas-lg</code>.</p></div></div></body></html>
Output
Bootstrap Offcanvas Placement
.offcanvas-start places offcanvas on the left of the viewport.
.offcanvas-end places offcanvas on the right of the viewport.
.offcanvas-top places offcanvas on the top of the viewport.
.offcanvas-bottom places offcanvas on the bottom of the viewport.