Bootstrap Off-canvas Menu and Sidebar with or without Overlay

Previously we have created Bootstrap off-canvas menu and sidebar. Further, the code was quite handy for WordPress websites since it’s not easy to use data-* API or change in files for a WordPress site with Bootstrap theme.

However, Bootstrap offers better accessibility and support for screen readers and similar assistive technologies. Also, if you’re developing sites in native languages like PHP or similar, you might wish to take advantage of such Bootstrap features.

Bootstrap Off-canvas Menu and Sidebar

Keeping the fact in the mind, we’ve explained how to create off-canvas menu and sidebar in more Bootstrap style here. In this article, we’ll utilize Bootstrap classes, jQuery data-* API as well as assistive technologies to create the sidebar.

Check all the demos using the given above. We have provided the feature to push the content as well when the sidebar opens.

HTML Markup for Bootstrap Off-canvas Menu and Sidebar

We need an anchor or button that will trigger the sidebar pull event. Additionally, an optional content overlay and the off-canvas menu itself. So if you wish to add a content overlay then put the following markup direct below the opening body tag.

We also need a button or anchor to show the off-canvas.

If you have multiple elements to open the same sidebar then keep data-target/href elements for these same. The same sidebar targeted by a class name in a button and by id in an anchor won’t work adequately.

Also, we have used data-toggle=”canvas” attribute along with other attributes documented by the Bootstrap. The aria-expanded attribute will become true when the sidebar will be visible and vice-versa.

Finally, set up the off-canvas menu or sidebar content in a div. Put there whatever you wish to display as per Bootstrap style.

A few things are worth noticing here. The id of the sidebar container is the same as the data-target element of the button. Second, we have used many Bootstrap classes to set up the container. This helps us to avoid declaring duplicate CSS rules.

Also, the off-canvas menu close button has the class bs-canvas-close. Additionally, it has some aria-* attributes. The sidebar will hide upon clicking to either this button or the content overlay.

When the sidebar is hidden, all respective aria-hidden would have the value false, otherwise true. Additionally, placing the opener button in the navbar is also possible. Read here to know how can you achieve the same.

Essential CSS for the Off-canvas

We do need a few extra classes to let the positioning and smooth slide in/out transition work.

The CSS is quite self-explanatory. While the demo has a right-sided off-canvas, the bs-canvas-left class will let you set up it in the left.

Show and Hide the Off-Canvas Menu with jQuery

Finally, a ready to place JS is below. Copy and paste the following script in your JS file or in the page within <script></script> tag:

As you can see, this script is similar to the script provided in the previous article. However, there are a few differences as well. First, instead of dynamically adding the content overlay, we’ve added it directly below the body tag. It’s because we wanted to mess as lesser as possible in the WordPress context there.

Second, we have assigned the right value to the aria-expanded attributes in the code. Nevertheless, you can make the code simpler if you don’t need the overlay.

Finally, to implement the off-canvas menu in the left sidebar, use to bs-canvas-left class to set up the sidebar content. Then update the data-target and replace the mr-0 classes with ml-0 classes in the jQuery as well.

With help of this and previous articles, you can also set up off-canvas menu and sidebars on both sides. Here are just the explanation focused more on applying Bootstrap assistive and accessibility features.

So how much do you like the demo and the articles on off-canvas menu and sidebar in Bootstrap? Do you have any difficulty or question? Do let us know in the comment form given.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.