0

I have made a minimal snippet here, which loads Bootstrap 5 (CSS and Bundle) from CDN.

This regards a collapsable navbar header, which produces unwanted scrolling behavior.

<!DOCTYPE html>
<html>
<head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <ul class="navbar-nav" id="accordionSidebar">

            <!-- Nav Item - Pages Collapse Menu -->
            <li class="nav-item" style="border: 3px solid red">
                <a class="nav-link collapsed" href="#collapseTwo" data-bs-toggle="collapse" 
                    aria-expanded="true" aria-controls="collapseTwo" style="border: 6px solid green">
                    <span style="border: 9px solid purple">Collapsable header</span>
                </a>
                <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">
                    <div class="py-2 rounded">
                        <h6 class="collapse-header">Subitems</h6>
                        <a class="collapse-item" href="ZZZZZ-subitem-1.html">Sub-item 1</a>
                        <a class="collapse-item" href="ZZZZZ-subitem-2.html">Sub-item 1</a>
                    </div>
                </div>
            </li>

        </ul>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Begin Page Content -->
                <div class="container-fluid">

                    <!-- Content Row -->
                    <div class="row">
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
ABC</br>
                    </div>

                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

    <!-- Bootstrap core JavaScript-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

</body>
</html>

Great

When you click within the green rectangle, the behavior is as wanted. There is no scroll happening when one clicks the green item. Which is great, because the end-purpose is to use this in a side-bar. It's just a collapsable header of a sidebar, which then shows the sub-items.

Not great

However, if one clicks on the text itself (the purple rectangle) then scrolling behaviour is happening. This is unwanted. How to prevent this please?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
O0123
  • 481
  • 9
  • 28

1 Answers1

1

As explained here it's because of the HTML href anchor's scroll behavior. Instead, use an href="#" data-bs-target="#collapseTwo", or a button instead of a link...

    <button type="button" class="btn btn-link collapsed" data-bs-target="#collapseTwo" data-bs-toggle="collapse" aria-expanded="true" aria-controls="collapseTwo" style="border: 6px solid green">
          <span style="border: 9px solid purple">Collapsable header</span>
    </button>

https://codeply.com/p/jMwnjoJCLJ

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624