Display li child items on hover - html

I have the following menu structure:
<li id="nav-menu-item-226" class="menu-item-has-children">
About Us
<ul class="sub-menu dropdown-menu menu-odd menu-depth-2">
<li class="sub-menu-item"><a href="...">...</li>
<li class="sub-menu-item"><a href="...">...</li>
</ul>
</li>
Unordered list has by default display: none.
What I need to get done is that whenever user hovers over parent page link, sub-menu would drop down.
In the best case, I would need to get it done with css only. I have tried many different snippets but none of them helped.

Give id to ul tag and give its style display:none.
then,
in your css
#id ul li:hover > ul { display:block }

This is how I would do it:
.sub-menu {
display:none;
}
.menu-item-has-children:hover > .sub-menu {
display:block;
}
Here is the JSFiddle demo

You could also get it work with jQuery to have a nice slideDown/slideUp effect.
jQuery:
;(function($){
$("#menu li:has(ul)").hover(function(){
$(this).find("ul").not(":animated").slideDown();
}, function(){
$(this).find("ul").not(":animated").slideUp();
});
})(jQuery);
CSS
#menu .sub-menu {
display: none;
}
HTML
<ul id="menu">
<li id="nav-menu-item-221" class="menu-item-has-children">
Home
<li id="nav-menu-item-226" class="menu-item-has-children">
About Us
<ul class="sub-menu dropdown-menu menu-odd menu-depth-2">
<li class="sub-menu-item"><a href="...">...</li>
<li class="sub-menu-item"><a href="...">...</li>
</ul>
</li>
<li id="nav-menu-item-233" class="menu-item-has-children">
Third
</li>
</ul>
Working JSfiddle

All you really need to do is nest the <ul> within your <li> element.
<nav>
<ul>
<li>Link</li>
<li>
Link
<ul>
<li>Submenu</li>
<li>Submenu</li>
</ul>
</li>
<li>Link</li>
</ul>
</nav>
Here's some CSS that will help you get started:
* Resets */
nav a {
text-decoration: none;
font: 12px/1 Verdana;
color: #000;
display: block; }
nav a:hover { text-decoration: underline; }
nav ul {
list-style: none;
margin: 0;
padding: 0; }
nav ul li { margin: 0; padding: 0; }
/* Top-level menu */
nav > ul > li {
float: left;
position: relative; }
nav > ul > li > a {
padding: 10px 30px;
border-left: 1px solid #000;
display: block;}
nav > ul > li:first-child { margin: 0; }
nav > ul > li:first-child a { border: 0; }
/* Dropdown Menu */
nav ul li ul {
position: absolute;
background: #ccc;
width: 100%;
margin: 0;
padding: 0;
display: none; }
nav ul li ul li {
text-align: center;
width: 100%; }
nav ul li ul a { padding: 10px 0; }
nav ul li:hover ul { display: block; }
Preview Demo

Related

how to add submenu inside menu in a navbar in right position

Hello I am trying to add sub-menu inside a menu in right position in HTML. But I am facing an issue in terms of the positioning. I tried to fix that by using full ul and li tags, instead. But still I am facing many issues in terms of sizing, so I decided to go back for an old way. The example is in the link. I am sure I have to change class, but I am almost running out of ideas. So please help me, brothers. Here is the Link.
Please add this code with dropdown and sub menu deropdown
demo link here https://jsfiddle.net/JentiDabhi/5hmgv8h4/
HTML
<ul class="nav-list">
<li class="li-list">Home</li>
<li class="li-list dropdown">
Scoreboard
<ul class="dropdown-content">
<li class="li-list dropdown">
Europe continent
<ul class="dropdown-content">
Deep Menu 2
Deep Menu 2
</ul>
</li>
<li class="li-list">South continent</li>
<li class="li-list"> Asia continent</li>
<li class="li-list">Africa continent</li>
<li class="li-list">Australia continent</li>
<li class="li-list">North continent</li>
</ul>
</li>
<li class="dropdown">
Communities
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
CSS
.nav-list{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.li-list{
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
padding: 9px 12px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #f7ffba;
color:red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #333;
width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
padding: 0;
}
.dropdown-content li{
list-style: none;
width: 100%;
display: inline-block;
}
.dropdown-content a {
color: black;
padding: 4px 6px;
text-decoration: none;
display: block;
text-align: left;
border-bottom: 1px dotted #f7ffba;
/*position:relative;*/
}
.dropdown-content a:hover {background-color: #f7ffba}
.dropdown:hover > .dropdown-content ,
.dropdown-content .dropdown:hover > .dropdown-content {
display: block;
}
.dropdown-content .dropdown > .dropdown-content{
left: 100%;
top: 0;
}
For the sub menus dropdown add the css
.sub_mebu.dropdown-content{left:100%;}
Just add another css rule, that is
.dropdown li.dropdown {
display: inline-block;
float:right;
}
hope it helps
working fiddle
for submenu
CSS
.dropdown-content .dropdown-content
{
left:100%;
}
Hope this helps.
You're using too many unnecessary html tags.
Check my pen https://codepen.io/ery/pen/JJvdQX for more simple dropdown menu.
Hope it helps
/* LEVEL 0 */
.nav ul {
text-align: left;
margin: 0;
padding: 0;
}
.nav li {
display: inline-block;
position: relative;
list-style: none;
}
.nav li > a {
display: block;
line-height: 32px;
background: #666;
color: #FFF;
padding: 0 16px;
text-decoration: none;
}
.nav li:hover > a {
background: #333;
}
/* LEVEL 1 */
.nav ul ul {
position: absolute;
width: 160px;
left: 0;
top: 32px; /* same value with line 13 */
background: #333;
}
.nav li li {display: list-item}
.nav li li a {background: transparent;}
.nav li li a:hover {background: #999; }
/* LEVEL 2 */
.nav ul ul ul {
top: 0;
left: 160px; /* same value with line 28 */
}
/* Dropdown triggers */
.nav ul ul, .nav ul li:hover ul ul, .nav ul ul li:hover ul ul {display: none;}
.nav ul li:hover ul, .nav ul ul li:hover ul, .nav ul ul ul li:hover ul {display: block;}
<div class="nav">
<ul>
<li>menu 01
<ul>
<li>submenu 01
<ul>
<li>sub-submenu 01</li>
<li>sub-submenu 02</li>
</ul>
</li>
<li>submenu 02</li>
<li>submenu 03</li>
</ul>
</li>
<li>menu 02</li>
<li>menu 03</li>
<li>menu 04</li>
</ul>
</div>

How to style more than one ul

I'm using the CSS lines below for a menu on a webpage.
The problem is that I don't know how to make that code apply only to the menu, and not to other ul -unordered lists- on the page!
ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
}
ul li {
font: bold 12px/18px sans-serif;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #AAF7BB;
font-size: 110%;
}
ul li:hover {
background: #ffffff;
color: #000000;
}
ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
opacity: 0;
visibility: hidden;
}
ul li ul li {
background: #ffffff;
display: block;
color: #00ff00;
}
ul li ul li:hover { background: #ffffff; }
ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
Please help.
Try marking your ul as a class and refer to that instead of just ul.
<ul class="menu">
and change all instances of ul in the css to .menu.
Refering to just "ul" will apply the change to all ul elements.
As mentioned you can do this using classes on your html. I would try and Google this a little more. There are tons of example for menus out there.
In the top three is this:
http://www.noupe.com/essentials/freebies-tools-templates/100-great-css-menu-tutorials.html
Something simple:
<ul class="menu">
<li class="menu-option">
Home
</li>
<li class="menu-option active">
About
</li>
<li class="menu-option">
Contact
</li>
<ul class="sub-menu">
<a>Something to click</a>
<li class="menu-option">
Contact
</li>
<li class="menu-option">
Contact
</li>
</ul>
</ul>
Your css would need to be updated similar to below
ul.menu:{
/*Your style for the menu*/
}
ul.menu li.menu-option:{
/*Your style for the menu's options*/
}
ul.menu ul.sub-menu:{
/*Your style for the menu's sub-menu's*/
}
Use the class or id attributes alongside your HTML elements. For example:
HTML
<ul class="class1">
...
</ul>
<ul class="class1">
...
</ul>
<ul id="class2">
...
</ul>
CSS
.class1{
implement css styling here
}
#class2{
implement css styling here
}
Here, the top 2 ul elements will be styled by the .class1 CSS styling, the final ul with the attribute #class2 will be styled by the corresponding CSS.
Note that . denotes classes in CSS and # denotes IDs
IDs are unique, classes are not unique. More information

Dropdown layout issue (dropdown li's width bigger then head li)

I'm having trouble showing a drop down menu in the correct way. This is what I get to see:
This is my HTML code:
<nav class="menuBox">
<ul>
<li>HOME</li>
<li>KLASSEMENTEN</li>
<li>KALENDER</li>
<li>NIEUWS</li>
<li>MEDIA</li>
<li>LINKS</li>
<li>
INLOGGEN
<ul>
<li class="first">
ADMIN
</li>
<li>
EDIT ACCOUNT
</li>
<li class="last">
LOG OUT
</li>
</ul>
</li>
</ul>
</nav>
My CSS code:
/* ### menu Box ### */
.menuBox { position: absolute; top: 74px; right: 2px; }
.menuBox ul { list-style: none; }
.menuBox li { float: left; margin-left: 17px; font-size: 14px; text-transform: uppercase; }
.menuBox li a { color: #3f3f3f; text-decoration: none; display: block; padding-bottom: 14px; }
.menuBox li a:hover { background: url(../images/menu_hover.png) repeat-x 0 bottom; }
.menuBox ul li ul{ display: none; }
.menuBox ul li:hover ul{ display: block; }
I would like to have something like this:
But how can I do this?
add this to your CSS:
.menuBox ul li ul li {
display: block;
float:none;
}
Explanation: you're floating your li elements, but you need to clear the floats for the second level of li elements (those in the sub menus), thus you need to add this declaration.
See fiddle here

I have 2 ULs next to each other, one appears on hover; but cannot move mouse over to it fast enough

I am trying to have a horizontal menu, and when you hover on an item a vertical menu appears below it (menu2). When hovering on an item in menu 2, a third menu appears next to menu2 at the same height as the item being hovered on in menu 2.
http://gcommerce2.gtdsites.com/ (it is the menu I am building right below where it says "home page")
I got it all working. The item you hover on in menu2 to reveal menu3 is "submenu2". The only problem is that when you move the mouse over to try and select one of the items in menu 3, everything disappears before you can get the mouse onto menu 3.
Here is the code:
<style>
nav a {
text-decoration: none;
font: 12px/1 Verdana;
color: #000;
display: block; }
nav a:hover { text-decoration: underline; }
nav ul {
list-style: none;
margin: 0;
padding: 0; }
nav ul li { margin: 0; padding: 0; }
/* Top-level menu */
nav > ul > li {
float: left;
position: relative; }
nav > ul > li > a {
padding: 10px 30px;
border-left: 1px solid #000;
display: block;}
nav > ul > li:first-child { margin: 0; }
nav > ul > li:first-child a { border: 0; }
/* Dropdown Menu */
nav ul li ul {
position: absolute;
background: #ccc;
width: 100%;
margin: 0;
padding: 0;
display: none; }
nav ul li ul li {
text-align: center;
width: 100%; }
nav ul li ul a { padding: 10px 0; }
nav ul li:hover ul { display: block; }
a.menu2:link + ul.menu3 {display: none;}
a.menu2:hover + ul.menu3 {display: inline-block; }
.format_text ul ul {
margin: 0 0 0 .95em;
}
a.menu2, li.menu2 {display: inline-block;}
ul.menu2, ul.menu3 {border: black 2px solid;}
</style>
<nav>
<ul>
<li>Link</li>
<li>
Link2
<ul class="menu2">
<li>Submenu1</li>
<li class="menu2"><a class="menu2" href="#">Submenu2</a>
<ul class="menu3">
<li><a class="menu3" href="#">gmenu1</a></li>
<li>gmenu22</li>
<li>gmenu3</li>
</ul>
</li>
<li>Submenu3</li>
</ul>
</li>
<li>Link3</li>
</ul>
</nav>
<br style="clear:both;"/>
Can Anyone help with this?
Take a look at the http://jqueryui.com/menu/ plugin. It's much easier than trying to code your own menu structure
example http://jsfiddle.net/h7N5R/
You want to be targeting hover on the list item not the anchor tag
Change
a.menu2:hover + ul.menu3 {display: inline-block; }
To
li.menu2:hover > ul.menu3 {display: inline-block; }
See: http://jsfiddle.net/VdqnD/

Display a ul class when the mouse hovers over a li class css only

I am currently looking into developing a CSS only drop down menu. The idea is when the mouse hovers over a ul tag it will make another ul class appear.
Below is the code that I currently have.
HTML
<head>
<link href="nav.css" rel="stylesheet" type="text/css">
</head>
<ul class="nav">
<li class="topMenu">Home</li>
<ul class="subMenu" id="home">
<li>Hello</li>
<li>World</li>
</ul>
</ul>
CSS
.nav, .topMenu, .subMenu
{
position: relative;
list-style: none;
}
.topMenu
{
position: relative;
float: left;
}
.subMenu
{
display: none;
}
.topMenu a:hover + li
{
display: block;
background-color: blue;
}
The idea is that when the mouse hovers over the li class="topMenu" then the ul class="subMenu" id="home" should appear underneath.
Ideally this should be only in a CSS format without requiring any javascript etc.
Thanks for any help you can provide.
All you really need to do is nest the <ul> within your <li> element.
<nav>
<ul>
<li>Link</li>
<li>
Link
<ul>
<li>Submenu</li>
<li>Submenu</li>
</ul>
</li>
<li>Link</li>
</ul>
</nav>
Here's some CSS that will help you get started:
/* Resets */
nav a {
text-decoration: none;
font: 12px/1 Verdana;
color: #000;
display: block; }
nav a:hover { text-decoration: underline; }
nav ul {
list-style: none;
margin: 0;
padding: 0; }
nav ul li { margin: 0; padding: 0; }
/* Top-level menu */
nav > ul > li {
float: left;
position: relative; }
nav > ul > li > a {
padding: 10px 30px;
border-left: 1px solid #000;
display: block;}
nav > ul > li:first-child { margin: 0; }
nav > ul > li:first-child a { border: 0; }
/* Dropdown Menu */
nav ul li ul {
position: absolute;
background: #ccc;
width: 100%;
margin: 0;
padding: 0;
display: none; }
nav ul li ul li {
text-align: center;
width: 100%; }
nav ul li ul a { padding: 10px 0; }
nav ul li:hover ul { display: block; }
Preview: http://jsfiddle.net/Wexcode/BEhvQ/
It will need some tweaking:
<ul class="nav">
<li class="topMenu">Home</li>
<li class="subMenu">
<ul id="home">
<li>Hello</li>
<li>World</li>
</ul>
</li>
</ul>
.topMenu:hover + .subMenu
{
display: block;
background-color: blue;
}
Demo