how to make submenu dropdown visible outside parent dropdown - html

Currently I'm making multilevel dropdown using bootstrap 3. I expect the child menu is visible outside the parent dropdown menu. This is what I've done
HTML, CSS and JavaScript:
$(document).ready(function(){
$('.dropdown-submenu a.test').on("click", function(e){
$(this).next('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
});
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="navbar-collapse-1" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">Test</li>
<li class="dropdown">
Test 1<b class="caret"></b>
<ul role="menu" class="dropdown-menu col-xs-2" style="height: auto; max-height: 1000px; overflow-x: hidden;">
<li></li>
<li>Anting</li>
<li>Cincin</li>
<li>Diamond Jewelry</li>
<li>Dinar</li>
<li class="dropdown-submenu">
Fine Gold<span class="caret"></span>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Toko abc</a></li>
<li><a tabindex="-1" href="#">Toko cde</a></li>
</ul>
</li>
<li>Gelang</li>
<li>Kalung</li>
<li>Liontin</li>
<li>Pin & Accessories</li>
</ul>
</li>
<li class="dropdown">Test 2</li>
</ul>
</nav>
and the result is look like this. the child menu is half hidden. I've been searching for the solution and none of them solve my problem. Thanks in advance

You use position: absolute; to your submenu and you can use toggleClass() to show and hide the menu. check below code.
$(document).ready(function(){
$('.dropdown-submenu a.test').on("click", function(e){
$(this).next('ul').toggleClass('show');
e.stopPropagation();
e.preventDefault();
});
});
.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{
position:relative;
}
.dropdown-menu.submenu{
display:none;
}
.dropdown-menu.submenu.show {
position: absolute;
top: 5px;
left: 50px;
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="navbar-collapse-1" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">Test</li>
<li class="dropdown">
Test 1<b class="caret"></b>
<ul role="menu" class="dropdown-menu col-xs-2" style="height: auto; max-height: 1000px; overflow-x: hidden;">
<li></li>
<li>Anting</li>
<li>Cincin</li>
<li>Diamond Jewelry</li>
<li>Dinar</li>
<li class="dropdown-submenu">
Fine Gold<span class="caret"></span>
<ul class="dropdown-menu submenu">
<li><a tabindex="-1" href="#">Toko abc</a></li>
<li><a tabindex="-1" href="#">Toko cde</a></li>
</ul>
</li>
<li>Gelang</li>
<li>Kalung</li>
<li>Liontin</li>
<li>Pin & Accessories</li>
</ul>
</li>
<li class="dropdown">Test 2</li>
</ul>
</nav>

Related

How can I customize bootstrap dropdown?

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>

Bootstrap 4 dropdown submenu dropup variation

I am using Bootstrap 4 button dropdown. I want the dropdown menu to dropup. Using the dropup class as told here How to get a Bootstrap dropdown submenu to 'dropup', I somehow still cannot get the sub menu (Days and Hours in my code) to drop up.
HTML
<div class="dropup">
<button style="width:220px" class="btn btn-secondary dropdown-toggle ml-auto" type="button" id="dropdownMenu3" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Period</button>
<ul class="dropdown-menu dropup multi-level" role="menu" aria-labelledby="dLabel" id="option2">
<li class="dropdown-submenu others">Days
<ul class="dropdown-menu bottom-up" id="days">
<li class="dropdown-item option">1</li>
<li class="dropdown-item option">2</li>
<li class="dropdown-item option">3</li>
<li class="dropdown-item option">4</li>
</ul>
</li>
<li class="dropdown-submenu others">Hours
<ul class="dropdown-menu" id="hours">
<li class="dropdown-item option">1</li>
<li class="dropdown-item option">2</li>
<li class="dropdown-item option">3</li>
<li class="dropdown-item option">4</li>
</ul>
</li>
</ul>
</div>
CSS
<style>
.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.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;
}
</style>
Thank you in advance for the help! :)

CSS navbar image issue

I was just doing some tests with my navbar and when I wanted to put an image right before username, this happend. I don't really know what is the problem, I usually do server-side scripting but not these design. This active class on dropdown menu, as planned, needed to be together with the image, but actually it's only going to the half. If someone can give me example of what things I could put on my CSS file because there lies the problem.
Here is the image
.navbar-image {
width: 32px;
height: 32px;
position: absolute;
top: 10px;
left: -19px;
border-radius: 50%;
}
.nav-img {
position: relative;
padding-left: 50px;
}
<li class="dropdown">
<a href="#" class="dropdown-toggle nav-img" data-toggle="dropdown" role="button" aria-expanded="false">
<img src="/uploads/avatars/{{ Auth::user()->avatar }}" class='navbar-image'>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><i class='fa fa-btn fa-sticky-note'></i>Posts</li>
<li><i class='fa fa-btn fa-user'></i>Profile</li>
<li role="separator" class="divider"></li>
<li><i class="fa fa-btn fa-sign-out"></i>Log out</li>
</ul>
</li>
Try
.dropdown, .dropdown-toggle, .dropdown-menu {
display: inline-block;
}
.dropdown-toggle, .dropdown-menu {
float: right;
}
.navbar-image {
width: 32px;
height: 32px;
border-radius: 50%;
}
Here are the fixes of your menu image:
.dropdown { position: relative; }
.navbar-image {
position: absolute;
width: 32px;
height: 32px;
top: 10px; /*** Set value as per your required ***/
right: 10px; /*** Set value as per your required ***/
border-radius: 50%;
}
Try this code:
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
.navbar-image {
width: 32px;
height: 32px;
border-radius: 50%;
margin-right: 5px;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<img src="//placehold.it/80x80/?text=%20" class='navbar-image'>
User Name <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><i class='fa fa-btn fa-sticky-note'></i>Posts</li>
<li><i class='fa fa-btn fa-user'></i>Profile</li>
<li role="separator" class="divider"></li>
<li><i class="fa fa-btn fa-sign-out"></i>Log out</li>
</ul>
</li>
</ul>
</div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

How can I change the submenu at a dropdown menu from vertical to horizontal

I try to make a navbar menu where the submenu is horizontal.
Code:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Am Dakana</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1</a>
<ul class="dropdown-menu">
<li>Page 1-1</li>
<li>Page 1-2</li>
<li>Page 1-3</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 2</a>
<ul class="dropdown-menu">
<li>Page 2-1</li>
<li>Page 2-2</li>
<li>Page 2-3</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 3</a>
<ul class="dropdown-menu">
<li>Page 3-1</li>
<li>Page 3-2</li>
<li>Page 3-3</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</nav>
CSS:
.sidebar-nav {
padding: 9px 0;
}
.dropdown-menu .sub-menu {
left:100%;
position: absolute;
top: 0;
visibility: hidden;
margin-top: -1px;
}
.dropdown-menu li:hover .sub-menu {
visibility: visible;
}
.dropdown:hover .dropdown-menu {
display: block;
}
.nav-tabs .dropdown-menu, .nav-pills .dropdown-menu, .navbar .dropdown-menu {
margin-top: 0;
}
.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;
}
At this code the submenu goes vertical. I tried it with the id of the ul but it also didn´t worked (float:left).
I also read in the other threads, but i couldn´t find an answer.
Is there another way to make the submenu horizontal?
Solved the problem:
add this to your css file or use <style></style> in your html file
ul li {
display: block;
float: left;
}
li:hover ul {
display: block;
position: absolute;
left:0px;
}
li:hover li {
float: left;
font-size: 11px;
}

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>