Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 5 offcanvas is not working with images or icons inside the anchor

You might see it as a duplicate but let me assure you I have looked into several questions on stakeoverflow and was not able to find similar question.

I have implemented Bootstrap 5 offcanvas and there is a weird issue with it.

It works perfectly when you trigger a button (anchor/button) with text inside it. But the moment you put an image inside it or an icon svg. It stops working.

Here is my code:

Top Menu of my app

<div class="w-100 border fixed-top py-2">
<a class="btn float-start d-inline offcanvas_btn" data-bs-toggle="offcanvas" href="#sidebar_left" role="button" aria-controls="sidebar_left" >
<img src="https://www.bylde.com/assets/img/bylde-logo-small.webp?v=B01234567" class="img-fluid hlogo rounded">
</a>

<a class="btn float-end d-inline" data-bs-toggle="offcanvas" href="#sidebar_right" role="button" aria-controls="sidebar_right">
Menu
</a>

<div class="clearfix"></div>
</div>

This is sidebar code:

<div class="offcanvas offcanvas-start" tabindex="-1" id="sidebar_left" aria-labelledby="offcanvasExampleLabel">
  <div class="offcanvas-header">
    <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body">
    <div>
      Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.
    </div>
    <div class="dropdown mt-3">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
        Dropdown button
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
  </div>
</div>

Very simple straightforward implementation as per bootstrap documentation (offcanvas)

Replication steps:

  1. Go on your mobile device
  2. Go to the test URL
  3. Click on menu (top right)
  4. click on logo on top left

See the difference.

like image 385
Lonare Avatar asked Oct 26 '25 07:10

Lonare


1 Answers

This is a known issue in beta-3 that will be resolved in the 5.0.0 release.

The workaround is to use data-bs-toggle="offcanvas" on the inner element...

  <a class="btn float-start d-inline offcanvas_btn" href="#sidebar_left" role="button" aria-controls="sidebar_left">
      <img src="https://www.bylde.com/assets/img/bylde-logo-small.webp?v=B01234567" style="width: 30px" data-bs-target="#sidebar_left" data-bs-toggle="offcanvas" class="img-fluid hlogo rounded">
  </a>

Demo

like image 140
Zim Avatar answered Oct 29 '25 08:10

Zim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!