Home >>Bootstrap Tutorial >Bootstrap Navigation Bar

Bootstrap Navigation Bar

Bootstrap Navigation Bar

navigation bar is displayed at the top of your page. Mostly it is used in website to show the different pages that the user directly go to page by clicking the menus. By using navigation bar website becomes easy.

To create navigation bar use class .navbar with .navar-expand-*(-sm, -col, -lg) in <nav> element for responsive collapsing. For space and alignment use .flex class. You can also add class .navbar-brand for your company or brand name. By default navbar are responsive.

Basic Navbar with brand (Logo)/ Dropdown

Let's take an example of Navigation bar:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Navbar</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.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Basic Navbar with brand(Logo) / Dropdown</h2>
<nav class="navbar navbar-expand-sm bg-light">
 <!-- Brand/logo -->
 <a class="navbar-brand" href="#"><img src="../bootstrap navbar/logo.png"></a>
  <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</a>
    </li><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>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
</div>
</body>
</html>
Output:

Basic Navbar with brand(Logo) / Dropdown

Vertical Navbar

You can also display the navbar in vertically by removing the class .navbar-expand-xl|lg|md|sm. This class are used to display the navbar in horizontal form:

Let's take an example of vertical display navbar:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Navbar</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.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar bg-light">
  <!-- 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</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
</body>
</html>
Output:

Colored Navbar

For change background color of navbar, you can add class .bg-*(add contextual classes like- primary, secondary, success, danger, etc.) together with class .navbar to the <nav> element.

Let's take an Example of colored navbar:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Navbar</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.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Colored Navbar</h2>
<nav class="navbar navbar-expand-sm bg-secondary">
 <!-- Brand/logo -->
 <a class="navbar-brand" href="#">LOGO</a>
  <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</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-expand-sm bg-danger">
 <!-- Brand/logo -->
 <a class="navbar-brand" href="#">LOGO</a>
  <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</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-expand-sm bg-success">
 <!-- Brand/logo -->
 <a class="navbar-brand" href="#">LOGO</a>
  <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</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-expand-sm bg-dark">
 <!-- Brand/logo -->
 <a class="navbar-brand" href="#">LOGO</a>
  <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</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Contact</a>
    </li>
  </ul>
</nav>
</div>
</body>
</html>  

Collapsing Navbar

Collapsing the navigation bar means to hide the navbar menus on small screen by adding the button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget". You can create a div with class="collapse navbar-collapse" together with the id="". This div is matched with the data target button. When you click the button, Data-target helps you to collapse the links on small screen.

Let's take an example of collpasing Navbar:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Navbar</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.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-lg bg-light navbar-light">
  <!-- Brand -->
  <a class="navbar-brand" href="#">Navbar</a>
  <!-- Toggler/collapsibe 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="#">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Contact</a>
      </li>
    </ul>
  </div>
</nav>
</div>
</body>
</html>

Navbar Forms and Buttons

Sometimes you need a forms and buttons to the page in navbar. It makes the page user friendly and easy. You can display the forms and buttons in inline and block.

Let's take an example of buttons and forms:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Navbar</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.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
  <form class="form-inline" action="/action_page.php">
    <input class="form-control mr-sm-2" type="text" placeholder="Search">
    <button class="btn btn-success" type="submit">Search</button>
  </form>
</nav>
</div>
</body>
</html>   
Output:

No Sidebar ads