I'm new here & am working on my first mobile web project. Am having a problem with my dropdown menu. It drops down when hovered. However, it only display the first sub-menu. The rest of the menu is frozen. Nothing happens even when I click on any of the other parts of the menu. Appreciate any help coming this way. Many thanks in advanced.
This is my menu...
<nav>
<div id="nav"><!-- #Begin navigation -->
<div class="dropdown">
<button class="dropbtn">Menu</button>
<div class="dropdown-content">
<ul>
<ul>
<li>HOME</li>
</ul>
<ul>
<li>Sauces
<ul>
<li>French</li>
<li>Italian</li>
<li>Indian</li>
<li>Mexican</li>
<li>Chinese</li>
</ul>
</li>
</ul>
<ul>
<li>Cheese
<ul>
<li>Swiss</li>
<li>Danish</li>
<li>Dutch</li>
<li>French</li>
<li>Italian</li>
<li>American</li>
<li>Australian</li>
<li>German</li>
</ul>
</li>
</ul>
<ul>
<li>Wine
<ul>
<li>California</li>
<li>Chile</li>
<li>Argentina</li>
<li>France</li>
<li>Germany</li>
<li>Spain</li>
<li>Italy</li>
<li>Portugal</li>
<li>Australia</li>
<li>New Zealand</li>
</ul>
</li>
</ul>
<ul>
<li>Crusine
<ul>
<li>American</li>
<li>French</li>
<li>Chinese</li>
<li>Japanese</li>
<li>Korean</li>
<li>Indian</li>
<li>Indonesian</li>
<li>Thai</li>
</ul>
</li>
</ul>
<ul>
<li>Bread
<ul>
<li>American</li>
<li>French</li>
<li>British</li>
<li>Italian</li>
<li>Mexican</li>
<li>Indian</li>
</ul>
</li>
</ul>
</ul>
</div>
</div>
</div><!-- #End navigation -->
</nav>
...and this is my CSS...
/* 360 Resolution */
#media (max-width: 360px) {
/* Dropdown Button */
.dropbtn {
background-color: blue;
color: white;
margin-top: 25px;
padding: 16px;
font-size: 16px;
font-weight: bold;
text-align: center;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 8px 8px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: orange;
}
.dropdown-content ul li:hover > ul {
display: block;
padding-left: 100px;
left: auto;
}
.dropdown-content li li li {
margin-left: 1em;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
}
Related
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>
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>
I'm trying to create a dropdown menu, but for some reason, I cannot find a way to make its width the same as each <li>in nav.
/* GENERAL STYLES */
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url("../img/paven.png");
}
#font-face {
font-family: "nevis";
src: url("../fonts/nevis.ttf");
}
/* MENU */
nav {
width: 100%;
height: 40px;
background-color: #333;
}
nav > div {
width: 1000px;
margin: 0px auto;
}
nav > div > ul {
float: left;
}
nav > div > ul > li {
list-style: none;
height: 40px;
padding-left: 20px;
padding-right: 20px;
border-right: 1px solid gray;
}
nav > div > ul > li > a {
display: block;
height: 40px;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
color: white;
line-height: 35px;
}
nav > div > ul > li:hover {
cursor: pointer;
background-color: #70B231;
}
/* SUBMENU */
nav > div > ul > li > ul {
position: absolute;
width: 100px;
background-color: #70B231;
}
<!-- MENU -->
<nav>
<div>
<ul>
<li>Home</li>
</ul>
<ul>
<li>Content</li>
</ul>
<ul>
<li>Requirements</li>
</ul>
<ul>
<li>
Languages
<ul>
<li>JavaScript</li>
<li>CSS</li>
<li>HTML</li>
</ul>
</li>
</ul>
<ul>
<li>
Frameworks
<ul>
<li>Less</li>
<li>Sass</li>
<li>Flexbox</li>
</ul>
</li>
</ul>
<ul>
<li>Projects</li>
</ul>
<ul>
<li>Instructor</li>
</ul>
<ul>
<li>Reviews</li>
</ul>
<ul>
<li>Blog</li>
</ul>
<ul>
<li>Contact</li>
</ul>
</div>
</nav>
As you can see, I've tried setting a width for each <ul>, but they are not well aligned to the <li> they belong to:
I would like to achieve something like this:
Here you go, I've edited a few thing.
I removed the padding on the main menu li element and placed it on the a instead so the submenu can take the full width.
Added postition: relative; to the main menu li so the submenu postition: absolute will be relative to the main menu item
Made the submenu full width with width 100% and removed the list styles.
added some padding to the submenu li
/* GENERAL STYLES */
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url("../img/paven.png");
}
#font-face {
font-family: "nevis";
src: url("../fonts/nevis.ttf");
}
/* MENU */
nav {
width: 100%;
height: 40px;
background-color: #333;
}
nav>div {
width: 1000px;
margin: 0px auto;
}
nav>div>ul {
float: left;
}
nav>div>ul>li {
list-style: none;
height: 40px;
position: relative;
border-right: 1px solid gray;
}
nav>div>ul>li>a {
display: block;
height: 40px;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
color: white;
line-height: 35px;
padding-left: 20px;
padding-right: 20px;
}
nav>div>ul>li:hover {
cursor: pointer;
background-color: #70B231;
}
/* SUBMENU */
nav>div>ul>li>ul {
position: absolute;
width: 100%;
background-color: #70B231;
list-style: none;
}
nav>div>ul>li>ul>li {
padding: 3px 10px;
}
<!-- MENU -->
<nav>
<div>
<ul>
<li>Home</li>
</ul>
<ul>
<li>Content</li>
</ul>
<ul>
<li>Requirements</li>
</ul>
<ul>
<li>
Languages
<ul>
<li>JavaScript</li>
<li>CSS</li>
<li>HTML</li>
</ul>
</li>
</ul>
<ul>
<li>
Frameworks
<ul>
<li>Less</li>
<li>Sass</li>
<li>Flexbox</li>
</ul>
</li>
</ul>
<ul>
<li>Projects</li>
</ul>
<ul>
<li>Instructor</li>
</ul>
<ul>
<li>Reviews</li>
</ul>
<ul>
<li>Blog</li>
</ul>
<ul>
<li>Contact</li>
</ul>
</div>
</nav>
I don't know how to really explain this in a simple question for the title, but I have an example page to show you what I mean at http://www.yenrac.net/test
I am having problems with dropdown menus, as you can see. Whenever I hover over to the next category in the dropdown 'accordion' it jumps back and screws with desktop cursors if they have their browser in a small window. Another thing is that mobile users cannot just click the box link again to close the dropdown, which is something else I would like to fix. Can someone please take a look at this and help me figure out what I am doing wrong?
Here is my HTML: http://pastebin.com/RkFs97wH
<nav>
<ul id="navigation">
<li><a id="current" href="index.html">Home</a></li>
<li>FAQ</li>
<li><a class="dropdown" href="#">Dropdown</a>
<ul>
<li>
<a class="pullout" href="#">Pullout Tab</a>
<ul>
<li>Tier 3 Tab</li>
</ul>
</li>
<li>
<a class="pullout" href="#">Pullout Tab 2</a>
<ul>
<li>Tier 3 Tab</li>
</ul>
</li>
</ul>
</li>
<li>Servers</li>
<li id="last">Contact Us</li>
</ul>
</nav>
Here is my CSS: http://pastebin.com/ttYh0Qz6
/* Set to very large range just for convenience of testing */
#media screen and (max-width: 10000px) {
#postdatemeta {
visibility: visible;
}
.postdate {
visibility: hidden;
}
nav ul {
list-style-type: none;
margin: 0 auto;;
padding: 0;
width: 98%;
}
nav li a {
display: block;
color: #fff;
padding: 1em 0;
margin: 0.3em auto;;
text-decoration: none;
text-align: center;
background: #000;
}
nav ul ul {
position: absolute;
visibility:hidden;
}
nav ul li:hover > ul {
visibility: visible;
position: relative;
}
nav ul ul li a {
background: #999;
}
.pullout:after {
content: "\000020\0025BE";
}
I appreciate any help and feedback you are able to provide me!
You are adding a margin to the a element, but the parent li item ignores these margins currently, so when you mouse over the margins, it recognizes you as moving the mouse off of the li, so the :hover state goes away resulting in a collapsed menu.
Try add the following:
nav li {
width: 100%;
display: inline-block;
}
This allows the li element to wrap the a element including the margins, so when they are moused over, the :hover state remains.
#media screen and (max-width: 10000px) {
nav ul {
list-style-type: none;
margin: 0 auto;
;
padding: 0;
width: 98%;
}
nav li {
width: 100%;
display: inline-block;
}
nav li a {
display: block;
color: #fff;
padding: 1em 0;
margin: 0.3em auto;
;
text-decoration: none;
text-align: center;
background: #000;
}
nav ul ul {
position: absolute;
visibility: hidden;
}
nav ul li:hover > ul {
visibility: visible;
position: relative;
}
nav ul ul li a {
background: #999;
}
.pullout:after {
content: "\000020\0025BE";
}
}
<nav>
<ul id="navigation">
<li><a id="current" href="index.html">Home</a>
</li>
<li>FAQ
</li>
<li><a class="dropdown" href="#">Dropdown</a>
<ul>
<li>
<a class="pullout" href="#">Pullout Tab</a>
<ul>
<li>Tier 3 Tab
</li>
</ul>
</li>
<li>
<a class="pullout" href="#">Pullout Tab 2</a>
<ul>
<li>Tier 3 Tab
</li>
</ul>
</li>
</ul>
</li>
<li>Servers
</li>
<li id="last">Contact Us
</li>
</ul>
</nav>
I want to make a submenu for a submenu and also change the background color of main menu and background color on hover of submenu. This is my code, what changes should I make? The green background is not visible in submenu and also the submenu of submenu.
#horizontalmenu ul {
padding: 1;
margin: 1;
list-style: none;
}
#horizontalmenu li {
float: left;
position: relative;
padding-right: 100;
display: block;
border: 4px solid #CC55FF;
border-style: inset;
}
#horizontalmenu li ul {
display: none;
position: absolute;
}
#horizontalmenu li: hover ul {
display: block;
background: red;
height: auto;
width: 8em;
background-color: green;
}
#horizontalmenu li ul li {
clear: both;
border-style: none;
}
Html
<div id="horizontalmenu">
<ul>
<li>
News
<ul>
<li>
National</li>
<li>
International</li>
<li>Sport</li>
<li>Hollybood</li> </ul> </li>
<li> Technology
<ul> <li>IT/Software</li>
<li>Hardware</li>
<li>Iphone
<ul><li>IT/Software</li> </ul></li>
<li>Neuro-Science</li> </ul> </li>
<li> Sports
<ul> <li>Cricket</li>
<li>Tenis</li>
<li>Badminton</li>
<li>Hockey</li> </ul> </li>
<li> Country
<ul> <li>India</li>
<li>America</li>
<li>France</li>
<li>Pakistaan</li>
</ul>
</li>
</ul>
</div>