Bootstrap5 Offcanvas Navigation (Offcanvas)
The Bootstrap5 offcanvas is similar to a modal and is commonly used in mobile devices.
Creating Sliding Navigation
We can use JavaScript to set whether to add the .offcanvas class after the .show class, thereby controlling the display and hiding of the offcanvas:
.offcanvasHide Content (default).offcanvas.showDisplay content
You can use the a link's href attribute or the button element's data-bs-target attribute to set up the offcanvas. Both cases require using data-bs-toggle="offcanvas"。
Create a sliding navigation example as follows:
Example
<a class=">btn btn-primary
"
data-bs-toggle
=
"
offcanvas
"
href
=
"
#offcanvasExample
"
role
=
"
button
"
aria-controls
=
"
offcanvasExample
"
Using the link's href attribute
a
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasExample
"
aria-controls
=
"
offcanvasExample
"
Using data-bs-target in a button
button
<div class=">offcanvas offcanvas-start
"
tabindex
=
"
-1
"
id
=
"
offcanvasExample
"
aria-labelledby
=
"
offcanvasExampleLabel
"
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasExampleLabel" Sidebar h5>
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
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
<div class=">dropdown mt-3
"
<button class=">btn btn-secondary dropdown-toggle
"
type
=
"
button
"
id
=
"
dropdownMenuButton
"
data-bs-toggle
=
"
dropdown
"
Dropdown button
button
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<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
div
div
div
Try it »
Offcanvas Direction
The direction of the offcanvas can be controlled with the following four classes:
.offcanvas-startDisplayed on the left, as in the example above..offcanvas-endDisplayed on the right.offcanvas-topDisplayed on the top.offcanvas-bottomDisplayed on the bottom
Example
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasTop
"
aria-controls
=
"
offcanvasTop
"
Top Navbar
button
<div class=">offcanvas offcanvas-top
"
tabindex
=
"
-1
"
id
=
"
offcanvasTop
"
aria-labelledby
=
"
offcanvasTopLabel
"
<div class="offcanvas-header">
<h5 id="offcanvasTopLabel">Displayed in the top navbar
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class="offcanvas-body">...
div
div
Try it »
Example
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasRight
"
aria-controls
=
"
offcanvasRight
"
Right Offcanvas
button
<div class=">offcanvas offcanvas-end
"
tabindex
=
"
-1
"
id
=
"
offcanvasRight
"
aria-labelledby
=
"
offcanvasRightLabel
"
<div class="offcanvas-header">
<h5 id="offcanvasRightLabel">Displayed in the right offcanvas
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class="offcanvas-body">...
div
div
Try it »
Example
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasBottom
"
aria-controls
=
"
offcanvasBottom
"
Bottom Navbar
button
<div class=">offcanvas offcanvas-bottom
"
tabindex
=
"
-1
"
id
=
"
offcanvasBottom
"
aria-labelledby
=
"
offcanvasBottomLabel
"
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasBottomLabel">Displayed in the bottom navbar
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class=">offcanvas-body small
"
...
div
div
Try it »
Setting the Backdrop and Whether the Background is Scrollable
When popping out the offcanvas, we can set whether the element is scrollable, and also set whether to show a backdrop. Use data-bs-scroll Use the property to set whether the element is scrollable,data-bs-backdrop to toggle whether to show the backdrop.
Example
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasScrolling
"
aria-controls
=
"
offcanvasScrolling
"
The body element can scroll
button
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasWithBackdrop
"
aria-controls
=
"
offcanvasWithBackdrop
"
Show Backdrop (default)
button
<button class=">btn btn-primary
"
type
=
"
button
"
data-bs-toggle
=
"
offcanvas
"
data-bs-target
=
"
#offcanvasWithBothOptions
"
aria-controls
=
"
offcanvasWithBothOptions
"
Allow scrolling and show backdrop
button
<div class=">offcanvas offcanvas-start
"
data-bs-scroll
=
"
true
"
data-bs-backdrop
=
"
false
"
tabindex
=
"
-1
"
id
=
"
offcanvasScrolling
"
aria-labelledby
=
"
offcanvasScrollingLabel
"
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasScrollingLabel">The body content can scroll
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class="offcanvas-body">
<p>Scroll the page to see the effect.
p
div
div
<div class=">offcanvas offcanvas-start
"
tabindex
=
"
-1
"
id
=
"
offcanvasWithBackdrop
"
aria-labelledby
=
"
offcanvasWithBackdropLabel
"
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBackdropLabel">Using the Backdrop
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class="offcanvas-body">
<p>The body content cannot scroll
p
div
div
<div class=">offcanvas offcanvas-start
"
data-bs-scroll
=
"
true
"
tabindex
=
"
-1
"
id
=
"
offcanvasWithBothOptions
"
aria-labelledby
=
"
offcanvasWithBothOptionsLabel
"
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Using the backdrop, the body content can scroll
h5
<button type="button" class=">btn-close text-reset
"
data-bs-dismiss
=
"
offcanvas
"
aria-label
=
"
Close
"
button
div
<div class="offcanvas-body">
<p>Scroll the page to see the effect.
p
div
div
Try it »
Offcanvas Example
Example
Try it »