Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Bootstrap is an open-source framework that we can integrate into web projects to make our development easy. Bootstrap helps developers to save time by integrating the already done templates of various sections in a website.
This blog will teach how to implement the navigation bar into the HTML(Hyper Text Markup Language) file using the Bootstrap library. We will implement various examples of navigation bars so you can know what style or type of navigation you want on your website.
Navigation Bar
A Navigation bar is a Graphical User Interface that helps navigate one page to another on the website. With navigation, we can easily access the resources available on the website. It is the top section on a webpage that includes links for the other website areas. It performs an essential role in the website as it allows visitors to visit any section quickly & easily.
Bootstrap Navbar makes adding a navigation bar to a webpage super easy such that the developers can focus on other prominent things and make their website look professional hassle-free!
Bootstrap provides you with its classes and navbar template so you can copy the code and call it a day. To use these templates and classes available in Bootstrap, you need to include Bootstrap in your website.
You should include the following link in the <head> </head>element to use bootstrap.
To implement the navigation, we need to include the .navbar class in the <nav> tag so that we can add the styling in <a> or other tags using the sub-classes available in the .navbar class.
When you click on the navigation icon, you will see the other elements in it.
We have implemented a basic navigation bar with all the essential criteria included. We have added a dropdown menu in the navigation with the .dropdown class. We have also added a responsive behaviour in the navigation.
We have added the .collapse and .navbar-collapse classes in the <div> tag and included all the navigation elements in that <div> tag which will be collapsed when we change the screen size.
Using Bootstrap templates, you can save the time of resizing the components. With the help of the Bootstrap library, templates automatically adjust themselves according to the screen size.
Classes Used in Example
Here’s a list of the classes in Bootstrap we have used in the above code example.
.navbar-brand - Use it for the top brand element in the navbar. It works best with an anchor tag <a></a>.
.navbar-nav - Use for full height & lightweight navigation.
.nav-item - Use it to list an item.
.nav-link - Use for enclosing an anchor tag.
.navbar-toggler - Use with collapse plugin and other toggling behaviours.
.navbar-text - Use it to add vertically centred strings of text.
.active - Makes the currently active link highlighted.
.disabled - Makes a link disabled.
.collapse & .navbar-collapse - Use for grouping and hiding navbar contents.
.me-auto - Stands for “Margin End”. Assigns margin automatically.
.dropdown - Use it for dropdown elements.
.dropdown-toggle - Use it to open or close the dropdown menu.
.dropdown-menu - Use as a container for dropdown items.
.dropdown-item - Use it to list an item in the section.
.dropdown-divider - Sets a divider in the dropdown menu.
Here are some of the most used classes required for your Bootstrap Navbar.
Bootstrap Navbar requires a “navbar” class inside <nav> element with “navbar-expand{-sm, -md, -lg, -xl, or -xxl}” for responsive collapsing. Each class represents a particular size.
-sm: small
-md: medium
-lg: large
-xl: extra large
-xxl: double extra large
Note - we use <nav> element instead of <div> since it is more specific.
Use the “navbar-brand” class for your brand name.
Use “navbar-{primary, secondary, success, warning, danger, dark, light, info}” to change the navbar's text color.
Use “bg-{primary, secondary, success, warning, danger, dark, light, info}” to change the navbar's background color.
Other Classes
.navbar-btn - Use to add a button in the navigation.
.navbar-default - it adds the default styling in the navigation bar.
.navbar-inverse - use it to change the default styling with a block theme navigation bar.
.navbar-form - if you want to add form elements in the navbar, you need to add this class.
Alternate Approach
You can implement the navigation bar without using the <ul> tag. You can achieve the same result by navigating the bar with <a> tag. A <a> tag links the elements with another URL or page. Let's see how we can implement it.
Here is the above code implemented without using a list.
Like the code below, we can fix the Bootstrap Navbar to the top or bottom so that it is always visible on the webpage. Add a fixed-top to stick the navbar at the top or use a fixed-bottom to stick it at the bottom of the screen.
We have added some random lorem Ipsum text to properly display the .fixed-bottom class.
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
<title>Code Studio</title>
</head>
<body>
<h3>Fixed-bottom Example</h3>
<div class="center">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the
industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type
specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
containing Lorem Ipsum
passages, and more recently with desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.
</p>
</div>
<nav class="navbar navbar-expand-lg navbar-inverse bg-danger navbar-dark fixed-bottom">
<div class="container-fluid">
<a class="navbar-brand" href="#">Coding Ninjas</a>
</div>
</nav>
</body>
</html>
You can add an image in the navbar by adding the <img></img> tag in the <a> tag, and you need to include the image path in the src=’ ‘ property in <img> tag.
Search Bar
We can include a search bar in the navbar. We need to add the <form></form> tag, and inside, we need to add the <input> and <button> tags.
Bootstrap is a free & open-source CSS framework that aims to be a mobile-first front-end web development framework. It contains CSS & JavaScript-based interface components and design templates like forms, buttons, navbar, and many more.
What is a navigation bar?
A Navigation bar comes under Graphical User Interface that helps in accessing information. It is the top section on a webpage that includes links for the other website areas.
Why should we use Bootstrap Navbar?
Bootstrap Navbar is free & open-source, and it saves the time & effort of a developer as they are using a ready-made template and can focus on the logic instead of worrying about the styling.
Do we have more templates in Bootstrap?
There are templates for almost every website component in the Bootstrap library.
Are these templates responsive?
Yes, the templates in Bootstrap are responsive; you do not need to make them responsive.
Conclusion
In this blog, we have discussed the navigation bar. We have learned how to implement the navigation bar templates with the help of Bootstrap. We have also discussed a different approach to implementing a Bootstrap navbar.
You can check out the following links for more Bootstrap information.