How to open dropdown menu on hover in Bootstrap ?
Here is the task to open the dropdown menu on hover in Bootstrap. The dropdown on mouseover can be done using the following methods.
- Using the jQuery hover() method: It is used to specify two functions to start when mouse pointer move over the selected element.
Syntax:
$(selector).hover(Function_in, Function_out);
Approach:
- The .hover() method is the used to tackle the mouseenter event occurrence
- Store all direct children of the selected element using the .children() method.
- Check the elements whether it is visible or not using .is(“:visible”) method.
- The elements are shown using .toggleClass(“open”) method.
Example :
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>How to open dropdown menuon hover in Bootstrap</title><linkrel="stylesheet"href=<scriptsrc=</script><scriptsrc=</script><styletype="text/css">.bs-example {margin: auto;width: 25%;}@media screen {.dropdown:hover .dropdown-menu,.btn-group:hover .dropdown-menu {display: block;}.dropdown-menu {margin: auto;}.dropdown-toggle {margin: auto;}.navbar .dropdown-toggle,.nav-tabs .dropdown-toggle {margin: auto;}}</style></head><body><center><divclass="container"><h1style="text-align:center;color:green;">GeeksforGeeks</h1><h3>How to open dropdown menu on hover in Bootstrap</h3><divclass="bs-example"><!--Tabs with dropdown menu--><ulclass="nav"><liclass="active"><ahref="#">Home</a></li><li><ahref="#">Login</a></li><li><ahref="#">SignUp</a></li><liclass="dropdown"><ahref="#"data-toggle="dropdown"class="dropdown-toggle">Menu<bclass="caret"></b></a><ulclass="dropdown-menu"><li><ahref="#">Algorithm</a></li><li><ahref="#">Gate</a></li><li><ahref="#">Languages</a></li><li><ahref="#">Interview</a></li><li><ahref="#">Subject</a></li></ul></li></ul><hr></div></div><scripttype="text/javascript">$(document).ready(function() {$(".dropdown, .btn-group").hover(function() {var dropdownMenu =$(this).children(".dropdown-menu");if (dropdownMenu.is(":visible")) {dropdownMenu.parent().toggleClass("open");}});});</script></center></body></html>chevron_rightfilter_noneOutput:
Before moving mouse to the dropdown:

After moving mouse to the dropdown:

- Using the CSS property
Section of code to be used in the program.dropdown:hover .dropdown-menu { display: block; }Example:
<!DOCTYPE html><htmllang="en"><head><title>How to open dropdownmenu on hover in Bootstrap</title><linkhref=rel="stylesheet"/><scriptsrc=</script><scriptsrc=</script><styletype="text/css">.bs-example {margin: auto;width: 25%;}.dropdown:hover .dropdown-menu {display: block;}</style></head><body><center><divclass="container"><h1style="text-align:center;color:green;">GeeksforGeeks</h1><h3>How to open dropdown menu on hover in Bootstrap</h3><divclass="bs-example"><ulid="nav"class="nav nav-pills clearfix right"role="tablist"><li><ahref="#">Home</a></li><li><ahref="#">Login</a></li><li><ahref="#">SignUp</a></li><liclass="dropdown"><ahref="#"class="dropdown-toggle"data-toggle="dropdown">Menu</a><ulid="products-menu"class="dropdown-menu clearfix"role="menu"><li><ahref="">Algo</a></li><li><ahref="">Gate</a></li><li><ahref="">Subject</a></li><li><ahref="">Practise</a></li></ul></li></ul></div></div></center></body></html>chevron_rightfilter_noneOutput:
Before moving mouse to the dropdown:

After moving mouse to the dropdown:

Recommended Posts:
- How to avoid dropdown menu to close menu items on clicking inside ?
- How to display div elements using Dropdown Menu in jQuery?
- How to clear previous appended data on change the dropdown menu in JavaScript ?
- How to remove arrow in dropdown in Bootstrap ?
- How to accordion scroll to top to open content in Bootstrap ?
- How to replace dropdown-toggle icon with another default icon in Bootstrap ?
- CSS | :hover Selector
- How to write a:hover in inline CSS?
- How to disable a CSS :hover effect?
- How to write :hover condition for a:before and a:after in CSS?
- How to style a dropdown using CSS?
- Include Bootstrap in AngularJS using ng-bootstrap
- How to unbind “hover” event using JQuery?
- How to clear all options in a dropdown box?
- How to get selected value in dropdown list using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

