Lesson 39 of 60 – Bootstrap Offcanvas
65%

Bootstrap Offcanvas

Bootstrap Offcanvas is a component that allows you to display hidden content from the side of the screen. It is commonly used for mobile navigation menus, sidebars, filters, settings panels, and other expandable side content.

Note: Bootstrap Offcanvas uses JavaScript to show and hide content. It can be opened using buttons or links and can appear from the start, end, top, or bottom of the viewport.

1. What Is Offcanvas?

Offcanvas is a Bootstrap component that displays content outside the visible area of the page and slides it into view when activated.

It is useful for:

  • Mobile navigation menus.
  • Website sidebars.
  • Filter panels.
  • Settings panels.
  • User account menus.
  • Additional page content.

2. Basic Offcanvas Structure

A basic Offcanvas contains a trigger button and an Offcanvas element.

<button
class="btn btn-primary"
data-bs-toggle="offcanvas"
data-bs-target="#myOffcanvas">

    Open Menu

</button>


<div
class="offcanvas offcanvas-start"
id="myOffcanvas">

    Content

</div>

3. The .offcanvas Class

The .offcanvas class creates the main Offcanvas component.

<div
class="offcanvas"
id="menu">

    Offcanvas Content

</div>

The Offcanvas is normally combined with a placement class such as .offcanvas-start.

4. Offcanvas from the Start

The .offcanvas-start class makes the Offcanvas appear from the left side in a left-to-right layout.

<div
class="offcanvas offcanvas-start"
id="startMenu">

    <div class="offcanvas-body">

        Left Side Menu

    </div>

</div>

5. Offcanvas from the End

The .offcanvas-end class displays the Offcanvas from the right side in a left-to-right layout.

<div
class="offcanvas offcanvas-end"
id="endMenu">

    <div class="offcanvas-body">

        Right Side Menu

    </div>

</div>

6. Offcanvas from the Top

The .offcanvas-top class makes the Offcanvas slide down from the top.

<div
class="offcanvas offcanvas-top"
id="topMenu">

    <div class="offcanvas-body">

        Top Content

    </div>

</div>

7. Offcanvas from the Bottom

The .offcanvas-bottom class makes the Offcanvas slide up from the bottom.

<div
class="offcanvas offcanvas-bottom"
id="bottomMenu">

    <div class="offcanvas-body">

        Bottom Content

    </div>

</div>

8. Offcanvas Header

The .offcanvas-header class is used to create the header area of an Offcanvas.

<div
class="offcanvas offcanvas-start"
id="menu">

    <div class="offcanvas-header">

        <h5 class="offcanvas-title">

            My Menu

        </h5>

    </div>

</div>

9. The .offcanvas-title Class

The .offcanvas-title class is commonly used for the title inside the Offcanvas header.

<div class="offcanvas-header">

    <h5 class="offcanvas-title">

        Navigation

    </h5>

</div>

10. The .offcanvas-body Class

The .offcanvas-body class contains the main content of the Offcanvas.

<div class="offcanvas-body">

    <p>
    Welcome to our website.
    </p>

    <a href="#">
    Home
    </a>

</div>

11. Close Button

The .btn-close class can be used to create a close button.

<button
type="button"
class="btn-close"
data-bs-dismiss="offcanvas"
aria-label="Close">

</button>

The data-bs-dismiss="offcanvas" attribute tells Bootstrap to close the current Offcanvas.

12. Opening Offcanvas with a Button

A button can open an Offcanvas using data-bs-toggle="offcanvas".

<button
class="btn btn-primary"
data-bs-toggle="offcanvas"
data-bs-target="#myMenu"
type="button">

    Open Menu

</button>


<div
class="offcanvas offcanvas-start"
id="myMenu">

    <div class="offcanvas-body">

        Menu Content

    </div>

</div>

13. Opening Offcanvas with a Link

A link can also be used as an Offcanvas trigger.

<a
href="#myPanel"
class="btn btn-success"
data-bs-toggle="offcanvas"
role="button"
aria-controls="myPanel">

    Open Panel

</a>


<div
class="offcanvas offcanvas-end"
id="myPanel">

    <div class="offcanvas-body">

        Panel Content

    </div>

</div>

14. aria-controls

The aria-controls attribute identifies the Offcanvas element controlled by the trigger.

<button
data-bs-toggle="offcanvas"
data-bs-target="#menu"
aria-controls="menu">

    Open Menu

</button>

The value should match the ID of the Offcanvas element.

15. aria-labelledby

The aria-labelledby attribute can connect the Offcanvas to its visible title.

<div
class="offcanvas offcanvas-start"
id="menu"
aria-labelledby="menuTitle">

    <div class="offcanvas-header">

        <h5
        class="offcanvas-title"
        id="menuTitle">

            Main Menu

        </h5>

    </div>

</div>

This relationship provides useful information to assistive technologies.

16. Backdrop

When an Offcanvas is open, Bootstrap normally displays a backdrop behind it.

The backdrop visually separates the Offcanvas from the main page and helps indicate that the Offcanvas is currently active.

<div
class="offcanvas offcanvas-start"
id="menu"
data-bs-backdrop="true">

    Menu Content

</div>

17. Disabling the Backdrop

The backdrop can be disabled using data-bs-backdrop="false".

<div
class="offcanvas offcanvas-start"
id="menu"
data-bs-backdrop="false">

    Content

</div>

This allows the page behind the Offcanvas to remain visually unchanged.

18. Static Backdrop

A static backdrop can be configured so that clicking the backdrop does not close the Offcanvas.

<div
class="offcanvas offcanvas-start"
id="menu"
data-bs-backdrop="static">

    Important Content

</div>

The exact behavior should be chosen according to the purpose of the panel and the user experience required.

19. Scrollable Offcanvas

The data-bs-scroll option can allow the main page to remain scrollable while the Offcanvas is open.

<div
class="offcanvas offcanvas-start"
id="menu"
data-bs-scroll="true">

    Long Content

</div>

This can be useful when the Offcanvas contains navigation or controls while users still need to interact with the page behind it.

20. Scroll and Backdrop Together

Bootstrap also allows scroll behavior and the backdrop to be configured together.

<div
class="offcanvas offcanvas-start"
id="menu"
data-bs-scroll="true"
data-bs-backdrop="false">

    Menu Content

</div>

This combination can be useful for specific application layouts.

21. Offcanvas Navigation Menu

One of the most common uses of Offcanvas is a navigation menu.

<div
class="offcanvas offcanvas-start"
id="navMenu">

    <div class="offcanvas-header">

        <h5 class="offcanvas-title">

            Navigation

        </h5>

        <button
        type="button"
        class="btn-close"
        data-bs-dismiss="offcanvas">

        </button>

    </div>


    <div class="offcanvas-body">

        <ul class="list-group">

            <li class="list-group-item">
                Home
            </li>

            <li class="list-group-item">
                Courses
            </li>

            <li class="list-group-item">
                Contact
            </li>

        </ul>

    </div>

</div>

22. Offcanvas Filter Panel

Offcanvas is useful for displaying filters on shopping, library, or search pages.

<div
class="offcanvas offcanvas-end"
id="filterPanel">

    <div class="offcanvas-header">

        <h5 class="offcanvas-title">

            Filters

        </h5>

    </div>


    <div class="offcanvas-body">

        <label class="form-label">
        Category
        </label>

        <select class="form-select">

            <option>
            All
            </option>

            <option>
            Courses
            </option>

            <option>
            Books
            </option>

        </select>

    </div>

</div>

23. Offcanvas User Menu

An Offcanvas can also be used as a user account panel.

My Account

Welcome to your account.

Profile Settings Logout

24. Offcanvas Using JavaScript

Offcanvas can also be controlled through the Bootstrap JavaScript API.

const element =
document.getElementById(
'myOffcanvas'
);

const offcanvas =
new bootstrap.Offcanvas(
element
);

offcanvas.show();

The show() method opens the Offcanvas.

25. Offcanvas JavaScript Methods

Bootstrap Offcanvas provides several JavaScript methods.

  • show() – opens the Offcanvas.
  • hide() – closes the Offcanvas.
  • toggle() – switches between open and closed states.
const offcanvas =
new bootstrap.Offcanvas(
element
);

offcanvas.show();

offcanvas.hide();

offcanvas.toggle();

26. Offcanvas Events

Bootstrap Offcanvas provides events that can be used to execute JavaScript when its state changes.

  • show.bs.offcanvas
  • shown.bs.offcanvas
  • hide.bs.offcanvas
  • hidden.bs.offcanvas
const menu =
document.getElementById(
'myOffcanvas'
);

menu.addEventListener(
'shown.bs.offcanvas',
function(){

    console.log(
    'Offcanvas opened'
    );

});

27. Offcanvas Accessibility

Accessibility is important when using Offcanvas because it changes the visible interface and can move focus.

  • Use a meaningful title.
  • Use aria-labelledby when appropriate.
  • Provide a clear close button.
  • Use meaningful text for navigation links.
  • Make sure keyboard users can operate the component.
  • Do not make important content inaccessible without a clear control.
<div
class="offcanvas offcanvas-start"
id="accessibleMenu"
aria-labelledby="menuTitle">

    <div class="offcanvas-header">

        <h5
        class="offcanvas-title"
        id="menuTitle">

            Main Menu

        </h5>

    </div>

</div>

28. Common Offcanvas Mistakes

Avoid these common mistakes:

  • Forgetting data-bs-toggle="offcanvas".
  • Using the wrong data-bs-target.
  • Using duplicate IDs.
  • Forgetting the Bootstrap JavaScript bundle.
  • Forgetting a close button.
  • Using the wrong Offcanvas placement class.
  • Adding too much content without considering usability.
  • Using unclear navigation labels.
  • Ignoring keyboard and accessibility requirements.

29. Practical Offcanvas Sidebar

The following example creates a practical sidebar with navigation links.

30. Complete Bootstrap Offcanvas Example

This complete example combines a trigger button, Offcanvas header, navigation links, close button, and accessibility attributes.

<button
class="btn btn-primary"
type="button"
data-bs-toggle="offcanvas"
data-bs-target="#completeMenu"
aria-controls="completeMenu">

    Open Menu

</button>


<div
class="offcanvas offcanvas-start"
id="completeMenu"
aria-labelledby="completeMenuTitle">


    <div class="offcanvas-header">

        <h5
        class="offcanvas-title"
        id="completeMenuTitle">

            Website Menu

        </h5>


        <button
        type="button"
        class="btn-close"
        data-bs-dismiss="offcanvas"
        aria-label="Close">

        </button>

    </div>


    <div class="offcanvas-body">

        <ul class="list-group">

            <li class="list-group-item">
                <a href="#">Home</a>
            </li>

            <li class="list-group-item">
                <a href="#">Courses</a>
            </li>

            <li class="list-group-item">
                <a href="#">Tutorials</a>
            </li>

            <li class="list-group-item">
                <a href="#">Contact</a>
            </li>

        </ul>

    </div>

</div>

📌 Key Points

  • .offcanvas creates the main Offcanvas component.
  • .offcanvas-start displays it from the start side.
  • .offcanvas-end displays it from the end side.
  • .offcanvas-top displays it from the top.
  • .offcanvas-bottom displays it from the bottom.
  • .offcanvas-header creates the header.
  • .offcanvas-title styles the title.
  • .offcanvas-body contains the main content.
  • data-bs-toggle="offcanvas" activates the component.
  • data-bs-target identifies the Offcanvas to open.
  • data-bs-dismiss="offcanvas" closes the Offcanvas.
  • aria-controls identifies the controlled component.
  • aria-labelledby can identify the Offcanvas title.
  • Backdrop and scrolling behavior can be configured.
  • Offcanvas can be controlled using the JavaScript API.
  • Important JavaScript methods include show(), hide(), and toggle().
  • Offcanvas is commonly used for navigation menus and side panels.
  • Accessibility should always be considered when creating an Offcanvas interface.

🧠 Quick Quiz

Question: Which Bootstrap class is used to create the main Offcanvas component?