bootstrap
  1. bootstrap-offcanvas

Bootstrap Offcanvas

  • Bootstrap Offcanvas is a component that provides a flexible and dynamic way to display content outside the main content area of a website.
  • It is perfect for mobile devices and small screens, where the main content may not be easily accessible.

Syntax

To use Bootstrap Offcanvas in your HTML file, you need to add the following code:

<button class="btn btn-primary" type="button" data-toggle="offcanvas" data-target="#offcanvasExample" aria-controls="offcanvasExample" aria-expanded="false">
  Toggle offcanvas
</button>

<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasExampleLabel">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>

Example

Here is an example of how to create a simple Bootstrap Offcanvas.

<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasScrolling"
    aria-controls="offcanvasScrolling">Offcanvas</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">Offcanvas</h5>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <p>This is the offcanvas content.</p>
    </div>
</div>
Try Playground

Placement

There’s no default placement for offcanvas components, so you must add one of the modifier classes below.

  • .offcanvas-start places offcanvas on the left of the viewport (shown above)
  • .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

Example

Try the top, right, and bottom examples out below.

 <!-- top  -->
 <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop"
 aria-controls="offcanvasTop">Toggle top offcanvas</button>

<div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel">
 <div class="offcanvas-header">
     <h5 class="offcanvas-title" id="offcanvasTopLabel">Offcanvas top</h5>
     <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
 </div>
 <div class="offcanvas-body">
     This is the top offcanvas content.
 </div>
</div>
<!-- right -->
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight"
 aria-controls="offcanvasRight">Toggle right offcanvas</button>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
 <div class="offcanvas-header">
     <h5 class="offcanvas-title" id="offcanvasRightLabel">Offcanvas right</h5>
     <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
 </div>
 <div class="offcanvas-body">
     This is the right offcanvas content.
 </div>
</div>
<!-- bottom  -->

<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom"
 aria-controls="offcanvasBottom">Toggle bottom offcanvas</button>

<div class="offcanvas offcanvas-bottom" tabindex="-1" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel">
 <div class="offcanvas-header">
     <h5 class="offcanvas-title" id="offcanvasBottomLabel">Offcanvas bottom</h5>
     <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
 </div>
 <div class="offcanvas-body small">
     This is the bottom offcanvas content.
 </div>
</div>
Try Playground

Explanation

In the code above, we used the following classes to create the Offcanvas component:

  • btn btn-primary: this sets the style of the button that toggles the offcanvas.
  • data-toggle="offcanvas": this attribute tells Bootstrap to toggle the offcanvas when the button is clicked.
  • data-target="#offcanvasExample": this attribute specifies the offcanvas to toggle. The value should match the id of the offcanvas.
  • offcanvas offcanvas-start: this class indicates that this is an offcanvas and sets its position to the left side of the screen.
  • offcanvas-header: this is a container for the offcanvas header.
  • offcanvas-title: this sets the title of the offcanvas.
  • btn-close: this is a close button that appears in the offcanvas header.
  • offcanvas-body: this is a container for the offcanvas content.

Use

Bootstrap Offcanvas can be used to display menus, forms, or any other content outside the main content area of a website. It is especially useful for mobile devices and small screens, where the main content may not be easily accessible.

Important Points

  • Bootstrap Offcanvas is a flexible and dynamic way to display content outside the main content area of a website.
  • Using Bootstrap Offcanvas is easy and requires only a few lines of code.
  • Bootstrap Offcanvas can be used to display menus, forms, or any other content outside the main content area of a website.

Summary

Bootstrap Offcanvas is a useful component for displaying content outside the main content area of a website, especially on mobile devices and small screens. By using Bootstrap Offcanvas, you can make your website more accessible and user-friendly.

Published on: