Home > Software design >  Bootstrap 5 navbar not collapsing
Bootstrap 5 navbar not collapsing

Time:10-14

i have an issue with bootstrap 5 navbar toggle. It's not toggling. Exactly same code with bootstrap 4 works fine.

I've already tried by some examples to remove navbar-toggler class from button and navbar-collapse class from navbar but it didn't work.

Workaround that I've used is to add onclick element to a button which would point to javascript which will add show class to navbar but I need to figure out why the native method doesn't work.

Tried on Firefox running on Fedora linux and Chrome running on Android 11 phone.

Bootstrap 5 example (not working):

<!doctype html>
<html lang="sr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=false, initial-scale=1">
    <link rel="icon" href="{{ url_for('static', path='/images/favicon.png') }}" sizes="32x32" />

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <title>Navbar Test</title>
  </head>
  <body>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <div >
        <nav >
            <button  type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                <span ></span>
            </button>
            <div  id="navbarNavAltMarkup">
                <div >
                    <a  href="/">Home</a>
                    <a  href="/link1">Link 1</a>
                    <a  href="/link2">Link 2</a>
                    <div >
                        <a  href="#" role="button" id="dropdownMenuReports" data-bs-toggle="dropdown" aria-expanded="false">
                          Reports
                        </a>
                      
                        <ul  aria-labelledby="dropdownMenuReports">
                          <li><a  href="/reports/reminders">Reminders - Send</a></li>
                          <li><a  href="/reports/reminders?render_only">Reminders - Display Only</a></li>
                        </ul>
                      </div>
                </div>
            </div>
        </nav>
    </div>
  </body>
</html>

Bootstrap 4 example (working):

<!doctype html>
<html lang="sr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=false, initial-scale=1">
    <link rel="icon" href="{{ url_for('static', path='/images/favicon.png') }}" sizes="32x32" />

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    <title>Navbar Test</title>
  </head>
  <body>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <div >
        <nav >
            <button  type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                <span ></span>
            </button>
            <div  id="navbarNavAltMarkup">
                <div >
                    <a  href="/">Home</a>
                    <a  href="/link1">Link 1</a>
                    <a  href="/link2">Link 2</a>
                    <div >
                        <a  href="#" role="button" id="dropdownMenuReports" data-bs-toggle="dropdown" aria-expanded="false">
                          Reports
                        </a>
                      
                        <ul  aria-labelledby="dropdownMenuReports">
                          <li><a  href="/reports/reminders">Reminders - Send</a></li>
                          <li><a  href="/reports/reminders?render_only">Reminders - Display Only</a></li>
                        </ul>
                      </div>
                </div>
            </div>
        </nav>
    </div>
  </body>
</html>

CodePudding user response:

Change two things:

  • data-toggle="collapse" (Bootstrap 4) to data-bs-toggle="collapse" (Bootstrap 5)
  • data-target="#navbarNavAltMarkup" (Bootstrap 4) to data-bs-target="#navbarNavAltMarkup" (Bootstrap 5)

See the snippet below.

<!doctype html>
<html lang="sr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=false, initial-scale=1">
  <link rel="icon" href="{{ url_for('static', path='/images/favicon.png') }}" sizes="32x32" />

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  <title>Navbar Test</title>
</head>

<body>
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
  <div >
    <nav >
      <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                <span ></span>
            </button>
      <div  id="navbarNavAltMarkup">
        <div >
          <a  href="/">Home</a>
          <a  href="/link1">Link 1</a>
          <a  href="/link2">Link 2</a>
          <div >
            <a  href="#" role="button" id="dropdownMenuReports" data-bs-toggle="dropdown" aria-expanded="false">
                          Reports
                        </a>

            <ul  aria-labelledby="dropdownMenuReports">
              <li><a  href="/reports/reminders">Reminders - Send</a></li>
              <li><a  href="/reports/reminders?render_only">Reminders - Display Only</a></li>
            </ul>
          </div>
        </div>
      </div>
    </nav>
  </div>
</body>

</html>

  • Related