Is it possible to have a multi level dropdown menu by using the elements of twitter bootstrap 2? The original version doesn't have this feature.
-
actually searched for an implementation but I wasn't luck with it. – Hellnar Mar 18 '12 at 17:25
-
Maybe you need a suckerfish menu? http://www.htmldog.com/articles/suckerfish/dropdowns/ – Elaine Marley Mar 19 '12 at 09:46
-
1have a look at this, too: https://github.com/twitter/bootstrap/issues/424 – Paolo May 01 '13 at 20:46
6 Answers
Updated Answer
* Updated answer which support the v2.1.1** bootstrap version stylesheet.
**But be careful because this solution has been removed from v3
Just wanted to point out that this solution is not needed anymore as the latest bootstrap now supports multi-level dropdowns by default. You can still use it if you're on older versions but for those who updated to the latest (v2.1.1 at the time of writing) it is not needed anymore. Here is a fiddle with the updated default multi-level dropdown straight from the documentation:
http://jsfiddle.net/2Smgv/2858/
Original Answer
There have been some issues raised on submenu support over at github and they are usually closed by the bootstrap developers, such as this one, so i think it is left to the developers using the bootstrap to work something out. Here is a demo i put together showing you how you can hack together a working sub-menu.
Relevant code
CSS
.dropdown-menu .sub-menu {
left: 100%;
position: absolute;
top: 0;
visibility: hidden;
margin-top: -1px;
}
.dropdown-menu li:hover .sub-menu {
visibility: visible;
display: block;
}
.navbar .sub-menu:before {
border-bottom: 7px solid transparent;
border-left: none;
border-right: 7px solid rgba(0, 0, 0, 0.2);
border-top: 7px solid transparent;
left: -7px;
top: 10px;
}
.navbar .sub-menu:after {
border-top: 6px solid transparent;
border-left: none;
border-right: 6px solid #fff;
border-bottom: 6px solid transparent;
left: 10px;
top: 11px;
left: -6px;
}
Created my own .sub-menu
class to apply to the 2-level drop down menus, this way we can position them next to our menu items. Also modified the arrow to display it on the left of the submenu group.

- 6,732
- 3
- 36
- 49

- 75,075
- 21
- 157
- 138
-
2As of a Bootstrap 2.0.4, you must also have the declaration "display: block;" in selector ".dropdown-menu li:hover .sub-menu". – Omar Jun 04 '12 at 20:32
-
I tried to implement it, the first submenu works fine. But if i'll add another submenu in the same menu, then things go wrong. For some reason it opens the first submenu instead of the second. Any idea's or clues would be welcome. Already thankx :) – Esocoder Jun 26 '12 at 14:39
-
-
@HermesTrismegistus fixed the demo with the suggested fix by Omar, everything is working now. Thanks for the heads up guys. – Andres I Perez Jun 26 '12 at 23:49
-
@AndresIlich Have you tried a JS implementation ? By using another namespace for the click ? – Sherbrow Jul 05 '12 at 17:44
-
So I forked to add a third level menu: [Demo](http://jsfiddle.net/tAFeB/). I just had to replace `.dropdown-menu li:hover .sub-menu` with `.dropdown-menu li:hover > .sub-menu`. – Vytautas Jakutis Sep 06 '12 at 08:36
-
hey @AndresIlich, The submenu with the style HOVER can't be used in Smartphone (Touch Screen). Any solution about this? – Kannika Dec 17 '12 at 10:46
-
1@Kannika mobilizing this fix would require some extra markup and probably some javascript as well. I suggest just displaying the second level dropdown content instead of hiding it on mobile devices as to keep things clean. I will try to work on a solution later during the day. I have not tested the default bootstrap solution though, so if anything you can always go with that. – Andres I Perez Dec 17 '12 at 11:30
-
5The updated version only works with Bootstrap 2.3.2, it was removed in Bootstrap 3.0 – seantomburke May 20 '14 at 05:45
-
is there a reason they removed it? Only reason i can imagine is that the sub-menues wont work on mobile devices. Edit: okay found the answer: "Submenus just don't have much of a place on the web right now, especially the mobile web. They will be removed with 3.0" - Bootstrap author Mark Otto. – Kaito Sep 10 '15 at 07:54
[Twitter Bootstrap v3]
To create a n-level dropdown menu (touch device friendly) in Twitter Bootstrap v3,
CSS:
.dropdown-menu>li /* To prevent selection of text */
{ position:relative;
-webkit-user-select: none; /* Chrome/Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */
/* Rules below not implemented in browsers yet */
-o-user-select: none;
user-select: none;
cursor:pointer;
}
.dropdown-menu .sub-menu
{
left: 100%;
position: absolute;
top: 0;
display:none;
margin-top: -1px;
border-top-left-radius:0;
border-bottom-left-radius:0;
border-left-color:#fff;
box-shadow:none;
}
.right-caret:after,.left-caret:after
{ content:"";
border-bottom: 5px solid transparent;
border-top: 5px solid transparent;
display: inline-block;
height: 0;
vertical-align: middle;
width: 0;
margin-left:5px;
}
.right-caret:after
{ border-left: 5px solid #ffaf46;
}
.left-caret:after
{ border-right: 5px solid #ffaf46;
}
JQuery:
$(function(){
$(".dropdown-menu > li > a.trigger").on("click",function(e){
var current=$(this).next();
var grandparent=$(this).parent().parent();
if($(this).hasClass('left-caret')||$(this).hasClass('right-caret'))
$(this).toggleClass('right-caret left-caret');
grandparent.find('.left-caret').not(this).toggleClass('right-caret left-caret');
grandparent.find(".sub-menu:visible").not(current).hide();
current.toggle();
e.stopPropagation();
});
$(".dropdown-menu > li > a:not(.trigger)").on("click",function(){
var root=$(this).closest('.dropdown');
root.find('.left-caret').toggleClass('right-caret left-caret');
root.find('.sub-menu:visible').hide();
});
});
HTML:
<div class="dropdown" style="position:relative">
<a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">Click Here <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a class="trigger right-caret">Level 1</a>
<ul class="dropdown-menu sub-menu">
<li><a href="#">Level 2</a></li>
<li>
<a class="trigger right-caret">Level 2</a>
<ul class="dropdown-menu sub-menu">
<li><a href="#">Level 3</a></li>
<li><a href="#">Level 3</a></li>
<li>
<a class="trigger right-caret">Level 3</a>
<ul class="dropdown-menu sub-menu">
<li><a href="#">Level 4</a></li>
<li><a href="#">Level 4</a></li>
<li><a href="#">Level 4</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Level 2</a></li>
</ul>
</li>
<li><a href="#">Level 1</a></li>
<li><a href="#">Level 1</a></li>
</ul>
</div>

- 610
- 6
- 15
-
Unfortunately this doesn't show the menu when the 'collapsed' button of the menu is showing. In Chrome and in FF. – Ben Power Nov 18 '13 at 11:29
-
@BenPower Can you please provide a jsfiddle demo of it, so that I can look into it because the jsfiddle demo I have given is working fine. Also which versions of Bootstrap, JQuery and Web browsers you are using? – Chirayu Chiripal Nov 28 '13 at 08:35
-
Its working for Bootstrap v3.0. Buddy...Could you please make it supportive for Bootstrap v2.3.2? – Niks Jain Feb 12 '14 at 12:08
-
@Niks for v2.3.2 see the updated answer of Andres Ilich above. I tested it for v2.3.2 and here is the bootply example: http://www.bootply.com/114740 – Chirayu Chiripal Feb 18 '14 at 14:05
-
@Niks my above code also works in v2.3.2, here is the bootply example: http://www.bootply.com/114744 – Chirayu Chiripal Feb 18 '14 at 14:21
-
1I added `trigger.off('click');` before setting the listeners in order to prevent "double listening" to an event in case the javascript initialization is triggered more than once. – FranBran Apr 01 '15 at 14:08
-
@ChirayuChiripal Is possible to not close the menu when click on other part of the page? – marcelo2605 Apr 13 '16 at 12:55
-
@marcelo2605 Yes. See this JsFiddle: http://jsfiddle.net/06hjeqtd/3/ Based on following answer: http://stackoverflow.com/a/19797577/2214222 – Chirayu Chiripal Apr 14 '16 at 10:59
-
1
This example is from http://bootsnipp.com/snippets/featured/multi-level-dropdown-menu-bs3
Works for me in Bootstrap v3.1.1.
HTML
<div class="container">
<div class="row">
<h2>Multi level dropdown menu in Bootstrap 3</h2>
<hr>
<div class="dropdown">
<a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="#" href="/page.html">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
<li><a href="#">Some action</a></li>
<li><a href="#">Some other action</a></li>
<li class="divider"></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Hover me for more options</a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Second level</a></li>
<li class="dropdown-submenu">
<a href="#">Even More..</a>
<ul class="dropdown-menu">
<li><a href="#">3rd level</a></li>
<li><a href="#">3rd level</a></li>
</ul>
</li>
<li><a href="#">Second level</a></li>
<li><a href="#">Second level</a></li>
</ul>
</li>
</ul>
</div>
</div>
CSS
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
-webkit-border-radius: 0 6px 6px 6px;
-moz-border-radius: 0 6px 6px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #ccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #fff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
-webkit-border-radius: 6px 0 6px 6px;
-moz-border-radius: 6px 0 6px 6px;
border-radius: 6px 0 6px 6px;
}

- 2,158
- 21
- 20
-
Thanks you ! the 3.0 version doesn't support anymore the validated answer – Asenar Mar 18 '14 at 16:29
I was able to fix the sub-menu's always pinning to the top of the parent menu from Andres's answer with the following addition:
.dropdown-menu li {
position: relative;
}
I also add an icon "icon-chevron-right" on items which contain menu sub-menus, and change the icon from black to white on hover (to compliment the text changing to white and look better with the selected blue background).
Here is the full less/css change (replace the above with this):
.dropdown-menu li {
position: relative;
[class^="icon-"] {
float: right;
}
&:hover {
// Switch to white icons on hover
[class^="icon-"] {
background-image: url("../img/glyphicons-halflings-white.png");
}
}
}

- 6,267
- 2
- 43
- 45
Since Bootstrap 3 removed the submenu part and we need to adapt ourselves the style, I think it's better to go with SmartMenu Bootstrap: https://vadikom.github.io/smartmenus/src/demo/bootstrap-navbar.html#
That would save us time on mobile responsive and style.
This plugin also very promising.

- 2,253
- 25
- 42
-
What's your opinion about https://github.com/istvan-ujjmeszaros/bootstrap-dropdown-hover ? I cannot decide. – Csaba Toth May 01 '16 at 06:41
-
1@CsabaToth, this one was also in my consider, if you want to have only the bootstrap javascript API, you can go with this one, light enough to add and can be familiar with general bootstrap skill if you need any customize. – Osify May 02 '16 at 08:37
-
Finally I gonna use one of them in one project and the other in a companion site. Each solution fits one of them better – Csaba Toth Jun 16 '16 at 06:25