Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap offcanvas sidebar toggling for any canvas size

Could you please explain how offcanvas sidebar toggling works?

I can't make the actual sidebar disappear for sm. I can make the button visible (visible-sm), but toggling doesn't work - the sidebar is always visible.

Toggling works for xs size, that's fine, but I want to make it work for sm the same way, or any other size for that matter.


Partial answer: Changing max-width in offcanvas.css makes toggling work for bigger sizes as well, sm in this case:

@media screen and (max-width: 991px) {
  /* 991px instead of 767px makes toggling work for sm as well */

  .row-offcanvas {
    /* ... */
  }

  /* and so on */
}

But there's still a problem - when canvas width is between 780 and 991px, the sidebar is visible even if it's toggled off. Any idea how to fix this?

enter image description here

Thanks a lot

like image 857
ducu Avatar asked Apr 10 '14 19:04

ducu


People also ask

Which is the right placement of an Offcanvas?

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.

How do I use bootstrap off canvas?

Live demo. Use the buttons below to show and hide an offcanvas element via JavaScript that toggles the .show class on an element with the .offcanvas class. You can use a link with the href attribute, or a button with the data-bs-target attribute. In both cases, the data-bs-toggle="offcanvas" is required.

What is an off canvas menu?

An off-canvas menu, also called a flyout or side panel, is a section of your website that appears from the side of the screen when it is triggered by an action on the page. Typically, the trigger is the click of a button. However, it can also be activated by an icon, image or text.


1 Answers

How does the Bootstrap off canvas example works?

The Bootstrap offcanvas example uses the grid system. For xs screens, the content spans 12 columns (full screen width) and sidebar spans 6 columns (1/2 screen width).

Normally the sidebar would be pushed under the content. However in offcanvas.css there is a rule that uses absolute positioning to place it on the right top, outside the screen.

When you press 'Toggle nav', it moves the .row half the screen width (50%) to the left, revealing the sidebar.


Why is the sidebar shown for screen widths between 768px and 992px?

Your problem is caused by the .container placed around the .row. A container has a fixed width for viewports above 768px. You can solve this by using width: auto for small screens.

@media (max-width: 991px) {
    .container-smooth-sm {
        width: auto;
        max-width: none;
    }
}

A more advanced off canvas plugin

For my projects, this approach proved to limited. Therefor I've created the offcanvas plugin and navmenu component. The plugin can also be used to show the navbar as offcanvas menu for small screens.

Here are some examples using the Jasny Bootstrap offcanvas plugin

  • Slide in effect
  • Push effect
  • Reveal effect
  • Offcanvas navbar
like image 113
Arnold Daniels Avatar answered Sep 20 '22 08:09

Arnold Daniels