Building Responsive Navigation Menus in Bootstrap

Alexander Obregon
3 min readAug 4, 2023

--

Image Source

Introduction

Bootstrap, an open-source CSS framework, has quickly become one of the most widely used tools for web developers worldwide. Its popularity stems from its ability to offer pre-made styles, responsive features, and user-friendly, extensive components. Among its components, the navigation menu, also known as the “navbar,” plays a critical role in enhancing the user interface and improving user experience.

In this article, we be going into the details of how to create a responsive navigation menu using Bootstrap. Before we get started, ensure that you have a fundamental understanding of HTML, CSS, and JavaScript, as they form the basis for Bootstrap.

Getting Started

To utilize Bootstrap, we need to include it in our project. You can either download Bootstrap from the official website or link directly to the Bootstrap CDN. For simplicity, let’s use the latter.

In your HTML file, include the following lines in your <head> section:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

Building a Basic Navigation Menu

First, let’s construct a basic navigation menu. We’ll add some standard elements: a brand/logo and a few links.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">MyWebsite</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>

This code will create a navigation bar with four links — Home, About, Services, and Contact.

Making the Navigation Menu Responsive

Now, let’s make the navigation menu responsive. Bootstrap has a built-in class navbar-expand-{breakpoint}, where {breakpoint} can be 'sm', 'md', 'lg', 'xl', or 'xxl'. It determines at what point the navigation menu should collapse into a hamburger menu. In our example, we've used 'lg', which means the menu will collapse on screens smaller than 'lg' (large).

The navigation bar is still missing the hamburger menu button for smaller screens. Add the following code inside the <nav> tag but outside the <div> tag:

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

The data-toggle="collapse" attribute and data-target="#navbarNav" specify what should happen when the button is clicked. In this case, it toggles the visibility of the navbarNav division.

The complete responsive navbar should look like this:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">MyWebsite</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-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">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>

Adding Advanced Features to the Navbar

Bootstrap allows you to further customize your navigation menu. You can easily add dropdown menus, forms, or even text, making the navigation menu a versatile component.

Let’s add a dropdown menu under our ‘Services’ link. Here is how to do it:

<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Services
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Service 1</a>
<a class="dropdown-item" href="#">Service 2</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Service 3</a>
</div>
</li>

Conclusion

Bootstrap, with its intuitive and flexible approach, makes responsive web design an effortless task. The navigation menu, a key component for any website, can be made responsive and highly functional with just a few lines of code in Bootstrap. We hope this tutorial served as a helpful introduction to building responsive navigation menus using this powerful tool. Whether you’re creating a basic website or a complex web application, Bootstrap’s navigation menu can undoubtedly enhance your project’s usability and visual appeal.

  1. Bootstrap Documentation
  2. Bootstrap Themes
  3. CodePen

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/