I am making a website using Bootstrap 5. The navbar can show the navigation when it is in a responsive viewport and I click the toggler icon, but doesn’t hide if I click it again. Looking inside the developer tools I see that the class show
is still present.
My html navbar:
JavaScript
x
33
33
1
<nav class="navbar navbar-toggler navbar-light">
2
3
<div class="container-fluid">
4
5
<a class="navbar-brand text-light" href="#">
6
<img src="../TinDog Start Here/images/vecteezy_spectacle-dog-wearing-with-glasses-vector-illustration_2304572/spectacled_dog_vector_illustration1_generated.jpg" alt="Perro Vectores por Vecteezy" width="80" height="74">
7
TinDog</a>
8
9
<button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerContent" aria-controls="navbarTogglerContent" aria-expanded="false" aria-label="Toggle navigation">
10
<span class="navbar-toggler-icon"></span>
11
</button>
12
<div class="navbar-collapse collapse" id="navbarTogglerContent">
13
14
<ul class="navbar-nav ms-auto">
15
<li class="nav-item active">
16
<a href="#" class="nav-link text-light" aria-current="page">Contact</a>
17
</li>
18
<li class="nav-item">
19
<a href="#" class="nav-link text-light" aria-current="page">Pricing</a>
20
</li>
21
<li class="nav-item">
22
<a href="#" class="nav-link text-light" aria-current="page">Download</a>
23
</li>
24
25
</ul>
26
27
</div>
28
29
</div>
30
31
32
</nav>
33
The Bootstrap scripts:
JavaScript
1
4
1
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
2
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
3
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
4
The status of toggler before click:
JavaScript
1
5
1
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerContent" aria-controls="navbarTogglerContent" aria-expanded="false" aria-label="Toggle navigation">
2
<span class="navbar-toggler-icon"></span>
3
</button>
4
<div class="navbar-collapse collapse" id="navbarTogglerContent">
5
The status after click:
JavaScript
1
5
1
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerContent" aria-controls="navbarTogglerContent" aria-expanded="true" aria-label="Toggle navigation">
2
<span class="navbar-toggler-icon"></span>
3
</button>
4
<div class="navbar-collapse collapse show" id="navbarTogglerContent">
5
After I click it again it doesn’t hide the nav.
Advertisement
Answer
Here, Nothing wrong in your code. & it is working fine. but, You are added both Bundle & Separate bootstrap script. You will need to add any one from both.
Bundle:
JavaScript
1
2
1
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
2
Otherwise:
Separate:
JavaScript
1
3
1
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
2
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
3
Please read bootstrap docs here for more details.