How to implement bootstrap 4 navbar dropdown. I am using class .navvbar-bottom to make it horozontal.**
.navbar-bottom class to add scrollbar to navbar**
.navbar-bottom {
overflow-x: auto;
white-space: nowrap;
}
HTML
<nav class="navbar navbar-expand navbar-light navbar-bottom">
<ul class="navbar-nav mr-auto pl-2">
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
<li class="nav-item">
<div class="dropdown">
<a class="nav-link" href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<div class="dropdown-menu">
Dropdown1
Dropdown2
Dropdown3
Dropdown4
</div>
</div>
</li>
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
.........................................................................
.........................................................................
.........................................................................
.........................................................................
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
</ul>
</nav>
<nav class="navbar navbar-expand navbar-light">
<ul class="navbar-nav mr-auto pl-2">
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
<li class="nav-item">
<div class="dropdown">
<a class="nav-link" href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<div class="dropdown-menu">
Dropdown1
Dropdown2
Dropdown3
Dropdown4
</div>
</div>
</li>
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
.........................................................................
.........................................................................
.........................................................................
.........................................................................
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
</ul>
</nav>
This is how you can add a dropdown to your navbar menu item. Don't forget to add Bootstrap CDN's for CSS and JS in your HTML file. Also, you don't need navbar-bottom class to make the navbar horizontal.
Related
I have made navbar using Bootstrap 4 and also using .navbar-bottom class to add scrollbar to navbar when there are more menu items than page total width.
Now, issue is that the Bootstrap 4 navbar dropdown opens inside navbar. Check this issue as shown in below Screenshot GIF.
Screenshot GIF
Check Here
.navbar-bottom class to add scrollbar to navbar
.navbar-bottom {
overflow-x: auto;
white-space: nowrap;
}
HTML
<nav class="navbar navbar-expand navbar-light navbar-bottom">
<ul class="navbar-nav mr-auto pl-2">
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
<li class="nav-item">
<div class="dropdown">
<a class="nav-link" href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
<div class="dropdown-menu">
Dropdown1
Dropdown2
Dropdown3
Dropdown4
</div>
</div>
</li>
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
.........................................................................
.........................................................................
.........................................................................
.........................................................................
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
</ul>
</nav>
Try this
This solve your problem
The main problem is you use dropdown class in different div instade of the li element. Hope this solve your problem.
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<nav class="navbar navbar-expand navbar-light navbar-bottom">
<ul class="navbar-nav mr-auto pl-2">
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
<li class="nav-item"><a class="nav-link px-6" href="#">Menu Item</a></li>
</ul>
</nav>
here is code what I want to achieve by WordPress custom dropdown but I can't, can anyone tell me how can I do that, and other menu items should be normal and simple
[![<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="products.html">Products <span class="caret"></span></a>
<ul class="dropdown-menu">
<li class="nav-item">
<a class="dropdown-item" href="#">
<div class="img">
<img src="images/dropdown-img.jpg" alt="">
</div>
<span class="title">Cable &<br> Cable Accessories</span>
</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link dropdown" href="service.html">Service</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li class="nav-item">
<a class="dropdown-item" href="#">
DEMO
</a>
</li>
<li class="nav-item">
<a class="dropdown-item" href="#">
DEMO
</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="brands.html">Contact</a>
</li>
</ul>][1]][1]
How to make this treemenu active when click (selected).
Do i need to add additional javascript code?
I'm using latest bootstrap 4 and js.3.3.1
<ul class="nav submenu">
<li class="nav-item">
<a class="nav-link collapsed" href="#type" data-toggle="collapse" data-target="#type">Type</a>
<div class="collapse" id="type" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Residential</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Commercial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Industrial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Religious</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Institution</a></li>
</ul>
</div>
</li>
<li class="nav-item">
<a class="nav-link collapsed" href="#service" data-toggle="collapse" data-target="#service">Service</a>
<div class="collapse" id="service" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Residential</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Commercial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Industrial</a></li>
</ul>
</div>
</li>
<li class="nav-item">
<a class="nav-link collapsed" href="#award" data-toggle="collapse" data-target="#award">Award</a>
<div class="collapse" id="award" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Religious</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Institution</a></li>
</ul>
</div>
</li>
</ul>
I think Yes you should add additional JavaScript code.
function selectedItem(id) {
$("ul>li>a").removeClass("active");
$("#" + id).addClass("active");
}
.active {
background-color: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav submenu">
<li class="nav-item">
<a id="item1" onClick="selectedItem('item1')" class="nav-link collapsed" href="#type" data-toggle="collapse" data-target="#type">Type</a>
<div class="collapse" id="type" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Residential</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Commercial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Industrial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Religious</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Institution</a></li>
</ul>
</div>
</li>
<li class="nav-item">
<a id="item2" onClick="selectedItem('item2')" class="nav-link collapsed" href="#service" data-toggle="collapse" data-target="#service">Service</a>
<div class="collapse" id="service" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Residential</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Commercial</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Industrial</a></li>
</ul>
</div>
</li>
<li class="nav-item">
<a id="item3" onClick="selectedItem('item3')" class="nav-link collapsed" href="#award" data-toggle="collapse" data-target="#award">Award</a>
<div class="collapse" id="award" aria-expanded="false">
<ul class="flex-column nav">
<li class="nav-item"><a class="nav-link py-0" href="">Religious</a></li>
<li class="nav-item"><a class="nav-link py-0" href="">Institution</a></li>
</ul>
</div>
</li>
</ul>
Ok so I have to admit this is something I have always struggled with getting to work correctly even in BS3. But I want to have navbar links outside of the collapsed container that stay persistent.
This is what I have right now:
<nav class="navbar fixed-top navbar-expand-lg navbar-template">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">Features</a></li>
<li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown">Dropdown link</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-facebook"></span></a></li>
<li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-twitter"></span></a></li>
<li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-youtube"></span></a></li>
<li class="nav-item"><a class="nav-link" href="#"><span class="fa fa-linkedin"></span></a></li>
</ul>
</nav>
This works until the first nav is in a collapsed state. When the collapse is active it looks like this:
Has anyone found a clean solution for this?
The simplest way is using the flexbox utility classes, so no extra CSS is needed. Keep the items you always want to show out of the navbar-collapse div.
https://www.codeply.com/go/TWZGiy3VGw
<nav class="navbar fixed-top navbar-light navbar-expand-lg navbar-template">
<a class="navbar-brand" href="#">Navbar</a>
<div class="d-flex flex-row order-2 order-lg-3">
<ul class="navbar-nav flex-row">
<li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-facebook"></span></a></li>
<li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-twitter"></span></a></li>
<li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-youtube"></span></a></li>
<li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-linkedin"></span></a></li>
</ul>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="collapse navbar-collapse order-3 order-lg-2" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">Features</a></li>
<li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown">Dropdown link</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>
Notice the use the responsive order-* classes to ensure the collapse menu items remain last (order-3) when in the collapsed/mobile breakpoint.
I have the standard Bootstrap navbar with class="justify-content-center". I want this navbar to be scrollable as soon as the elements have not enough space. My problem is that, I can only scroll to the right but not to the left. The following codes that I've been using is located at https://jsfiddle.net/rwx0xLm8/.
Check it mate.
<div class="container-fluid" style="width:100%">
<div class="row justify-content-center">
<ul class="nav">
<li class="nav-item"><a class="nav-link" href="#">Tab 1</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 2</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 3</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 4</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 5</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 6</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 7</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 8</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 9</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 10</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 11</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 12</a></li>
<li class="nav-item"><a class="nav-link" href="#">Tab 13</a></li>
</ul>
</div>
</div>
.nav {
overflow:auto;
text
}
.nav .nav-item a {white-space: nowrap;}