Search code examples
drop-down-menubootstrap-4

Bootstrap4 dropdown only works in the second click


I am working on a django project, that is using bootstrap4 and I'm with a little problem with dropdown toggler.

The toggler only toggles the dropdown-menu after the second link!

What I've done wrong?

This is my dropdown HTML code:

            <li class="nav-item dropdown show">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown Toggler
                </a>
                <div class="dropdown-menu" role="menu" aria-labelledby="navbarDropdown">
                 <a class="dropdown-item">Link 1</a>
                 <a class="dropdown-item">Link 2</a>
                </div>
            </li>

Solution

  • Try to put bootstrap js file before bootstrap css

    Ex:

    1. Link Bootstrap.min.js file first.

      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
     

    1. Then link css file

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

    I got the same issue and after changing the positions of the js and css, dropdown works well. Hope it helped you.