Easy Tutorial
❮ Bootstrap4 Forms Custom Bootstrap4 Breadcrumb ❯

Bootstrap4 Navbar

The navbar is typically placed at the top of the page.

We can use the .navbar class to create a standard navbar, followed by the .navbar-expand-xl|lg|md|sm class to create a responsive navbar (expanded horizontally on large screens and stacked vertically on small screens).

Options in the navbar can be created using the <ul> element with the class="navbar-nav" class. Then, add the .nav-item class on the <li> element and use the .nav-link class on the <a> element:

Example

<!-- Horizontal navbar on small screens will switch to vertical -->
<nav class="navbar navbar-expand-sm bg-light">

  <!-- Links -->
  <ul class="navbar-nav">
    <li class="nav-item">
      <a class="nav-link" href="#">Link 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 2</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 3</a>
    </li>
  </ul>

</nav>

Vertical Navbar

Create a vertical navbar by removing the .navbar-expand-xl|lg|md|sm class:

Example

<!-- Vertical navbar -->
<nav class="navbar bg-light">

  <!-- Links -->
  <ul class="navbar-nav">
    <li class="nav-item">
      <a class="nav-link" href="#">Link 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 2</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 3</a>
    </li>
  </ul>

</nav>

Centered Navbar

Create a centered navbar by adding the .justify-content-center class:

Example

<nav class="navbar navbar-expand-sm bg-light justify-content-center">
  ...
</nav>

Navbar with Different Colors

Use the following classes to create navbars with different colors: .bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary, .bg-dark, and .bg-light.

Tip: For dark backgrounds, set the text color to light, and for light backgrounds, set the text color to dark.

Example

<!-- Gray background with black text -->
<nav class="navbar navbar-expand-sm bg-light navbar-light">
  <ul class="navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Active</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link</a>
    </li>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">Disabled</a>
    </li>
  </ul>
</nav>

<!-- Black background with white text -->
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">...</nav>

<!-- Blue background with white text -->
<nav class="navbar navbar-expand-sm bg-primary navbar-dark">...</nav>

Active and Disabled States: You can add the .active class to the <a> element to highlight the selected option. The .disabled class is used to make the link unclickable.


Brand/Logo

The .navbar-brand class is used to highlight the brand/Logo:

Example

<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
  <a class="navbar-brand" href="#">Logo</a>
  ...
</nav>

You can use the .navbar-brand class to make images fit the navbar.

Example

<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
   <a class="navbar-brand" href="#">
    <img decoding="async" src="bird.jpg" alt="Logo" style="width:40px;">
  </a>
  ...
</nav>

Collapsing the Navbar

Typically, on small screens, we collapse the navbar and display the navigation options upon clicking.

To create a collapsible navbar, add class="navbar-toggler", data-toggle="collapse", and data-target="#thetarget" to the button. Then wrap the navigation content (links) in a div with class="collapse navbar-collapse", ensuring the div's id matches the id specified in the button's data-target:

Example

<nav class="navbar navbar-expand-md bg-dark navbar-dark">
  <!-- Brand -->
  <a class="navbar-brand" href="#">Navbar</a>

  <!-- Toggler/collapsible Button -->
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
    <span class="navbar-toggler-icon"></span>
  </button>

  <!-- Navbar links -->
  <div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li> 
    </ul>
  </div> 
</nav>

Using Dropdowns in the Navbar

You can set up dropdown menus in the navbar:

Example

<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="#">Link 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 2</a>
    </li>

    <!-- Dropdown -->
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
        Dropdown link
      </a>
      <div class="dropdown-menu">
        <a class="dropdown-item" href="#">Link 1</a>
        <a class="dropdown-item" href="#">Link 2</a>
        <a class="dropdown-item" href="#">Link 3</a>
      </div>
    </li>
  </ul>
</nav>

<a class="dropdown-item" href="#">Link 1</a> <a class="dropdown-item" href="#">Link 2</a> <a class="dropdown-item" href="#">Link 3</a> </div> </li> </ul> </nav>

❮ Bootstrap4 Forms Custom Bootstrap4 Breadcrumb ❯