Creating Dropdown Submenu on Hover in Bootstrap 5

Learn how to create dropdown submenu on hover in Bootstrap 5, a popular front-end development framework. This tutorial will walk you through the process of creating a responsive and user-friendly dropdown menu that displays submenu when the user hovers over the parent menu item.

Table of Contents:

Introduction
Creating the HTML Structure
Adding CSS Styling
Implementing JavaScript for Dropdown Functionality
Testing and Troubleshooting
Conclusion
 

Introduction:


Bootstrap is a popular front-end development framework that provides a comprehensive set of CSS and JavaScript components for building responsive web applications. One of the most commonly used components in Bootstrap is the dropdown menu, which provides a convenient way to display a list of options to the user.

In this tutorial, we will focus on how to create a dropdown submenu on hover in Bootstrap 5. A dropdown submenu is a list of options that appears when the user hovers over a parent menu item. This type of menu is commonly used in modern web applications to improve user experience and make it easier for users to navigate through complex content.

Creating the HTML Structure:


The first step in creating a dropdown submenu on hover is to create the HTML structure for the menu. In Bootstrap 5, the basic structure for a dropdown menu is as follows:


<div class="dropdown">
 <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
   Dropdown button
 </button>
 <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
   <li><a class="dropdown-item" href="#">Action</a></li>
   <li><a class="dropdown-item" href="#">Another action</a></li>
   <li><a class="dropdown-item" href="#">Something else here</a></li>
 </ul>
</div>

 


To create a dropdown submenu, we need to add a nested ul element inside the li element of the parent menu item. The nested ul element will contain the submenu items. Here's an example of how the HTML structure should look like:


<div class="dropdown">
 <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
   Dropdown button
 </button>
 <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
   <li><a class="dropdown-item" href="#">Action</a></li>
   <li class="dropdown-submenu">
     <a class="dropdown-item dropdown-toggle" href="#">Dropdown</a>
     <ul class="dropdown-menu">
       <li><a class="dropdown-item" href="#">Submenu item 1</a></li>
       <li><a class="dropdown-item" href="#">Submenu item 2</a></li>
     </ul>
   </li>
   <li><a class="dropdown-item" href="#">Another action</a></li>
   <li><a class="dropdown-item" href="#">Something else here</a></li>
 </ul>
</div>


In this example, we have added a nested ul element with the class dropdown-menu inside the li element with the class dropdown-submenu. This nested ul element contains the submenu items.

Adding CSS Styling:


Now that we have created the HTML structure for the dropdown submenu, we need to add CSS styling to make the menu look good and work properly. Here's an example of the CSS code that we can use:

.dropdown-submenu {
 position: relative;
}

.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 0.25rem 0.25rem 0;
}

.dropdown-submenu:hover .dropdown-menu {
display: block;
}

In this CSS code, we first set the position of the .dropdown-submenu element to relative. This is necessary for positioning the submenu items correctly. We then set the top and left properties of the .dropdown-submenu .dropdown-menu element to position the submenu items to the right of the parent menu item. We also set the margin-top and margin-left properties to adjust the position of the submenu items.

Finally, we set the display property of the .dropdown-submenu:hover .dropdown-menu element to block to display the submenu items when the user hovers over the parent menu item.

Implementing JavaScript for Dropdown Functionality:


To make the dropdown submenu work properly, we need to add some JavaScript code that will handle the dropdown functionality. Here's an example of the JavaScript code that we can use:

javascript code


// Enable dropdown submenu on hover
$('.dropdown-submenu').hover(function () {
 $(this).find('.dropdown-menu').addClass('show');
}, function () {
 $(this).find('.dropdown-menu').removeClass('show');
});

// Fix dropdown submenu positioning
$('.dropdown-submenu .dropdown-menu').hover(function () {
 $(this).parent().addClass('show');
}, function () {
 $(this).parent().removeClass('show');
});


In this JavaScript code, we use jQuery to enable the dropdown submenu on hover. When the user hovers over a parent menu item that has a submenu, the show class is added to the .dropdown-menu element to display the submenu items.

We also add some additional code to fix the positioning of the dropdown submenu. When the user hovers over a submenu item, we add the show class to the parent menu item to make sure that the submenu items are positioned correctly.

Testing and Troubleshooting:

Once we have added the HTML, CSS, and JavaScript code, we can test the dropdown submenu to make sure that it works properly. We should test the menu in different browsers and on different devices to ensure that it is responsive and user-friendly.

If we encounter any issues with the dropdown submenu, we can use browser developer tools to inspect the HTML and CSS code and to identify any errors or conflicts that may be causing the issue.

 

<div class="dropdown">
 <a class="dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
   Dropdown
 </a>
 <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
   <li class="dropdown-submenu">
     <a class="dropdown-item" href="#">Submenu</a>
     <ul class="dropdown-menu">
       <li><a class="dropdown-item" href="#">Submenu item 1</a></li>
       <li><a class="dropdown-item" href="#">Submenu item 2</a></li>
     </ul>
   </li>
 </ul>
</div>
 

 

.dropdown-submenu:hover .dropdown-menu {
 display: block;
}
 

const dropdownSubmenu = document.querySelectorAll('.dropdown-submenu');
dropdownSubmenu.forEach((dropdownSubmenu) => {
 dropdownSubmenu.addEventListener('mouseenter', () => {
   dropdownSubmenu.querySelector('.dropdown-menu').classList.add('show');
 });

 dropdownSubmenu.addEventListener('mouseleave', () => {
   dropdownSubmenu.querySelector('.dropdown-menu').classList.remove('show');
 });
});
 

 

html

<!-- Button trigger dropdown -->
<div class="dropdown my-unique-class">
 <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
   Dropdown button
 </button>
 <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
   <li><a class="dropdown-item" href="#">Action</a></li>
   <li><a class="dropdown-item" href="#">Another action</a></li>
   <li><a class="dropdown-item" href="#">Something else here</a></li>
 </ul>
</div>

 

css 

.my-unique-class:hover .dropdown-menu {
 display: block;
}

 

 

Conclusion:

Creating a dropdown submenu on hover in Bootstrap 5 is a great way to improve user experience and make it easier for users to navigate through complex content. By following the steps outlined in this tutorial, we can create a responsive and user-friendly dropdown menu that displays submenu when the user hovers over the parent menu item.

Examples:

Here are some examples of websites that use dropdown submenu on hover:

  1. https://www.apple.com/
  2. https://www.microsoft.com/
  3. https://www.amazon.com/
     

Content Summary:

  • Bootstrap is a popular front-end development framework that provides a comprehensive set of CSS and JavaScript components for building responsive web applications.
  • A dropdown submenu is a list of options that appears when the user hovers over a parent menu item.
  • To create a dropdown submenu in Bootstrap 5, we need to add a nested ul element inside the li element of the parent menu item.
  • We can add CSS styling to make the dropdown submenu look good and work properly.
  • We can use JavaScript code to enable the dropdown submenu on hover and to fix the positioning of the submenu items.
  • We should test the dropdown submenu in different browsers and on different devices to ensure that it is responsive and user-friendly.

Reference:

  1. Bootstrap 5 documentation: https://getbootstrap.com/docs/5.1/components/dropdowns/

Tips:

  • Use clear and descriptive class names for the HTML and CSS elements to make it easier to read and understand the code.
  • Make sure to test the dropdown submenu in different browsers and on different devices to ensure that it is responsive and user-friendly.
  • Use browser developer tools to inspect the HTML and CSS code and to identify any errors or conflicts that may be causing issues with the dropdown submenu.

FAQ:

Q: Can I create a dropdown submenu without using JavaScript?
A: Yes, you can create a basic dropdown submenu in Bootstrap 5 using only HTML and CSS. However, if you want to enable the submenu on hover and fix the positioning of the submenu items, you will need to use JavaScript.

Q: How can I add more submenu items to the dropdown submenu?
A: To add more submenu items to the dropdown submenu, simply add additional li elements with the class dropdown-item inside the nested ul element.

Q: How can I change the appearance of the dropdown submenu?
A: You can change the appearance of the dropdown submenu by modifying the CSS styling for the .dropdown-submenu and .dropdown-menu elements. You can also use custom CSS classes to add your own styling to the submenu items.