How can I customize bootstrap dropdown? - html

I'm a beginner in front-end development, I would like modify the behavior of bootstrap dropdown. In particular I would like to activate the second level with click and rather than hover. Also the first level items should be checkable, now if I click on it the dropdown closes.
I'm using this code: https://bootsnipp.com/snippets/kM4Q

You have tagged this question with bootstrap-4 tag, but you have added a link with bootstrap 3 files only, so in my answer I choose to use those files. This is a solution for Bootstrap 3 not 4.
To make that change, you could use jQuery, working with toggleClass function transforming also this CSS rule:
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
in this:
.dropdown-submenu > .dropdown-menu.active {
display: block;
}
To reset to initial situation when you close the dropdown menu, I added this function using Dropdown's event:
$('#mydropdown').on('hidden.bs.dropdown', function () {
$('ul', $(this)).removeClass("active");
})
This is the code:
$(document).ready(function(){
$('.dropdown-submenu > a').on("click", function(e){
$(this).next('ul').toggleClass("active");
e.preventDefault();
e.stopPropagation();
});
$('#mydropdown').on('hidden.bs.dropdown', function () {
$('ul', $(this)).removeClass("active");
})
});
.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 > .dropdown-menu.active {
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;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div id="mydropdown">
<div class="dropdown">
<a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="dropdown-menu" href="#">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
<li>Some action</li>
<li>Some other action</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">
Even More..
<ul class="dropdown-menu">
<li>3rd level</li>
<li>3rd level</li>
</ul>
</li>
<li>Second level</li>
<li>Second level</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>

Related

css have another dropdown within a dropdown

I have this piece of code
<li class="dropdown" runat="server" id="btn_logout">
<i class="fa fa-user">USER</i> <b class="caret"></b>
<ul class="dropdown-menu" style="background-color:black;">
<li class="dropdown-header">DROPDOWN HERE: </li>
<li>Manage Account</li>
<li role="separator" class="divider"></li>
<li>Sign Out</li>
</ul>
</li>
I would like to have another dropdown where it says drop down here.
How can I do this?
Short answer, submenus are not supported on bootstrap.
(TLDR) There are hacks as you may imagine:
.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;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<li class="dropdown" runat="server" id="btn_logout">
<i class="fa fa-user">USER</i> <b class="caret"></b>
<ul class="dropdown-menu" style="background-color:black;">
<li class="dropdown-submenu">
<a class="dropdown-item" href="#">another level</a>
<ul class="dropdown-menu" style="background-color:black;">
<li>Manage Account</li>
<li role="separator" class="divider"></li>
<li>Sign Out</li>
</ul>
</li>
<li>Manage Account</li>
<li role="separator" class="divider"></li>
<li>Sign Out</li>
</ul>
</li>
source

Bootstrap class dropdown-menu dropdown-menu-right is not working on self-defined sub-menu's

In related to my previous question (Dropdown submenu on bootstrap is not working)
I'm currently building a sub-menu for a sub-menu of an li dropdown. So it means that it is nested. Under the banking menu there is transaction dropdown menu and below the transaction dropdown there is a menu for different type of transactions.
By the way, I've successfully created it BUT the submenu of the "transaction" module appears on the front of the transaction menu during hover, blocking the parent menu which is the transaction. How can I avoid it and place it on the right side of the transaction menu during hover? Anyways, I've done using "dropdown-menu dropdown-menu-right" but it doesnt change the output of the program. I think there is something wrong with my CSS.
HTML CODE:
<li class='dropdown'><a id = 'dLabel' class='dropdown-toggle active' data-toggle='dropdown' href='#'><img src = 'images/forbank.png' height = 35 width = 35>Banking<span class='caret'></span></a>
<ul class='dropdown-menu' role='menu' aria-labelledby='dropdownMenu'>
<li class = 'dropdown-submenu'>
<a tabindex = '-1' href='#'><span class = 'glyphicon glyphicon-cog'> </span> Transaction </a>
<ul class='dropdown-menu dropdown-menu-right' aria-labelledby='dLabel'>
<li><a tabindex='-1' href='#'> Withdrawal / Deposit </a></li>
<li><a tabindex='-1' href='#'> Fixed Deposit </a></li>
</ul>
</li>
<li role='separator' class='divider'></li>
<li><a href='#'><span class='glyphicon glyphicon-list-alt'> </span> Summaries </a></li>
<li><a href='#'><span class='glyphicon glyphicon-wrench'> </span> Settings </a></li>
</ul>
</li>
CSS CODE:
.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;
}
For anyone who stumbles upon this in the future. Refer to https://github.com/twbs/bootstrap/issues/23553.
It seems bootstrap4's dropdown-menu-right is not working outside navbars. Jipexu's hack worked for me :
.dropdown-menu-right {
right: 0;
left: auto;
}
Try this css
/* dropdown open on right side of the menu*/
.dropdown-menu {
top: 0;
left: 100%;
}
I tried a slight variation to Siddharth Garg answer above.
<div class="dropdown-menu dropdown-menu-right">
.dropdown-menu-right {
padding-left: 10px;
}
This works and allows me to set the padding.

How to make twitter bootstrap subcategory on the right side of the category when mouse hovers over it?

i want to add subcategory on the right side of the category when mouse hovers over it . Here in the code
<div class="col-md-3 column margintop20">
<ul class="nav nav-pills nav-stacked">
<li>Option 1<span class="glyphicon glyphicon-chevron-right pull-right"></span> </li>
<li>Option 2<span class="glyphicon glyphicon-chevron-right pull-right"></span> </li>
<li>Option 3<span class="glyphicon glyphicon-chevron-right pull-right"></span> </li>
<li>Option 4<span class="glyphicon glyphicon-chevron-right pull-right"></span> </li>
<li>Option 5<span class="glyphicon glyphicon-chevron-right pull-right"></span> </li>
</ul>
</div>
You need the following code to do it. I have already did it in the past and the code is working perfect. I always use the this code to do it.
EDIT:
Updated JSFiddle Demo
You need to use the following CSS.
/**** Sub DropDown Menus ****/
.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;
}
And you needs to add the following HTML to each list item in your list and it will show the child items on the right on hover.
<li class="dropdown-submenu">Option 1<span class="glyphicon glyphicon-chevron-right pull-right"></span>
<ul class="dropdown-menu">
<li>Child1</li>
<li>Child2</li>
<li>Child3</li>
<li>Child4</li>
<li>Child5</li>
</ul>
</li>
Working JSFiddle here.
**** Sub DropDown Menus ****/
.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;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-default">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">BrandLogo</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Home Page <span class="sr-only">(current)</span>
</li>
<li id="Configure" class="dropdown">
Configure<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-submenu">
Accounts1
<ul class="dropdown-menu">
<li>Option1
</li>
<li>Option2
</li>
<li>Option3
</li>
<li>Option4
</li>
<li>Option5
</li>
</ul>
</li>
<li class="dropdown-submenu">
Accounts2
<ul class="dropdown-menu">
<li>Option1
</li>
<li>Option2
</li>
<li>Option3
</li>
<li>Option4
</li>
<li>Option5
</li>
</ul>
</li>
<li class="dropdown-submenu">
Accounts3
<ul class="dropdown-menu">
<li>Option1
</li>
<li>Option2
</li>
<li>Option3
</li>
<li>Option4
</li>
<li>Option5
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>

Dropdown-submenu not functional even after adding it to CSS and implementing in html

I am using bootstrap v3.0.0. I am aware that this does not support dropdown-submenu. Lot of solutions posted on stackoverflow and other sources provided the "dropdown-submenu" CSS that I added to my Bootstrap.css and also made changes to my HTML.
And here is my HTML:
<li id="navPayments" class="dropdown">
Movies<b class="caret"></b>
<ul class="dropdown-menu">
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Scary</a>
<ul class="dropdown-menu">
<li style="display: block; width: 100%">#this.Html.ActionLink("Voilent", "Voilent", "Movies")</li>
<li style="display: block; width: 100%">#this.Html.ActionLink("Extortion", "Extortion", "Movies")</li>
<li style="display: block; width: 100%">#this.Html.ActionLink("Evil", "Evil", "Movies")</li>
</ul>
</li>
</ul>
</li>
Here is the 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 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:#cccccc;
margin-top:5px;
margin-right:-10px;
}
.dropdown-submenu:hover > a:after{
border-left-color:#ffffff;
}
.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;
}
I see the menu Scary inder Movies but it does not bring up the sub-menus when I hover over that. I am not sure what I am missing here. And it also does not show the little right arrow beside "Scary" menu.
It looks like you're using Skelly's sub menu dropdown solution
Here is a stripped down† version with comments so you can follow along with each rule:
HTML:
<!-- Parent Menu -->
<ul class="dropdown-menu" id="parentMenu"
role="menu" aria-labelledby="dropdownMenu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<!-- Child Menu -->
<li class="dropdown-submenu">
<a tabindex="-1" href="#">More options</a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Second level</a></li>
<li>Second level</li>
<li>Second level</li>
</ul>
</li>
</ul>
CSS:
/* Helps position submenu */
.dropdown-submenu {
position: relative;
}
/* Positions and styles child menu */
.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 6px;
border-radius:0 6px 6px 6px;
}
/* Makes submenu visible when hovering */
.dropdown-submenu:hover > .dropdown-menu {
display:block;
}
/* Adds caret to submenu links */
.dropdown-submenu>a:after {
float: right;
content: "►";
}
†
Took out cool css triangle and pretty formatting for left opening menus with pull-left class.
Demo in jsFiddle
Compare this version with your output.
Try to convert it over until it breaks, and tell us where you get stuck.
PS: It looks like it's working just fine

Bootstrap create third menu level with AdminLTE css

I'm currently using the AdminLTE template.
But I'm struggling with a third level menu. I've just tried with and without this plugin I'm using and I cannot figure out what I'm doing wrong, because my third level menu does not show up.
Here's the jsfiddle.
And also a image that shows what's going on.
I've read that since bootstrap 3 the dropdown-submenu was implemented, and I've tried to use it, without any success.
<ul class="dropdown-menu">
<li>Login</li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">More options</a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Second level</a></li>
<li class="dropdown-submenu">
More..
<ul class="dropdown-menu">
<li>3rd level</li>
<li>3rd level</li>
</ul>
</li>
</ul>
</li>
</ul>
I've searched around google and I found this example, which is the one I'm trying to implement.
As far as I know, bootstrap3 has no default multi-level dropdown functionality. What you are using as markup is taken from this "extension".
You are simply lacking the according CSS for this dropdown solution:
Add this to your stylesheet:
.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;
}
Your updated fiddle