Bootstrap Navbar
Bootstrap Navbar is a responsive navigation component used to create the main navigation area of a website. It can contain a brand name, navigation links, buttons, dropdowns and other controls.
What is a Bootstrap Navbar?
A Bootstrap Navbar is a responsive navigation component that helps users move between important pages and sections of a website.
A Navbar can contain several elements such as a brand, navigation links, dropdown menus, buttons and a responsive toggle button.
1. Basic Navbar
A basic Navbar can be created using the
.navbar and .navbar-expand
classes.
.navbar class creates the Navbar,
while other Bootstrap classes control its layout,
color and responsiveness.
Example
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
CIIT
</a>
<div class="navbar-nav">
<a class="nav-link active" href="#">
Home
</a>
<a class="nav-link" href="#">
Courses
</a>
<a class="nav-link" href="#">
Contact
</a>
</div>
</div>
</nav>
Output:
2. Navbar Brand
The .navbar-brand class is used to display
the name or brand of the website.
Example
<a class="navbar-brand" href="#">
CIIT Training Institute
</a>
3. Navbar Links
Navbar links are created using the
.nav-link class.
Example
<nav class="navbar">
<a class="nav-link" href="#">
Home
</a>
<a class="nav-link" href="#">
Courses
</a>
<a class="nav-link" href="#">
About
</a>
<a class="nav-link" href="#">
Contact
</a>
</nav>
4. Navbar Colors
Bootstrap provides background and text utility classes that can be used to change the appearance of a Navbar.
bg-primary or bg-dark
to change the Navbar background.
Example
<nav class="navbar bg-primary">
<div class="container-fluid">
<a class="navbar-brand text-white" href="#">
CIIT
</a>
</div>
</nav>
Output:
5. Dark Navbar
A dark Navbar can be created using the
navbar-dark approach together with a
dark background.
Example
<nav class="navbar bg-dark">
<div class="container-fluid">
<a class="navbar-brand text-white" href="#">
CIIT
</a>
<a class="nav-link text-white" href="#">
Courses
</a>
</div>
</nav>
6. Navbar with Button
Buttons can be placed inside a Navbar for actions such as Login, Register, Enroll Now or Contact Us.
Example
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
CIIT
</a>
<button class="btn btn-primary">
Enroll Now
</button>
</div>
</nav>
7. Responsive Navbar
One of the most important features of the Bootstrap Navbar is its responsive behavior.
The Navbar can automatically collapse navigation links on smaller screens using the Bootstrap responsive classes and collapse component.
Example
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
CIIT
</a>
<button class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#mainNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse"
id="mainNavbar">
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a class="nav-link" href="#">
Home
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Courses
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
Contact
</a>
</li>
</ul>
</div>
</div>
</nav>
Output:
8. Navbar with Dropdown
A Navbar can contain dropdown menus to organize multiple related links under one menu item.
Example
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
CIIT
</a>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle"
href="#"
data-bs-toggle="dropdown">
Courses
</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="#">
Java Full Stack
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Python Full Stack
</a>
</li>
<li>
<a class="dropdown-item" href="#">
.NET Full Stack
</a>
</li>
</ul>
</li>
</ul>
</div>
</nav>
Output:
9. Navbar with Search Form
A search form can be placed inside a Navbar so that users can search for content directly from the menu.
Example
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
CIIT
</a>
<form class="d-flex">
<input class="form-control me-2"
type="search"
placeholder="Search">
<button class="btn btn-primary"
type="submit">
Search
</button>
</form>
</div>
</nav>
Output:
10. Navbar with Container
The .container or
.container-fluid class can be used
inside a Navbar to control its content width.
container keeps the content within a
responsive maximum width, while
container-fluid uses almost the full
available width.
Example
<nav class="navbar bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="#">
CIIT
</a>
<a class="nav-link" href="#">
Courses
</a>
</div>
</nav>
11. Fixed Navbar
Bootstrap provides utilities that can keep a Navbar fixed to a particular position on the screen.
Common classes include
fixed-top and
fixed-bottom.
Fixed Top Example
<nav class="navbar fixed-top bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="#">
CIIT
</a>
</div>
</nav>
12. Sticky Navbar
The sticky-top utility can be used to
make an element behave like a sticky element while
scrolling.
Example
<nav class="navbar sticky-top bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="#">
CIIT
</a>
</div>
</nav>
sticky-top is a Bootstrap utility class
that uses CSS sticky positioning.
13. Real-World CIIT Training Institute Example
A training institute website can use a Navbar to provide quick access to important sections such as Home, Courses, Internship, Placement and Contact.
Example
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand fw-bold"
href="#">
CIIT Training Institute
</a>
<button class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#ciitNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse"
id="ciitNavbar">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active"
href="#">
Home
</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">
Courses
</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">
Internship
</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">
Placement
</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">
Contact
</a>
</li>
</ul>
<button class="btn btn-primary">
Enroll Now
</button>
</div>
</div>
</nav>
Output:
Important Bootstrap Navbar Classes
| Class | Purpose |
|---|---|
.navbar |
Creates the main Navbar component. |
.navbar-brand |
Displays the website or company brand. |
.navbar-nav |
Contains the navigation links. |
.nav-item |
Represents an individual navigation item. |
.nav-link |
Styles a navigation link. |
.navbar-expand-lg |
Controls when the Navbar expands on larger screens. |
.navbar-toggler |
Creates the responsive toggle button. |
.navbar-collapse |
Contains collapsible Navbar content. |
.fixed-top |
Fixes the Navbar to the top. |
.sticky-top |
Makes the Navbar sticky while scrolling. |
Common Mistakes
-
Forgetting the
.navbarclass. -
Using an incorrect ID between
data-bs-targetandnavbar-collapse. - Forgetting Bootstrap JavaScript when using the responsive toggle or dropdown.
- Using too many links inside the Navbar.
- Not testing the Navbar on mobile screen sizes.
-
Using
fixed-topwithout considering the content that may appear underneath it. - Not providing a meaningful active link for the current page.
Summary
- Bootstrap Navbar is used to create the main navigation area of a website.
-
.navbar-brandis used for the website or company name. -
.navbar-navcontains navigation items. -
.nav-linkcreates navigation links. -
.navbar-togglerprovides a responsive menu button on smaller screens. - Navbar can contain dropdowns, buttons and search forms.
-
fixed-topcan keep the Navbar at the top. - A responsive Navbar improves navigation on both desktop and mobile devices.