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

← Prev Next → ...

1 minute read.

Bootstrap Searchable Dropdown Menu

Searchable Dropdown Menu When we click on the dropdown button/arrow in this dropdown menu, an input field will pop up with all the dropdown items listing inside. If we type in...

5 minutes read.

CakePHP Layouts & Elements

A layout includes a presentation code that can be wrapped out in your view. It means that what you want to see in your view should be well-positioned in layout. When you create...

9 minutes read.

Bootstrap 4 Carousel

Bootstrap 4 Carousel Bootstrap 4 offers a carousel component, which is also known as a slider or slideshow. The carousel is used for the dynamic representation of content using images with...

7 minutes read.

Bootstrap Tabs and Pills

Bootstrap Menus Bootstrap menu is used to create different types of menu. It can created horizontal with its .list-inlineclass. <div class="container">       <h3>Example of Bootstrap Menu</h3>       <ul class="list-inline">         <li><a href="#">Home</a></li>         <li><a href="#">About</a></li>         <li><a href="#">Contact</a></li>         <li><a href="#">services</a></li>       </ul>     </div> Try Now Bootstrap Tabs Bootstrap tabl is look like is menu but there is some difference....

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

Bootstrap 4 Form

Bootstrap 4 Forms Bootstrap 4 provides forms, which are one of the essential components for web pages and web applications. It is an input-based component, used to collect user data with the help of...

8 minutes read.

Bootstrap Badges and Labels

Bootstrap Badges Bootstrap Badges are numerical indicators. It is used to show that how many items are associated with a link.The .badge class is used to create of Bootstrap Badges. Let us see...

2 minutes read.

Bootstrap 4 Utilities

Bootstrap 4 Utilities Bootstrap 4 utilities also known as helper classes that are used to add more style to the components. Utilities are used to make the website or webpage more stylish or elegant....

10 minutes 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 4 List Groups

Bootstrap 4 List Groups Bootstrap 4 offers List Group component, which is used to display a series of content in an organized way. The most common list group is an unordered...

13 minutes read.

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 Tables

We can create different types of responsive and awesome bootstrap table like: striped , border etc. Bootstrap Basic Table We can create a basic bootstrap table by using its class .table. It has...

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

Bootstrap 4 Modal The Modal is a component of Bootstrap 4, which is a dialog box or pop-up window. The modals are used to provide information or warning to the user, such as...

18 minutes 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 Popover

Bootstrap 4 Popover Popover A popover is similar to the tooltip. The only difference between tooltip and Popover is that in the tooltip, the popup box appears when you move the cursor...

6 minutes read.

Environment Setup

How to use Bootstrap? We can use bootstrap in our project either by downloading or by providing link of the library files. 1) Download Bootstrap files To download files, visit official site of bootstrap www.getbootstrap.com/download/ and...

1 minute read.