With Bootstrap, a navigation bar can extend or collapse, depending on the screen size.
A standard navigation bar is created with navbar navbar-default.
- Navbars require a wrapping
.navbarwith.navbar-expand{-sm|-md|-lg|-xl}for responsive collapsing and color scheme classes. - Navbars and their contents are fluid by default. Use optional containers to limit their horizontal width.
- Use our spacing and flex utility classes for controlling spacing and alignment within navbars.
- Navbars are responsive by default, but you can easily modify them to change that. Responsive behavior depends on our Collapse JavaScript plugin.
navigation item add using nav-item
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
for dropdown use
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
product
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">sample 1</a>
<a class="dropdown-item" href="#">sample 2</a>
<a class="dropdown-item" href="#">sample 3</a>
</div>
</li>
above all final code,
<!DOCTYPE html>
<html lang="en">
<head>
<title>Navingation menu with dropdown</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="#">Logo</a>
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">about us</a>
</li>
<!-- Dropdown -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
product
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">sample 1</a>
<a class="dropdown-item" href="#">sample 2</a>
<a class="dropdown-item" href="#">sample 3</a>
</div>
</li>
</ul>
</nav>
<br>
<div class="container">
<h3>Navbar With Dropdown</h3>
<p>This example adds a dropdown menu in the navbar.</p>
</div>
</body>
</html>