Dropdown submenu on hover in Bootstrap 4.1 - html

I am working on a website in which I want to create a dropdown submenu on hover in Bootstrap 4.1
The HTML code which I have used in order to create a dropdown menu on hover are:
<div class="navbar-collapse text-center" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
main menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">P</a>
<a class="dropdown-item" href="#">Q</a>
<a class="dropdown-item" href="#">R</a>
<a class="dropdown-item" href="#">S</a>
</div>
</li>
<button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>
</ul>
</div>
The above HTML code is the working code which displays P, Q, R, and S on hover.
Problem Statement:
I am wondering what changes I should make in the code above now so that on hover of S; dropdown items T, U, V, and W are shown.

You could just apply the inception concept and put back another dropdown-toggle element inside the menu.
To open the sub-menu on hover you have to wrap the content inside a container to apply the :hover css styling display:block on the .dropdown-menu child element that contains the submenu links.
/* makes main-menu open on hover */
.menu-item:hover > .dropdown-menu {
display: block;
}
/* makes sub-menu S open on hover */
.submenu-item:hover > .dropdown-menu {
display: block;
}
<link href="https://getbootstrap.com/docs/4.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<div class="navbar-collapse text-center" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="menu-item nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
main menu
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">P</a>
<a class="dropdown-item" href="#">Q</a>
<a class="dropdown-item" href="#">R</a>
<div class="submenu-item">
<a class="dropdown-item dropdown-toggle dropright" href="#" id="navbarDropdownSubMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
S
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownSubMenuLink">
<a class="dropdown-item" href="#">T</a>
<a class="dropdown-item" href="#">U</a>
<a class="dropdown-item" href="#">V</a>
<a class="dropdown-item" href="#">W</a>
</div>
</div>
</div>
</li>
<button type="submit" onclick="location.href='/M';" class="btn btn-default">R</button>
</ul>
</div>

Well, I think you've already figured out that isn't possible with the default bootstrap 4.1 classes.
You can achieve the desired effect using some jQuery and CSS.
First, create a div with a custom class and a list with the items you need, TUVW in your case.
After in CSS, you can hide your list container and handle its position.
In jQuery you can track mouse hover event('mouseenter mouseleave') puting your logic to show/hide the list container.

Related

Bootstrap 4 - left sidebar with menu after click icon

example photo sidebar
I looking for solution for create sidebar like on above photo.
photo2
After click icon sidebar then display menu.
I have somelike this but I need to add extra menu after click like first screen:
https://www.codeply.com/p/yRugA2FB3I
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu">
<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>
Put this as dropdown list, try to edit css of the dropdown item.
or use this as well:
<!-- Default dropright button -->
<div class="btn-group dropright">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Dropright
</button>
<div class="dropdown-menu">
<!-- Dropdown menu links -->
</div>
</div>
it is the element of bootstrap 4 that do dropdown on the right.

Why Bootstrap DropDownMenu doesn't work when clicked?

I'm trying to use a dropdown menu and include both signup and signin pages in it, but it doesnt work with me, the dropdown is not dropping down, even if I add a link in the ( <a class="nav-link dropdown-toggle" href="#" ....) it doesnt do anything... I appreciate your help in advance.
<--! Code: -->
<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">
Sign-in / Sign-up
</a>
<div class="dropdown-menu drop" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="LoginPage.php">Sign-in</a>
<a class="dropdown-item" href="Signup.php">Sign-up</a>
<div class="dropdown-divider"></div>
</div>
</li>

Removing whitespace between nav-item dropdown, default behavior not working properly

I'm trying to add the bootstrap menu on the header in the application, almost everything is ok, but when using on <nav> the <li class='nav-item dropdown'>, it adds an empty space and doesn't collapse on responsive mode.
I added a simple menu with brand on right side, with the following structure:
>Menu
--Home
--Link
--Dropdown
---Action
---Another action
---Something else
--Another dropdown
---Action
---Another action
---Something else
--Disabled
>Brand
In desktop mode, the menu is being shown properly.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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 dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Another 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 disabled" href="#">Disabled</a>
</li>
</ul>
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
My output needs to be like this:
Instead, I'm getting this:
Try to edit them in CSS.
.nav-item{
line-height: normal;
}
It seems you want to block the space of your disabled element, but not showing it.
With this condition you can't just display: none; but use any sort of work-arround.
You could get the trick simple with adding css:
.disabled { opacity: 0;}
As you can see in this Codepen:
https://codepen.io/anon/pen/dBzYNg
EDIT: I don't know how to change view width in codepen, but you can use developer tools to change the browser view or pull the border between code and preview so that preview is like mobile width.

How do I fix a hyperlink to a subpage's label? Bootsrap 4

I'm working on my first big project. I've built almost everything but the menu has problems. Clicking on the link in the menu opens the subpage but does not go to the label.
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" data-target="dropdown_target" href="#">
<span class="caret"></span>
Nurty</a>
<div class="dropdown-menu aria-labelledby=dropdown_target">
<a class="dropdown-item" href="/nurty.html">Techouse</a>
<a class="dropdown-item" href="/nurty.html#minit">Minimal</a>
<a class="dropdown-item" href="/nurty.html#classt">Classic</a>
<a class="dropdown-item" href="/nurty.html#detrt">Detroit</a>
<a class="dropdown-item disabled" href="/nurty.html#hardgroove">Hardgroove</a>
<a class="dropdown-item" href="/nurty.html#hart">Hard</a>
</div>
In subpage I want to go for example to this label:
<section id="minit">
But still not working.
How can I fix it or maybe is other way to do it?
this is the working example of your need, i also added smooth scoll by adding css smooth-behavior: smooth, i added 1500px height for clear view of your problem
Index.html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" data-target="dropdown_target" href="#">
<span class="caret"></span>
Nurty
</a>
<div class="dropdown-menu aria-labelledby=dropdown_target">
<a class="dropdown-item" href="example.html">Techouse</a>
<a class="dropdown-item" href="example.html#minit">Minimal</a>
<a class="dropdown-item" href="example.html#classt">Classic</a>
<a class="dropdown-item" href="example.html#detrt">Detroit</a>
<a class="dropdown-item disabled" href="example.html#hardgroove">Hardgroove</a>
<a class="dropdown-item" href="example.html#hart">Hard</a>
</div>
</li>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
example.html
<style>
html {
scroll-behavior: smooth;
}
</style>
<div style="height: 1500px;"></div> <!-- FOR SCROLL VIEW -->
<section id="minit">scroll or die</section>

bootstrap 4 making dropdown scrollable

I have a problem with my drop down menu on smaller devices. i cannot make it scroll able when i tried the solution here (that is overflow:auto/ overflow-y:scroll) it's not working even if i use !important. What i'm able to scroll was my main page even if the drop down is open.
<nav class="navbar navbar-toggleable-sm ">
<button class="navbar-toggler navbar-toggler-right main-navbar-toggler" type="button" data-toggle="collapse" data-target="#main-nav-collapse" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="fa fa-bars"></span>
</button>
<a class="navbar-brand animation" data-animation ="fadeInLeft" href="#"><img src="/093017/img/logo-tmi.png" alt="logo"/></a>
<div class="collapse navbar-collapse" id = "main-nav-collapse" >
<ul class="nav navbar-nav navbar-main mr-auto mt-2 mt-md-0 animation" data-animation = "fadeInRight">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Vehicles</a>
<div class="dropdown-menu default-menu main-menu sm-main-menu animation" data-animation = "fadeIn">
<!-- Nav tabs -->
<div class="sm-main-nav" >
<a class="dropdown-item" href="#">Cars</a><hr>
<a class="dropdown-item" href="#">Vans & Pickup</a><hr>
<a class="dropdown-item" href="#">SUVs & Crossover</a><hr>
<a class="dropdown-item" href="#">MPVs</a><hr>
<a class="dropdown-item" href="#">Hybrid</a><hr>
<a class="dropdown-item" href="#">Performance</a>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Owners</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop</a>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Promotions</a>
</li>
</ul>
</div>
just you can use #media in your css
#media (max-width: 500px) {
.dropdown-menu{
height:200px;
overflow-y:auto;
}
}
Use max-height instead of height:
.dropdown-menu {
max-height: 200px;
overflow-y: auto;
}
What I can suggest is in place of hard coding height in pixels you can take the advantage of the view port height like adding this style to your dropdown-menu item or add at the class level
Inline
style="overflow-y:auto; max-height:80vh"
For every dropdown
.dropdown-menu {
overflow-y:auto;
max-height:80vh
}
You can use a custom class for scroll bar for dropdown list.
<div class="sm-main-nav customClassForDropDown" >
<a class="dropdown-item" href="#">Cars</a><hr>
<a class="dropdown-item" href="#">Vans & Pickup</a><hr>
<a class="dropdown-item" href="#">SUVs & Crossover</a><hr>
<a class="dropdown-item" href="#">MPVs</a><hr>
<a class="dropdown-item" href="#">Hybrid</a><hr>
<a class="dropdown-item" href="#">Performance</a>
</div>
This is the custom css class.
.customClassForDropDown
{
height: 100px;
overflow-y: auto;
}
check this JSFIDDLE