Bootstrap - Offcanvas: руковод novichkov

Hey there, future web developers! Today, we're going to dive into the wonderful world of Bootstrap's Offcanvas component. Don't worry if you're new to this - I'll be your friendly guide, and we'll explore this topic together step by step. So, grab your favorite beverage, get comfortable, and let's embark on this exciting journey!

Bootstrap - Offcanvas

Overview

First things first - what exactly is an Offcanvas? Well, imagine you're in a tiny apartment and need more storage space. Suddenly, a hidden compartment slides out from the wall - that's essentially what an Offcanvas is in web design! It's a sidebar that can slide in and out of view, providing extra space for navigation, forms, or any other content you want to tuck away until needed.

Offcanvas Components

Before we start building, let's break down the main parts of an Offcanvas:

  1. The trigger: This is usually a button that, when clicked, reveals the Offcanvas.
  2. The Offcanvas itself: The panel that slides into view.
  3. The backdrop: An optional overlay that dims the main content when the Offcanvas is open.

Now, let's see how these components come together in code!

Live Demo

Here's a simple example to get us started:

<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#demoOffcanvas">
Open Offcanvas
</button>

<div class="offcanvas offcanvas-start" tabindex="-1" id="demoOffcanvas">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Offcanvas Title</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. You can put anything you want here!</p>
</div>
</div>

Let's break this down:

  1. The button has data-bs-toggle="offcanvas" to tell Bootstrap it's an Offcanvas trigger.
  2. data-bs-target="#demoOffcanvas" links the button to the Offcanvas with the matching ID.
  3. The Offcanvas itself is a div with the class offcanvas.
  4. offcanvas-start positions it on the left side (we'll explore other positions later).
  5. The header contains the title and a close button.
  6. The body is where your main content goes.

Body Scrolling

By default, when an Offcanvas is open, the body behind it can't be scrolled. But what if you want to allow scrolling? Easy peasy! Just add data-bs-scroll="true" to your Offcanvas div:

<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="scrollableOffcanvas">
<!-- Offcanvas content -->
</div>

Body Scrolling and Backdrop

Want to allow scrolling but keep that fancy backdrop? No problem! Add data-bs-backdrop="false" along with data-bs-scroll="true":

<div class="offcanvas offcanvas-start" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="scrollableBackdropOffcanvas">
<!-- Offcanvas content -->
</div>

Static Backdrop

If you want the Offcanvas to stay open unless explicitly closed (i.e., clicking outside won't close it), use data-bs-backdrop="static":

<div class="offcanvas offcanvas-start" data-bs-backdrop="static" tabindex="-1" id="staticBackdropOffcanvas">
<!-- Offcanvas content -->
</div>

Dark Offcanvas

Feeling a bit goth? Let's make our Offcanvas dark! Just add the text-bg-dark class:

<div class="offcanvas offcanvas-start text-bg-dark" tabindex="-1" id="darkOffcanvas">
<!-- Offcanvas content -->
</div>

Responsive

Here's a cool trick - you can make your Offcanvas responsive! It can be an Offcanvas on smaller screens and inline content on larger ones. Use the .offcanvas-{breakpoint} classes:

<div class="offcanvas-lg offcanvas-start" tabindex="-1" id="responsiveOffcanvas">
<!-- Offcanvas content -->
</div>

This Offcanvas will be inline on screens larger than the 'lg' breakpoint and an Offcanvas on smaller screens.

Placement

Remember when I mentioned we could change the position? Here's how:

Class Position
offcanvas-start Left
offcanvas-end Right
offcanvas-top Top
offcanvas-bottom Bottom

For example, to place the Offcanvas on the right:

<div class="offcanvas offcanvas-end" tabindex="-1" id="rightOffcanvas">
<!-- Offcanvas content -->
</div>

Accessibility

Last but certainly not least, let's talk about making our Offcanvas accessible to all users:

  1. Use aria-labelledby to link the Offcanvas to its title:
<div class="offcanvas offcanvas-start" tabindex="-1" id="accessibleOffcanvas" aria-labelledby="offcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasLabel">Accessible Offcanvas</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<!-- Offcanvas content -->
</div>
  1. Use aria-controls on the trigger button:
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#accessibleOffcanvas" aria-controls="accessibleOffcanvas">
Open Accessible Offcanvas
</button>

And there you have it, folks! You're now an Offcanvas expert. Remember, practice makes perfect, so don't be afraid to experiment with these components in your projects. Who knows? You might just create the next big sliding sensation in web design!

Happy coding, and may your Offcanvas always slide smoothly!

Credits: Image by storyset