Lesson 52 of 60 – Bootstrap Navbar Project
87%

Bootstrap Navbar Project

In this practical project, we will create a complete responsive navigation bar using Bootstrap. You will learn how to combine the Bootstrap Navbar, brand, navigation links, dropdown menu, buttons, responsive collapse, and utility classes to build a professional website header.

Project Goal: Create a responsive website navigation bar that works on desktop, tablet, and mobile screens using Bootstrap.

1. Project Overview

Our project will contain a complete responsive website navbar.

The navbar will include:

  • Website brand.
  • Home link.
  • About link.
  • Courses link.
  • Dropdown menu.
  • Contact link.
  • Login button.
  • Mobile toggle button.

Bootstrap will automatically handle the responsive collapse behavior.

2. Create the HTML Document

Start with a standard HTML document.

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
    name="viewport"
    content="width=device-width,
    initial-scale=1.0">

    <title>
        Bootstrap Navbar Project
    </title>

</head>

<body>

    Navbar

</body>

</html>

3. Add Bootstrap CSS

Include Bootstrap CSS in the head section.

<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet">

This gives us access to Bootstrap's navbar classes and utilities.

4. Add Bootstrap JavaScript

The responsive navbar toggle and dropdown functionality require Bootstrap's JavaScript.

<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
</script>

The Bootstrap bundle includes the JavaScript needed by components such as the navbar collapse and dropdown.

5. Create the Navbar Element

Start the navigation bar with the navbar class.

<nav class="navbar">

    Navbar Content

</nav>

Bootstrap navbars are built using the navbar class together with additional navbar utilities.

6. Add Navbar Background

Use a Bootstrap background utility to style the navbar.

<nav
class="navbar
bg-dark">

    Navbar Content

</nav>

You can also use contextual background utilities such as bg-primary, bg-light, and others.

7. Add the Container

Place the navbar content inside a responsive container.

<nav class="navbar bg-dark">


    <div class="container">

        Navbar Content

    </div>


</nav>

The container keeps the navigation content organized.

8. Create the Navbar Brand

The navbar-brand class is commonly used for the website name or logo.

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

    Soopro Pathshala

</a>

The brand can contain text, an image, or both.

9. Create Navigation Links

Use the navbar-nav and nav-link classes.

<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>


</ul>

10. Mark the Active Link

Use the active class to indicate the current page.

<a
class="nav-link active"
aria-current="page"
href="#">

    Home

</a>

The aria-current="page" attribute provides useful information to assistive technologies.

11. Create the Navbar Toggler

The navbar toggler is used to open and close the collapsed navigation on smaller screens.

<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#mainNavbar"
aria-controls="mainNavbar"
aria-expanded="false"
aria-label="Toggle navigation">

    <span class="navbar-toggler-icon">
    </span>

</button>

The button targets the element with the ID mainNavbar.

12. Add Navbar Light or Dark Mode

Bootstrap provides navbar-dark and navbar-light classes for navbar content styling.

<nav
class="navbar
navbar-dark
bg-dark">

    ...

</nav>

For a dark navbar, navbar-dark helps provide suitable contrast for the navbar text and toggler.

13. Create the Collapse Area

The navigation links are placed inside a collapsible container.

<div
class="collapse navbar-collapse"
id="mainNavbar">


    Navigation Content


</div>

The ID must match the value used by the toggler's data-bs-target.

14. Create a Complete Basic Navbar

Combine the brand, toggler, collapse area, and navigation links.

<nav
class="navbar
navbar-expand-lg
navbar-dark
bg-dark">


    <div class="container">


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

            Soopro Pathshala

        </a>


        <button
        class="navbar-toggler"
        type="button"
        data-bs-toggle="collapse"
        data-bs-target="#mainNavbar"
        aria-controls="mainNavbar"
        aria-expanded="false"
        aria-label="Toggle navigation">

            <span
            class="navbar-toggler-icon">
            </span>

        </button>


        <div
        class="collapse
        navbar-collapse"
        id="mainNavbar">


            <ul
            class="navbar-nav
            ms-auto">


                <li class="nav-item">

                    <a
                    class="nav-link active"
                    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>


    </div>


</nav>

15. Understand navbar-expand-lg

The navbar-expand-lg class controls when the navbar changes from collapsed to expanded navigation.

navbar-expand-lg

The navigation is collapsed below the large breakpoint and expanded at the large breakpoint and above.

You can use other breakpoint variants such as:

  • navbar-expand-sm
  • navbar-expand-md
  • navbar-expand-lg
  • navbar-expand-xl
  • navbar-expand-xxl

16. Align Links to the Right

Use the Bootstrap spacing utility ms-auto to push the navigation list toward the end of the flex container.

<ul
class="navbar-nav
ms-auto">

    ...

</ul>

This is useful when the brand should remain on the left and the navigation should appear on the right.

17. Create a Dropdown Menu

A dropdown can contain additional navigation options.

<li
class="nav-item
dropdown">


    <a
    class="nav-link
    dropdown-toggle"
    href="#"
    role="button"
    data-bs-toggle="dropdown"
    aria-expanded="false">

        Courses

    </a>


    <ul class="dropdown-menu">


        <li>

            <a
            class="dropdown-item"
            href="#">

                HTML

            </a>

        </li>


        <li>

            <a
            class="dropdown-item"
            href="#">

                CSS

            </a>

        </li>


        <li>

            <a
            class="dropdown-item"
            href="#">

                JavaScript

            </a>

        </li>


    </ul>


</li>

18. Add a Dropdown Divider

Use dropdown-divider to visually separate groups of dropdown items.

<li>

    <hr class="dropdown-divider">

</li>

This can make a long dropdown easier to understand.

19. Add a Login Button

A Bootstrap button can be placed alongside the navigation links.

<a
href="#"
class="btn btn-primary
ms-lg-3">

    Login

</a>

The ms-lg-3 utility adds start-side margin from the large breakpoint and above.

20. Add a Sign Up Button

You can add another button for registration.

<a
href="#"
class="btn btn-success
ms-lg-2">

    Sign Up

</a>

Multiple buttons can be placed inside the collapsed navbar area.

21. Add a Search Form

A search form can also be placed inside a navbar.

<form
class="d-flex
ms-lg-3
mt-3
mt-lg-0"
role="search">


    <input
    class="form-control me-2"
    type="search"
    placeholder="Search"
    aria-label="Search">


    <button
    class="btn btn-outline-success"
    type="submit">

        Search

    </button>


</form>

The responsive spacing classes help the form work well when the navbar is collapsed.

22. Add a Navbar Shadow

Bootstrap's shadow utilities can add visual depth.

<nav
class="navbar
navbar-expand-lg
navbar-dark
bg-dark
shadow">

    ...

</nav>

The shadow utility applies a Bootstrap box shadow without requiring custom CSS.

23. Add Navbar Padding

Use spacing utilities to add vertical padding.

<nav
class="navbar
navbar-expand-lg
navbar-dark
bg-dark
py-3">

    ...

</nav>

The py-3 class adds padding to the top and bottom.

24. Add a Logo Image

The navbar brand can contain an image.

<a
class="navbar-brand
d-flex
align-items-center"
href="#">


    <img
    src="logo.png"
    alt="Soopro Pathshala"
    width="40"
    height="40"
    class="rounded-circle
    me-2">


    Soopro Pathshala


</a>

Use meaningful alternative text for informative images.

25. Create a Responsive Navbar Layout

A practical responsive navbar can combine all the components learned so far.

<nav
class="navbar
navbar-expand-lg
navbar-dark
bg-dark
py-3
shadow">


    <div class="container">


        Brand


        Toggler


        <div
        class="collapse
        navbar-collapse"
        id="mainNavbar">


            Navigation


            Search


            Buttons


        </div>


    </div>


</nav>

The navbar becomes collapsed on smaller screens and expanded at the specified breakpoint.

26. Add a Mobile-Friendly Navigation

The important responsive classes are:

  • navbar-expand-lg controls the expansion breakpoint.
  • navbar-toggler creates the mobile toggle button.
  • collapse creates the collapsible area.
  • navbar-collapse styles the collapsed navigation.
  • data-bs-toggle="collapse" activates the collapse behavior.
  • data-bs-target="#mainNavbar" identifies the target element.
Result: On smaller screens, users can open the navigation using the menu button.

27. Add a Website Hero Section

After the navbar, we can add a simple hero section to demonstrate how the navigation fits into a complete page.

<section
class="py-5
text-center">


    <div class="container">


        <h1>

            Learn New Skills

        </h1>


        <p>

            Learn web development
            step by step.

        </p>


        <a
        href="#"
        class="btn btn-primary">

            Explore Courses

        </a>


    </div>


</section>

28. Complete Navbar Structure

The complete navbar structure contains five major parts:

  1. Navbar container.
  2. Brand.
  3. Mobile toggler.
  4. Collapsible navigation.
  5. Navigation links and actions.
<nav class="navbar navbar-expand-lg">

    <div class="container">

        Brand

        Toggler

        <div
        class="collapse navbar-collapse">

            Links

            Dropdown

            Search

            Buttons

        </div>

    </div>

</nav>

29. Complete Project Code

Here is the complete responsive Bootstrap navbar project.

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">


    <meta
    name="viewport"
    content="width=device-width,
    initial-scale=1.0">


    <title>
        Soopro Pathshala
    </title>


    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    rel="stylesheet">

</head>


<body>


<nav
class="navbar
navbar-expand-lg
navbar-dark
bg-dark
py-3
shadow">


    <div class="container">


        <a
        class="navbar-brand
        fw-bold"
        href="#">

            Soopro Pathshala

        </a>


        <button
        class="navbar-toggler"
        type="button"
        data-bs-toggle="collapse"
        data-bs-target="#mainNavbar"
        aria-controls="mainNavbar"
        aria-expanded="false"
        aria-label="Toggle navigation">


            <span
            class="navbar-toggler-icon">
            </span>


        </button>


        <div
        class="collapse
        navbar-collapse"
        id="mainNavbar">


            <ul
            class="navbar-nav
            ms-auto
            align-items-lg-center">


                <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="#">

                        About

                    </a>

                </li>


                <li
                class="nav-item
                dropdown">


                    <a
                    class="nav-link
                    dropdown-toggle"
                    href="#"
                    role="button"
                    data-bs-toggle="dropdown"
                    aria-expanded="false">

                        Courses

                    </a>


                    <ul
                    class="dropdown-menu">


                        <li>

                            <a
                            class="dropdown-item"
                            href="#">

                                HTML

                            </a>

                        </li>


                        <li>

                            <a
                            class="dropdown-item"
                            href="#">

                                CSS

                            </a>

                        </li>


                        <li>

                            <a
                            class="dropdown-item"
                            href="#">

                                JavaScript

                            </a>

                        </li>


                        <li>

                            <hr
                            class="dropdown-divider">

                        </li>


                        <li>

                            <a
                            class="dropdown-item"
                            href="#">

                                All Courses

                            </a>

                        </li>


                    </ul>


                </li>


                <li class="nav-item">

                    <a
                    class="nav-link"
                    href="#">

                        Contact

                    </a>

                </li>


            </ul>


            <form
            class="d-flex
            ms-lg-3
            mt-3
            mt-lg-0"
            role="search">


                <input
                class="form-control me-2"
                type="search"
                placeholder="Search"
                aria-label="Search">


                <button
                class="btn
                btn-outline-light"
                type="submit">

                    Search

                </button>


            </form>


            <div
            class="d-flex
            gap-2
            mt-3
            mt-lg-0
            ms-lg-3">


                <a
                href="#"
                class="btn
                btn-primary">

                    Login

                </a>


                <a
                href="#"
                class="btn
                btn-success">

                    Sign Up

                </a>


            </div>


        </div>


    </div>


</nav>


<section
class="py-5
text-center">


    <div class="container">


        <h1>

            Learn New Skills

        </h1>


        <p class="lead">

            Learn web development
            step by step with
            Soopro Pathshala.

        </p>


        <a
        href="#"
        class="btn btn-primary">

            Explore Courses

        </a>


    </div>


</section>


<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
</script>


</body>

</html>

30. Final Project Result

After completing the project, you will have a complete responsive Bootstrap navbar.

  • Responsive website brand.
  • Responsive navigation links.
  • Active navigation link.
  • Courses dropdown menu.
  • Dropdown divider.
  • Search form.
  • Login button.
  • Sign Up button.
  • Mobile navbar toggler.
  • Responsive collapse behavior.
  • Bootstrap spacing utilities.
  • Bootstrap shadow utility.
  • Responsive hero section.
Project Complete! You have created a responsive Bootstrap navbar using Bootstrap's navbar, collapse, dropdown, form, button, spacing, and responsive utility classes.

📌 Key Points

  • Use navbar to create a Bootstrap navigation bar.
  • Use navbar-brand for the website name or logo.
  • Use navbar-nav for the navigation list.
  • Use nav-item for individual navigation items.
  • Use nav-link for navigation links.
  • Use navbar-expand-lg to control the responsive expansion breakpoint.
  • Use navbar-toggler for the mobile navigation button.
  • Use collapse navbar-collapse for the collapsible navigation area.
  • Use dropdown and dropdown-menu for dropdown navigation.
  • Use ms-auto to push content toward the end of a flex container.
  • Bootstrap JavaScript is required for interactive collapse and dropdown behavior.
  • Use meaningful ARIA attributes for accessible navigation controls.
  • Test the navbar at different screen sizes.

🧠 Quick Quiz

Question: Which Bootstrap class is used to create the responsive mobile menu toggle button?