Table of Contents
✅ Use the Correct Bootstrap Navbar Structure
? Key Points to Remember
Optional Enhancements
? Test It
Home Web Front-end Bootstrap Tutorial How to create a dropdown menu in a Bootstrap navbar?

How to create a dropdown menu in a Bootstrap navbar?

Aug 05, 2025 am 11:05 AM

To create a drop-down menu in the Bootstrap navigation bar, you need to use the Bootstrap classes and structure correctly. The specific steps are as follows: 1. Use .navbar, .navbar-nav, .nav-item and .nav-link to build the basic navigation bar; 2. Add .nav-item.dropdown class on the list items that need to be pulled down; 3. Add .nav-link.dropdown-toggle class to the drop-down trigger link, and set data-bs-toggle="dropdown" to activate the drop-down function; 4. Create an unordered list containing the .dropdown-menu class as the drop-down menu container, and set the menu item to the .dropdown-item class; 5. Make sure to introduce Bootstrap's JavaScript file (including Popper.js) to support drop-down interaction; 6. Optionally use .dropdown-menu-end to achieve right alignment, .dropdown-divider adds dividing lines, and .dropdown-menu-dark enables dark themes; 7. Test whether the drop-down menu can be clicked normally to expand and collapse, support keyboard operations, and click on the external area to close, and finally achieve a complete and beautiful drop-down menu.

How to create a dropdown menu in a Bootstrap navbar?

Creating a dropdown menu in a Bootstrap navbar is straightforward using Bootstrap's built-in classes and structure. Here's how to do it step by step.

How to create a dropdown menu in a Bootstrap navbar?

✅ Use the Correct Bootstrap Navbar Structure

First, make sure you're using the proper Bootstrap navbar markup. You'll need the .navbar , .navbar-nav , and .nav-item / .nav-link classes.

Here's a working example with a dropdown:

How to create a dropdown menu in a Bootstrap navbar?
 <nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">MySite</a>

    <div class="collapse navbar-collapse">
      <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="#">About</a>
        </li>

        <!-- Dropdown Menu -->
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Services
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="#">Web Design</a></li>
            <li><a class="dropdown-item" href="#">Development</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="#">Consulting</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>

? Key Points to Remember

    <li> dropdown class on <li> : Wrap the dropdown toggle and menu inside a <li> with class .nav-item.dropdown .<li> dropdown-toggle and data-bs-toggle="dropdown" : These activate the toggle behavior.<li> dropdown-menu : This is the container for the dropdown items.<li> dropdown-item : Apply this class to each link inside the dropdown for proper styling.<li> Include Bootstrap JS : Dropdowns require Bootstrap's JavaScript (and Popper.js) to work. Make sure you've included it:
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

Optional Enhancements

You can also:

    <li> Add icons or badges inside dropdown items<li> Use .dropdown-divider for visual separation<li> Position the menu on the right with .dropdown-menu-end<li> Enable dark dropdowns with .dropdown-menu-dark

Example of right-aligned dropdown:

How to create a dropdown menu in a Bootstrap navbar?
 <ul class="dropdown-menu dropdown-menu-end">
  ...
</ul>

? Test It

Make sure:

    <li> The dropdown opens on click (not hover by default) <li> It closes when clicking outside <li> It's accessible via keyboard (Tab Enter)

That's it! With the right classes and Bootstrap JS loaded, your navbar dropdown will work smoothly. Just plug it into your layout.

The above is the detailed content of How to create a dropdown menu in a Bootstrap navbar?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1594
276
How to install and use Bootstrap Icons library? How to install and use Bootstrap Icons library? Jul 27, 2025 am 01:25 AM

There are three ways to install and use BootstrapIcons: 1. Use CDN and add links to the HTML head; 2. Install through npm, suitable for modern projects such as React and Vue. You need to run npminstallbootstrap-icons and import CSS; 3. Manually download SVG or font files and import them. When using it, you can add bi and icon name classes (such as bi-heart) to insert icons. You can also use other inline elements such as span. It is recommended to use SVG files for better performance and customization capabilities. You can adjust the size through bi-lg, bi-2x and other classes, and use Bootstrap text such as text-danger.

Does Bootstrap 5 require jQuery? Does Bootstrap 5 require jQuery? Aug 03, 2025 am 01:37 AM

Bootstrap5doesnotrequirejQuery,asithasbeencompletelyremovedtomaketheframeworklighterandmorecompatiblewithmodernJavaScriptpractices.Theremovalwaspossibleduetodroppedsupportforolderbrowsers,enablingtheuseofmodernES6 JavaScriptfeaturesforbetterperforman

How to change the height of the Bootstrap navbar? How to change the height of the Bootstrap navbar? Jul 28, 2025 am 12:50 AM

Adjusting the height of the Bootstrap navigation bar can be achieved by the following methods: 1. Use custom CSS to modify the padding-top and padding-bottom values of .navbar to directly control the height; 2. Adjust the font size and line height of .navbar-nav.nav-link indirectly change the height to enhance responsive adaptability; 3. Set styles for .navbar-brand and .nav-item separately, such as height, line-height or use flex layout to optimize vertical alignment; 4. Use Bootstrap's built-in spacing tools such as p-3, py-4, etc. to quickly adjust the inner margins to affect the overall height. Palm

How to make an image responsive in Bootstrap? How to make an image responsive in Bootstrap? Aug 03, 2025 am 04:11 AM

TomakeanimageresponsiveinBootstrap,addthe.img-fluidclasstothetag;thisappliesmax-width:100%andheight:auto,ensuringtheimagescalesproportionallywithinitscontainerwithoutoverflowing;1.Use;2.Worksinsidegrids,cards,oranycontainer;3.Avoidfixedwidthsthatcanb

How to use Bootstrap forms? How to use Bootstrap forms? Aug 05, 2025 am 08:34 AM

The key to using Bootstrap forms is to master its structure and use of classes. 1. The basic form structure uses form-control, form-label, form-text and form-check to style input, label, help text and check boxes; 2. Horizontal forms are displayed in line with labels and controls by combining grid systems (such as col-sm-*), and inline forms use practical classes such as d-flex to replace the removed form-inline in Bootstrap5; 3. Form verification uses is-valid or is-invalid classes to match valid-feedback and invalid-feedback to display inversely.

Bootstrap navbar dropdown cut off by screen edge Bootstrap navbar dropdown cut off by screen edge Jul 28, 2025 am 01:11 AM

When Dropdown is truncated, you should give priority to using Bootstrap's .dropdown-menu-end class to expand the menu to the left. 1. Adding this class to dropdown-menu can solve the problem of incomplete display on the right menu 2. If it is still invalid, check whether the parent container has overflow:hidden and adjust it 3. In complex layouts, position:absolute and inset can be set through custom CSS 4. JavaScript can be used to dynamically calculate the position but not required.

How to customize Bootstrap with Sass? How to customize Bootstrap with Sass? Jul 29, 2025 am 12:47 AM

InstallBootstrapvianpmanduseitsSassfilesinsteadofprecompiledCSS.2.Createacustom.scssfileandoverrideBootstrap’sdefaultvariableslike$primary,$font-family-sans-serif,or$enable-shadowsbeforeimportingBootstrap.3.CustomizecomplexcomponentsusingSassmapssuch

How to use the Bootstrap CDN link? How to use the Bootstrap CDN link? Aug 01, 2025 am 06:01 AM

Using BootstrapCDN, you can quickly introduce CSS and JS with just two links. 1. Add CSS link in HTML: 2. Add JS scripts before: 3. Optionally introduce icon library: and then use etc. This method does not require installation and is suitable for learning and prototyping, but the production environment recommends self-hosting for better control.

See all articles