Bootstrap 4 navbar

A navigation navbar or navbar uses in every website to make it more user-friendly. Navigating through the website is easier, and the user can instantly search for the topic of their interest. It is a strong and responsive feature for the web page. The navigation bar is located at the top of the page.

The navbar ( navigation header) constructs with the <nav> element or a <div> tag with the role="navigation" property. To acquire the Bootstrap 4 look, you'll need the ".navbar" class. By default, the navbar fills the entire width of the screen and functions like a Bootstrap 4 flex container. 

How does it work?

Before you begin using the navbar, you should be aware of the following:

  • Wrapping is required for navbars.
  • Use the .navbar-expand-sm|-md|-lg|-xl for responsive collapsing and colour scheme classes,.
  • By default, navbars and their contents work in a fluid. Use optional containers to keep their horizontal width to a minimum.
  • To modify the spacing and other styles of navbars, use our spacing and flex utility classes.
  • Navbars are responsive by default on the web page, but we can easily change this.
  • The collapse JavaScript plug-in is responsible for responsive behavior.
  • When printing, navbars are by default hidden. Add ".d-print" to the ".navbar" to force them to be printed. See the show utility class for further information.
  • Use a <nav> element to ensure accessibility.
  • If we use a more generic element like a <div>, add a role="navigation" to each navbar expressly, then it indicates assistive technologies.

Navbar supportive content

Navbars include support for a few sub-components by default. As needed, select from the following options:

Element of navbarDescription
The ".navbar-brand" Uses for the name of your company, product, or project.
The ".navbar-nav"Uses for full-height and lightweight navigation. (Including support for dropdowns).
The ".navbar-toggler" The class uses our collapse plug-in and other navigation toggling behaviors.
The ".form-inline" Uses for all form controls and actions use.
The ".navbar-text" Uses for adding text strings that are vertically cantered.
The ".collapse.navbar-collapse" Uses for grouping and hiding navbar contents by a parent breakpoint.
The ".navbar-expand-md|sm|xl" Uses for horizontal navbar on web page.

Basic Bootstrap 4 navbar

The basic bootstrap 4 navbar includes navbar class, navbar item, and navbar link with web page content. This navbar displays horizontally with simple item names. We can choose a light or dark theme for the navbar.

Example

The following example shows basic bootstrap 4 navbar for the web page.

<!DOCTYPE html>

<html lang="en">

<head>

<title> Bootstrap 4 navbar Example </title>

<meta

name = "viewport" content = "width=device-width, initial-scale = 1">

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

<script src = "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js">

</script>

<script src = "https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js">

</script>

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

</script>

</head>

<body>

<div class="container">

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

<ul class = "navbar-nav" id="links">

<li class = "nav-item" id="link1">

<a class = "nav-link" href="#"> First Link </a>

</li>

<li class = "nav-item" id="link2">

<a class = "nav-link" href="#"> Second Link </a>

</li>

<li class = "nav-item"  id="link3">

<a class = "nav-link" href="#"> Thirds Link </a>

</li>

</ul>

</nav>

<br>

<div class = "container-fluid">

<h3> Basic Bootstrap 4 Navbar Example </h3>

<p> Bootstrap's strong, responsive navigation header or navbar documentation and examples. Bootstrap 4 navbar Includes branding, navigation, and other features, as well as compatibility for our collapse plugin.</p>

</div>

</div>

</body>

</html>

Output

The following image shows the basic navbar of bootstrap version 4.

Bootstrap 4 Navbar

Contextual navbar with dropdown

The basic bootstrap 4 navbar includes contextual navbar using background classes. We can add the "bg-primary", "bg-primary", "bg-secondary", "bg-dark", "bg-success", and others. The navbar uses dropdown function for space saving and user-friendly header.

Example

The following example uses the "bg-secondary" class with dropdown function for navbar.

<!DOCTYPE html>

<html lang="en">

<head>

<title> Bootstrap 4 navbar Example </title>

<meta charset = "utf-8">

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

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

<script src = "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js">

</script>

<script src = "https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js">

</script>

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

</script>

<style>

</style>

</head>

<body>

<div class="container ">

<nav class = "navbar navbar-expand-md bg-secondary navbar-dark">

<ul class = "navbar-nav" id="links">

<li class = "nav-item" id="link1">

<a class = "nav-link" href="#"> First Link </a>

</li>

<li class = "nav-item" id="link2">

<a class = "nav-link" href="#"> Second Link </a>

</li>

<li class = "nav-item dropdown"  id="link3">

<a class = "nav-link dropdown-toggle" href = "#" data-toggle = "dropdown"> More Links </a>

<div class = "dropdown-menu bg-light" style = "border:1px solid black;" >

<a class = "dropdown-item" id = "items" href = "#">Item 1</a>

<a class = "dropdown-item" id="items" href = "#">Item 2</a>

<a class = "dropdown-item" id="items" href = "#">Item 3</a>

</div>

</li>

</ul>

</nav>

<br>

<div class = "container-fluid">

<h3> Bootstrap 4 Navbar Example </h3>

<p> Bootstrap's strong, responsive navigation header or navbar documentation and examples. Bootstrap 4 navbar Includes branding, navigation, and other features, as well as compatibility for our collapse plugin.</p>

</div>

</div>

</body>

</html>

Output

The image displays the output of the secondary navbar with the dropdown function.

Bootstrap 4 Navbar

Vertical navbar

The basic bootstrap 4 creates a verticle navbar with other functions. If navbar removes or does not use the ".navbar-expand-md|sm|xl" class. The "navbar-item" uses the "active" keyword for the default link.

Example

The following example shows vertical navbar with activate link.

<!DOCTYPE html>

<html lang="en">

<head>

<title> Bootstrap 4 navbar Example </title>

<meta charset = "utf-8">

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

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

<script src = "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js">

</script>

<script src = "https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js">

</script>

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

</script>

<style>

#links{

color:white;

}

</style>

</head>

<body>

<div class="container w-50" style="border:1px solid black;">

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

<ul class = "navbar-nav" id="links">

<li class = "nav-item active" id="link1">

<a class = "nav-link" href="#"> First Link </a>

</li>

<li class = "nav-item" id="link2">

<a class = "nav-link" href="#"> Second Link </a>

</li>

<li class = "nav-item dropdown"  id="link3">

<a class = "nav-link dropdown-toggle" href="#" data-toggle="dropdown"> More Links </a>

<div class="dropdown-menu" style="border:1px solid black;">

<a class="dropdown-item" id="items" href="#">Item 1</a>

<a class="dropdown-item" id="items" href="#">Item 2</a>

<a class="dropdown-item" id="items" href="#">Item 3</a>

</div>

</li>

</ul>

</nav>

<br>

<div class = "container-fluid">

<h3> Bootstrap 4 Navbar Example </h3>

<p> Bootstrap's strong, responsive navigation header or navbar documentation and examples.</p>

</div>

</div>

</body>

</html>

Output

The following image shows the verticle navbar with its link.

Bootstrap 4 Navbar

The following image shows the verticle navbar after clicking the dropdown function.

Bootstrap 4 Navbar

navbar with brand, form, and button

The basic bootstrap4 creates a horizontal navbar with other functions. We can add a logo, search tag, form, and buttons in the available navbar.

Example

<!DOCTYPE html>

<html lang="en">

<head>

<title> Bootstrap 4 navbar Example </title>

<meta charset = "utf-8">

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

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

<script src = "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js">

</script>

<script src = "https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js">

</script>

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

</script>

<style>

</style>

</head>

<body>

<div class="container ">

<nav class = "navbar navbar-expand-md bg-secondary navbar-dark">

<a class = "navbar-brand" id = "brand" href="#"> JTP </a>

<ul class = "navbar-nav" id="links">

<li class = "nav-item" id="link1">

<a class = "nav-link" href="#"> First Link </a>

</li>

<li class = "nav-item" id="link2">

<a class = "nav-link" href="#"> Second Link </a>

</li>

<li class = "nav-item dropdown"  id="link3">

<a class = "nav-link dropdown-toggle" href = "#" data-toggle = "dropdown"> More Links </a>

<div class = "dropdown-menu bg-light" style = "border:1px solid black;" >

<a class = "dropdown-item" id = "items" href = "#">Item 1</a>

<a class = "dropdown-item" id="items" href = "#">Item 2</a>

</li>

</ul>

<form class="form-inline" id ="forms" action="">

<input class="form-control mr-sm-2" type="text" name = "search" placeholder="User Search">

<button class="btn btn-primary" type="submit"> Search </button>

</form>

</nav>

<br>

<div class = "container-fluid">

<h3> Bootstrap 4 Navbar Example </h3>

<p> Bootstrap 4 navbar Includes branding, navigation, and other features, as well as compatibility for our collapse plugin.</p>

</div>

</div>

</body>

</html>

Output

Bootstrap 4 Navbar

Conclusion

The bootstrap 4 navbar gives an attractive, user-friendly, and space-saving header for web pages. The navbar navigates several pages and information with a header line simply and easily. We can add different functionalities and interactive user elements in a proper format.


Related Topics

Bootstrap List group

Bootstrap list group provides a group of a list of items. The most basic list is an unordered list. We can use <ul> element with .list-group class and <li> element with .lsit-group-item. Let us see...

3 minutes read.

Bootstrap Multiselect Dropdown

What is a multi-select Dropdown? Multi-select dropdown lists are used when we want to select multiple options for a particular record. Usually, dropdown lists provide the feature of choosing just one value...

4 minutes read.

Bootstrap 4 Colors

Bootstrap 4 Colors Bootstrap 4 consists of classes that are used to set colors according to the context of the element, and with the help of these classes, we can read...

2 minutes read.

Bootstrap 4 Grid Classes

Bootstrap 4 Grid Classes Extra-small Grid – In the extra-small grid system class, when you create the grid (columns), the columns will not be stacked on top of each other on...

8 minutes read.

Bootstrap Wells

In Bootstrap, well are used to add a rounded border around an element. It is default some padding and gray background color. It is like a container that displays the content....

1 minute read.

Bootstrap Panels

A bootstrap panel is used to create bordered box with some padding around its content. It is created with the .panels classes, and .panel-body classes. There are following classes of the panel: Panel header Panel...

4 minutes read.

Bootstrap Protocol (BOOTP)

In the following article, we will learn about the Bootstrap Protocol and how it maintains contracts among linked devices on webwork. What is Bootstrap Protocol? The Bootstrap Protocol is a webwork contract...

3 minutes read.

Bootstrap 4 Layout

Containers Containers are the essential elements of the bootstrap layout. It is required when we are using a bootstrap grid system. Containers can also be nested, but most layouts do not need nested containers. Bootstrap has...

3 minutes read.

Bootstrap Glyphicons

Bootstrap Glyphicon is used to create different type of glyphicons It is used in the web project. It provides 260 Glyphicons from the Glyphivons Halflings set. There are various examples of...

1 minute read.

Bootstrap Notification Bubble

What is a notification bubble? Bubbles are built into the Notification system. They float on top of the website or the button to indicate pending notifications. Bubbles can be expanded to reveal...

5 minutes read.

Bootstrap 4 Margin classes

Bootstrap 4 contains a variety of brief, responsive margin and padding utility classes to change the appearance of an element. The Bootstrap 4 margin classes use for the outer spacing of...

8 minutes read.

Bootstrap 4 Flex

Bootstrap 4 Flex Flex is a Bootstrap 4 component used to manage the layout, navigation bar, grid columns, sizing, alignment, and other components. The flexbox or flex utility is the replacement...

26 minutes read.

Text Shadows in CSS

Text shadows are created with the text-shadow CSS property. It takes a list of shadows separated by commas to be applied to the text and any of its styles. Each...

5 minutes read.

Bootstrap 4 Icons

Bootstrap 4 Icons Bootstrap 3 contains the default icon library. But in bootstrap 4, you have to include the external icon library by yourself. Many free icon libraries are available such...

2 minutes read.

Bootstrap Input groups

Bootstrap 4 Input groups The input group is a component of Bootstrap 4, which is used to create interactive and stylish form controls. With the help of input groups, you can easily customize the input...

8 minutes read.

Difference between bootstrap.css and bootstrap-theme.css

The "bootstrap.css" uses to create a website with pre-existing designs and context. It uses basic Bootstrap classes like rows and columns with a CSS styling tool. The website is styled...

5 minutes read.

Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy Bootstrap 4 offers a scrollspy component that is used to make the website more attractive and eye-catching. Basically, the scrollspy is used on the single-page website. The scrollspy highlights the nav...

5 minutes read.

Bootstrap 4 Dropdowns

Bootstrap Dropdowns Bootstrap 4 provides a dropdown component, which is a toggle and contextual layout, used to display a predefined list of links. It allows the user to select a value from the dropdown...

15 minutes read.

Bootstrap 4 Spinners

Bootstrap 4 offers spinner is a loading indicator that is used to display the loading state of any component or web page. The spinner is also known as a loader...

7 minutes read.

Bootstrap Time Picker Example

What is a Bootstrap Time picker? It is used to display the time in hours, minutes and seconds from a dropdown menu. In this section, we will create an example to...

4 minutes read.