I am trying to make a nested menu in my nav header. Under Reports there should be two menu items Global Shop and Ndustrios. Then if I hover over either of them there should be more items to show. Currently what happens under Reports is all of the options below it show up when hovered. so it reads: Global Shop, Inventory, Sales Orders, etc.. but Ndustrios doesn't appear.
Here is what I have tried:
#nav_ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #1D3567;
text-align: center;
}
li {
display: inline-block;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 24px 16px;
text-decoration: none;
}
.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1D3567;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: White;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content ul {
display: none;
position: absolute;
left: 100%;
top: 75;
}
.dropdown-content:hover ul {
display: block;
}
<nav>
<ul id="nav_ul">
<li class="dropdown">
Reports
<div class="dropdown-content">
Global Shop
<div class="dropdown-content">
Inventory
Sales Orders
Quotes
Work Orders
Part Where Used
</div>
Ndustrios
</div>
</li>
</ul>
<nav>
I found the below code and modified it to reflect my needs. Nested drop downs working perfectly
.parent {display: block;position: relative;float: left;line-height:30px;background-color: #1D3567;}
.parent a{margin: 10px;color: #FFFFFF;text-decoration: none;}
.parent:hover > ul {display:block;position:absolute;}
.child {display: none;}
.child li {background-color: #1D3567;line-height: 30px;border-bottom:#CCC 1px solid;border-right:#CCC 1px solid; width:100%;}
.child li a{color: #FFFFFF;}
ul{list-style-type: none;margin: 0;padding: 0px; min-width:15em;}
ul ul ul{left: 100%;top: 0;margin-left:1px;}
li:hover {background-color: #95B4CA;}
.parent li:hover {background-color: #95B4CA;}
<ul id="nav_ul">
<li class="parent">Home</li>
<li class="parent">Outlook Web</li>
<li class="parent">Production
<ul class="child">
<li class="parent">South</li>
<li class="parent">Enclosure Systems</li>
<li class="parent">South</li>
<li class="parent">Factory Andon</li>
<li class="parent">Web Docs</li>
</ul>
</li>
<li class="parent">IT
<ul class="child">
<li>Knowledge Base</li>
<li>Submit a Ticket</li>
<li class="parent">Archived Links</li>
</ul>
</li>
<li class="parent">Office Directories
<ul class="child">
<li>Hennig</li>
<li>AME</li>
</ul>
</li>
<li class="parent">Hennig Parts</li>
<li class="parent">Factory Andon</li>
<li class="parent">Business Cards</li>
<li class="parent">Reports
<ul class="child">
<li class="parent">Global Shop<span class="expand">»</span>
<ul class="child">
<li>Inventory Report</li>
<li>Sales Report</li>
<li>Quotes Report</li>
<li>Work Order Report</li>
<li>Part Where Used Report</li>
</ul>
</li>
<li class="parent">Ndustrios<span class="expand">»</span>
</li>
</ul>
</li>
You will need to do some restructuring to your HTML and use another list inside your li to contain more links. You will still be left with a pretty serious issue though. If you no longer hover the submenu item and the list gets shorter and then you are no longer hovering anything. For example: hover over reports then global shop and try to hover over Ndustrios. You cannot do it because once you leave the drop down sub <li> the list will shrink leaving you with nothing. You have two options. 1. Place Ndustrios above the dropdown or 2. Make the dropdowns appear on click instead of on hover
#nav_ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #1D3567;
text-align: center;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 24px 16px;
text-decoration: none;
}
.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1D3567;
min-width: 160px;
z-index: 1;
list-style: none;
}
.dropdown-content a {
color: White;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-sub-list{
display: none;
list-style: none;
margin: 0;
}
.dropdown:hover > .dropdown-content {
display: block;
}
.dropdown-sub:hover > .dropdown-sub-list {
display: block;
}
<nav>
<ul id="nav_ul">
<li class="dropdown">
Reports
<ul class="dropdown-content">
<li class="dropdown-sub">
Global Shop
<ul class="dropdown-sub-list">
<li>Inventory</li>
<li>Sales Orders</li>
<li>Quotes</li>
<li>Work Orders</li>
<li>Part Where Used</li>
</ul>
</li>
<li>Ndustrios</li>
</ul>
</li>
</ul>
</nav>
If you are dead set on using dropdowns inside of dropdowns here is a simple example of a hover click dropdown combo. You can also make the top level dropdown clickable if you would like using this method.
const dropdownBtns = document.querySelectorAll(".dropdown-button");
const dropdownItem = document.querySelectorAll(".dropdown-item");
const subDropdown = document.querySelectorAll(".sub-dropdown");
const handleClick = (event) => {
const targetBtn = event.target;
const ariaExpanded = targetBtn.getAttribute("aria-expanded");
const ariaControls = targetBtn.getAttribute("aria-controls");
const controlledAria = document.getElementById(ariaControls);
if(ariaExpanded === "false") {
controlledAria.classList.add("active");
controlledAria.setAttribute("aria-hidden", "false");
targetBtn.setAttribute("aria-expanded", "true");
} else if (ariaExpanded === "true") {
controlledAria.classList.remove("active");
controlledAria.setAttribute("aria-hidden", "true");
targetBtn.setAttribute("aria-expanded", "false");
}
}
const handleLeave = () => {
dropdownBtns.forEach(button => button.setAttribute("aria-expanded", "false"));
subDropdown.forEach(ul => {
ul.classList.remove("active");
ul.setAttribute("aria-hidden", "true");
})
}
dropdownItem.forEach(item => item.addEventListener("mouseleave", handleLeave));
dropdownBtns.forEach(button => button.addEventListener("click", handleClick));
ul {
list-style: none;
padding: 0;
}
button {
background: transparent;
border: none;
width: 100%;
font-family: inherit;
font-size: inherit;
cursor: pointer;
}
a, button {
display: block;
padding: 10px;
border-bottom: 1px solid #ccc;
text-align: center;
}
a {
text-decoration: none;
color: inherit;
}
.dropdown {
display: none;
}
.dropdown-item:hover .dropdown {
display: block;
}
.sub-dropdown {
display: none;
}
.sub-dropdown.active {
display: block;
}
<ul>
<li class="dropdown-item">
hover item
<ul class="dropdown">
<li>
<button class="dropdown-button" aria-controls="dropdown1" aria-expanded="false">click item</button>
<ul id="dropdown1" class="sub-dropdown" aria-hidden="true">
<li>sub link</li>
<li>sub link</li>
<li>sub link</li>
<li>sub link</li>
<li>sub link</li>
</ul>
</li>
<li>standard link</li>
</ul>
</li>
<ul>
Related
I have been looking at multiple stackoverflow questions about how to get a dropdown menu working, but so far none of them solved the problem. I have a navigation list with a couple of links in it and would like to have a hover option on one of them that would drop down a more specific list of options.
Here is the HTML of the list:
<div id="leftMenu" ng-if="loggedin">
<li>Koti</li>
<li>Jäsentiedot</li>
<li>
<div class="dropdown">
Asukastiedot
<ul class="dropdown-content" role="menu">
<li>Kaste</li>
</ul>
</div>
</li>
<li>Raportointi</li>
<li>Toiminnot</li>
<li>Admin</li>
</div>
Here is my CSS:
.dropdown {
width: auto;
display: inline-block;
position: relative;
padding: 0px;
margin: -1px;
}
.dropdown-content {
position: absolute;
width:auto;
z-index:1000;
display: block;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block; margin-top: 0;}
Thanks in advance for your help on this problem!
.sub {
display: none;
}
.main > li:hover .sub {
display: block;
}
<ul class="main">
<li>a
<ul class="sub">
<li>a1</li>
<li>a2</li>
<li>a3</li>
</ul>
</li>
<li>b
<ul class="sub">
<li>b1</li>
<li>b2</li>
<li>b3</li>
</ul>
</li>
<li >c
<ul class="sub">
<li>c1</li>
<li>c2</li>
<li>c3</li>
</ul>
</li>
</ul>
The dropdown menu (sub) does not appear right below the element it is supposed to when I hover on the navbar. How can I get it to appear below it instead of to the right of it? Also, how can I get the extra spacing on the left side of each menu to disappear? I tried setting the padding to 0, but that did not change anything.
a {
text-decoration: none;
color: #000;
}
.navbar {
background-color: #f2f2f2;
position: fixed;
top: 0;
width: 100%;
text-align: center;
font-family: "Raleway";
font-size: 93%;
border-width:1px 0;
list-style:none;
margin:0;
padding:0;
left: 0;
}
.navbar > li{
display:inline-block;
}
.navbar li {
list-style-type: none;
padding-left: none;
}
.navbar a {
float: center;
display:inline-block;
color: #000000;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.navbar a:hover {
background-color: #ddd;
}
.navbar li:hover ul.sub {
display: inline-block;
}
.dropdown {
position: relative;
display: inline-block;
}
ul.sub {
padding-left: none;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
ul.sub a {
color: #000000;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
<ul class = "navbar">
<!--add dropdown menu links for everything except collaboration-->
<li class = "dropdown">
<li>About FRES(H)
<ul class = "sub">
<li>Safety</li>
<li>Sense</li>
<li>Express</li>
<li>Keep Fresh</li>
<li>Notebook</li>
<li>Interlab</li>
<li>Protocols</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Models
<ul class = "sub">
<li>Protein Modelling Etnr1</li>
<li>Protein Modelling Etnr2</li>
<li>Internal Cellular Model</li>
<li>Macroscopic Diffusion Model</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Technology
<ul class = "sub">
<li>Primer Design App</li>
<li>SmartPhone App</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Human-Centered Design
<ul class = "sub">
<li>Integrated Human Practices</li>
<li>Outreach</li>
<li>Theory of Knowledge</li>
</ul>
</li>
</li>
<li>Engagement</li>
<li class = "dropdown">
<li>Meet the Team
<ul class = "sub">
<li>About Us</li>
<li>Attributions</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Awards
<ul class = "sub">
<li>Parts</li>
<li>Medals</li>
</ul>
</li>
</li>
</ul>
You were almost there! You just added the :hover to the wrong div.
Make the following changes:
display:none to ul.sub and .navbar li:hover ul.sub instead of .navbar:hover ul.sub
UPDATE: to centre the ul.sub, use display: block. To remove padding, assign value of 0, instead of 'none'. See example below:
a {
text-decoration: none;
color: #000;
}
.navbar {
background-color: #f2f2f2;
position: fixed;
top: 0;
width: 100%;
text-align: center;
font-family: "Raleway";
font-size: 93%;
border-width:1px 0;
list-style:none;
margin:0;
padding:0;
left: 0;
}
.navbar > li{
display:inline-block;
}
.navbar li {
list-style-type: none;
padding-left: none;
}
.navbar a {
float: center;
display:inline-block;
color: #000000;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.navbar a:hover {
background-color: #ddd;
}
.navbar li:hover ul.sub {
display: block;
}
.dropdown {
position: relative;
display: inline-block;
}
ul.sub {
padding-left: 0;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
display:none;
}
ul.sub a {
color: #000000;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
<ul class = "navbar">
<!--add dropdown menu links for everything except collaboration-->
<li class = "dropdown">
<li>About FRES(H)
<ul class = "sub">
<li>Safety</li>
<li>Sense</li>
<li>Express</li>
<li>Keep Fresh</li>
<li>Notebook</li>
<li>Interlab</li>
<li>Protocols</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Models
<ul class = "sub">
<li>Protein Modelling Etnr1</li>
<li>Protein Modelling Etnr2</li>
<li>Internal Cellular Model</li>
<li>Macroscopic Diffusion Model</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Technology
<ul class = "sub">
<li>Primer Design App</li>
<li>SmartPhone App</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Human-Centered Design
<ul class = "sub">
<li>Integrated Human Practices</li>
<li>Outreach</li>
<li>Theory of Knowledge</li>
</ul>
</li>
</li>
<li>Engagement</li>
<li class = "dropdown">
<li>Meet the Team
<ul class = "sub">
<li>About Us</li>
<li>Attributions</li>
</ul>
</li>
</li>
<li class = "dropdown">
<li>Awards
<ul class = "sub">
<li>Parts</li>
<li>Medals</li>
</ul>
</li>
</li>
</ul>
I'm having trouble aligning my drop down menu in my nav bar, I've tried every suggestion out there. I've tried left, float: left, right, and pretty much everything else. I think it is possibly something interfering. The drop down menu has everything aligned from center to right of the parent menu item.
https://jsfiddle.net/ethacker/j7tgq95j/3/
My html code:
<header>
<nav>
<h1> Welcome to Mommy Madness</h1>
<ul>
<li class="parentMenu">Home
<ul class="sub-menu">
<li>About</li>
<li>Contact</li>
</ul>
</li>
<li class="parentMenu">Pregnancy
<!--
Gender Predictions:
Old Wive's Tale
Boy vs Girl- The Ramzi Method
-->
<ul class="sub-menu">
<li>Advice</li>
<li>Gender Predictions</li>
<li>Trying To Conceive</li>
</ul>
</li>
<li class="parentMenu">All About Baby
<ul class="sub-menu">
<li>Fetal Development</li>
<li>Guidelines </li>
<li> Milestones</li>
</ul>
</li>
<li class="parentMenu">Party Momma
<!--
Birthdays - Link to 1-10th bdays.
-->
<ul class="sub-menu">
<li>Pregnancy Announcement</li>
<li>Gender Reveal</li>
<li>Baby Shower</li>
<li>Birth Announcement</li>
<li> Birthdays</li>
</ul>
</li>
<li class="parentMenu">Stations
<ul class="sub-menu">
<li>Hospital Bag</li>
<li>Diaper Bag</li>
<li>Changing Station</li>
<li>Baby Gear</li>
</ul>
</li>
<li class="parentMenu">Memory Markers
<!--
Drop Down Menu:
DIY
Purchases
(Both to have holiday/event selectors on right of page)
-->
<ul class="sub-menu">
<li>DIY</li>
<li>Purchases</li>
</ul>
</li>
<li class="parentMenu">Reviews
<ul class="sub-menu">
<li>Games</li>
<li>Gear</li>
<li>Learning</li>
</ul>
</li>
<li class="parentMenu">Blog
<ul class="sub-menu">
<li>Fit Momma</li>
<li>Minimal Momma</li>
<li>Modern Momma</li>
<li>Organic Momma</li>
<li>Organizing Queen</li>
<li>Savings Savvy</li>
<li>Tech Savvy</li>
<li>Traditional Momma</li>
</ul>
</li>
</ul>
</nav>
My css code:
body {
background-color: beige;
color: lightblue;
padding: 0;
margin:0;
}
header {
background-color: lightblue;
padding: 5px 0;
margin: 0;
}
header h1 {
color: cadetblue;
font-family: Arial;
margin: 0;
padding: 5px 15px;
display: inline;
}
.navMenu{
display: inline;
margin: 0;
}
.navMenu .parentMenu {
display: inline-block;
list-style-type: none;
background-color: lightgray;
padding: 5px 10px;
border: thin solid darkgray;
border-radius: 3px;
color: honeydew;
position: relative;
margin: 0;
}
.navMenu .parentMenu a{
color: azure;
}
.navMenu .parentMenu .sub-menu{
display: none;
}
.navMenu .parentMenu:hover .sub-menu{
display: block;
position: absolute;
list-style-type: none;
margin:0;
}
.parentMenu:hover .sub-menu li{
border: thin solid darkgray;
padding: 4%;
background-color: lightgray;
color: honeydew;
text-align: left;
white-space: nowrap;
width: 100%;
list-style-type: none;
margin: 0;
}
.parentMenu .sub-menu li:hover {
background-color: lightsteelblue;
}
.section {
background-color: wheat;
color: darkslategray;
padding: 5px;
float: left;
display: inline;
width: 63%;
margin: 0 1% 1% 1%;
border-radius: 10px;
border: thin solid khaki;
box-shadow: lightgray;
}
#about {
float: right;
width: 30%;
margin: 1% 1% 0 0;
text-align: center;
}
#home{
margin: 1% 0 1% 1%;
}
h4, h3 {
color: lightseagreen;
}
This will align the submenu to the left:
.navMenu .parentMenu .sub-menu {
display: none;
position:absolute;
list-style-type: none;
padding:0;
margin: 0;
left:-1px;
top:27px;
}
.navMenu .parentMenu:hover .sub-menu {
display: block;
}
https://jsfiddle.net/am13qur4/
you did not specify where you want to align your drop down elements. Do you want to align all to the right, center or left. I assumed left so try adding the code below. You may need to adjust the left attribute's value and your hover background styling too.
.sub-menu a{
position: absolute;
left: 3%;
}
Let me know if this helps. Stay warm!
Need to select all sibling <li> elements on hover. Tried accepted answer here but it is not working. JSFiddle here
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu li:hover ~ .menu li{/*ON THIS HOVER NOT SHOWING ALL SIBLIING LIs*/
display: block !important;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a></li>
<li>Home</li>
<li>Portfolio</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu</li>
<li>Sub Menu</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
Your problem is the selector:
.menu li:hover ~ .menu li
A hidden element can't be hovered-over, which means that li:hover is never going to match an element. Also, the general-sibling combinator is trying to find (subsequent) siblings that are <li> elements descending from sibling .menu elements. Which matches no elements in the page.
Converting that to the following selector:
.menu:hover li ~ li
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu:hover li ~ li {
display: block;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a>
</li>
<li>Home
</li>
<li>Portfolio
</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu
</li>
<li>Sub Menu
</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
works; that said it will - because of the general sibling combinator - match only those <li> elements with a preceding <li> sibling, which means it will, and can, never show the first <li>.
So, instead, I'd suggest using:
.menu:hover li
.menu {
margin: 0;
padding: 0;
list-style: none;
overflow: hidden;
;
background-color: #777;
}
.menu li {
float: none;
display: none;
}
.menu li a {
display: block;
padding: 10px 20px 10px 20px;
text-decoration: none;
color: #bbb;
}
.menu li a:hover {
color: #fff;
}
.menu .btn {
display: block;
cursor: pointer;
}
.menu:hover li {
display: block;
}
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
<ul class="menu">
<li class="btn"><a>☰</a>
</li>
<li>Home
</li>
<li>Portfolio
</li>
<li>Contact
<ul class="sub-menu">
<li>Sub Menu
</li>
<li>Sub Menu
</li>
</ul>
</li>
</ul>
<!--NEED SOLUTION WITHOUT ALTERING HTML -->
I've been able to create a horizontal menu using (display:inline) and I now have a drop menu thanks to sousMenu. My problem is that all the submenus, regardless of element I hovered over, appear in the same place. How do I work around this?
My menu code thus far:
<ul>
<li>Home</li>
<li class="sousMenu">About Us
<ul>
<li>Board of Directors</li>
</br>
<li>Student Profiles</li>
</br>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul>
<li>Donations</li>
</br>
<li>Job Board</li>
</br>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul>
<li>Connections</li>
</br>
<li>Gallery</li>
</br>
<li>Tours</li>
</ul>
</li>
CSS:
#navcontainer ul {
/*margin: 0;*/
margin-left: auto;
margin-right: auto;
padding: 0;
top:180;
right:20;
width:800px;
list-style-type: none;
text-align: center;
position: absolute;
color: #fff;
background-color: #003300;
padding: .2em 1em;
}
#navcontainer ul li {
display: inline;
padding-left:2cm;
}
#navcontainer ul li a {
text-decoration: none;
color: #fff;
background-color: #030;
}
#navcontainer ul li a:hover {
color: #fff;
background-color: #000;
}
.sousMenu:hover ul {
display: block;
}
.sousMenu ul {
text-align: center;
display: none;
list-style-type: none;
}
Try setting the parent list item to position: relative and the child ul to position: absolute for starters. I made some other slight modifications to your code to achieve the desired effect.
Here's the CSS:
* {
margin: 0;
padding: 0;
vertical-align: baseline;
}
li {
list-style-type: none;
}
ul.main li {
position: relative;
display: inline-block;
}
.main li:hover > ul {
display: block;
}
ul.sub {
position: absolute;
display: none;
top: 100%;
left: 0;
}
ul.sub li {
display: block;
}
I also cleaned up the HTML a bit. You were missing a closing </ul> tag as well:
<ul class="main">
<li>Home</li>
<li class="sousMenu">About Us
<ul class="sub about">
<li>Board of Directors</li>
<li>Student Profiles</li>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul class="sub get-involved">
<li>Donations</li>
<li>Job Board</li>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul class="sub resources">
<li>Connections</li>
<li>Gallery</li>
<li>Tours</li>
</ul>
</li>
</ul>
Here's the fiddle: http://jsfiddle.net/vXhZb/2/