Skip to main content

Archive

Show more

Bootstrap Navbar

Bootstrap Navbar

A Bootstrap navbar is a responsive navigation bar that adapts to various screen sizes and devices. It typically contains links, buttons, or other navigation components to help users navigate through the website. Here's how to create a Bootstrap navbar:


1. Basic Navbar

To create a basic navbar, use the navbar class along with other utility classes such as navbar-expand-lg to control the responsive behavior:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

The navbar-brand class is used for the brand/logo, while the navbar-toggler class is used for the responsive toggle button.


2. Navbar Variants

Bootstrap provides different navbar variants such as light, dark, fixed-top, and fixed-bottom. You can apply these variants by adding corresponding classes:

  • Light Navbar: navbar-light bg-light
  • Dark Navbar: navbar-dark bg-dark
  • Fixed Top Navbar: fixed-top
  • Fixed Bottom Navbar: fixed-bottom

3. Navbar Links and Dropdowns

In addition to regular links, you can add dropdown menus to the navbar:

<li class="nav-item dropdown">
  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown
  </a>
  <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><hr class="dropdown-divider"></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
  </ul>
</li>

The dropdown-menu class creates a dropdown menu, and the dropdown-item class defines individual items within the dropdown.


4. Responsive Navbar

To make the navbar responsive, use the collapse class along with the navbar-toggler button:

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
  ...
</div>

5. Navbar Brand

The navbar-brand class is used for the brand/logo in the navbar:

<a class="navbar-brand" href="#">Brand</a>

Conclusion

Bootstrap provides a convenient way to create responsive navigation bars with the navbar component. By following these guidelines and examples, you can easily build navigation menus that adapt to various screen sizes and devices, improving the user experience of your website.

Comments