How to code a complete navigation bar with Bootstrap 4
Tags: webdev, css, bootstrap, html
Published: 2023-04-12 22:03:41

Bootstrap is a popular front-end framework that provides pre-designed CSS and JavaScript components to create responsive and mobile-first web pages. One of the key components of any website is the navigation bar, which provides easy access to different sections of the website.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36p4tpw4do3vag5cnbal.jpg)

To create a navigation bar with Bootstrap 4, follow these steps:

**1..** Include Bootstrap CSS and JavaScript files: You need to include the Bootstrap CSS and JavaScript files in your HTML file. You can do this by adding the following lines of code in the <head> section of your HTML file:


```
<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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

```
**2..** Create the HTML structure for the navigation bar: The next step is to create the HTML structure for the navigation bar. You can use the Bootstrap's built-in CSS classes to style your navigation bar. Here's an example of a basic navigation bar structure:

```
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Logo</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 ml-auto">
      <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>

```

**3..** Add CSS classes for styling: Bootstrap provides a wide range of CSS classes that you can use to style your navigation bar. For example, you can use the navbar class to define the main container for the navigation bar, and the navbar-light and bg-light classes to specify the light background color for the navigation bar. You can also use the navbar-brand class to style your logo, and the navbar-toggler class to create a hamburger menu icon for mobile devices.


**4..** Add JavaScript for responsive behavior: Bootstrap's navigation bar is designed to be responsive, meaning it will automatically adjust its layout based on the screen size. To enable this behavior, you need to add Bootstrap's JavaScript files as mentioned in step 1. The JavaScript files include functionality for the navigation bar, such as the collapsible hamburger menu for mobile devices.

That's it! With these steps, you should have a fully functional navigation bar using Bootstrap 4. You can further customize the appearance and behavior of the navigation bar by using additional Bootstrap classes and modifying the CSS as needed.