The dropdown nav element won't work for whatever reason(meaning the dropdown doesn't come up) I've looked over all the bootstrap documentation I could find. I originally thought the problem was that I was using it in react-router dom with nav links. I realized I should try putting it into a regular HTML doc to confirm this, and low and behold it still doesn't work...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01"
aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" activeClass="active">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" activeClass="active">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" activeClass="active" id="navbarDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" activeClass="active">Action</a>
<a class="dropdown-item" activeClass="active">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" activeClass="active">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" activeClass="active">Disabled</a>
</li>
</ul>
</div>
</nav>
</body>
</html>
CodePudding user response:
1) There is one problem in your code, You have used data-toggle
but it should be data-bs-toggle
It should be
<a class="nav-link dropdown-toggle" activeClass="active" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
instead of
<a class="nav-link dropdown-toggle" activeClass="active" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
2) Be sure to include bootstrap JS file
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
3) You should change data-toggle
to data-bs-toggle
and data-target
to data-bs-target
on the button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" activeClass="active">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" activeClass="active">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" activeClass="active" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" activeClass="active">Action</a>
<a class="dropdown-item" activeClass="active">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" activeClass="active">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" activeClass="active">Disabled</a>
</li>
</ul>
</div>
</nav>
</body>
</html>