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?
Thanks a lot
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.
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.
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.
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.
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;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With