I'm working on an ebay shop. I need to make categories collapsible after I click the root item (if that is even possible) following ebay's guidelines.
Rules:
Ebay doesn't want us to use active content, that means no JavaScript, flash.
It only allows us to use CSS3.
We can't even change the output HTML for categories. So only option available is CSS.
HTML:
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
Nice question! Because of this, I just discovered the magic of :focus-within.
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
Then here comes the magic:
li + .lev2 {
display: none;
}
li:focus-within + .lev2 {
display: block;
}
Check it out in a fiddle
So after digging into this subject, your request seems impossible with this markup. BUT with minor adjustments to the html you provide, you can achieve it with this minimal style:
<style>
.lev1 li { display:block; }
.lev1 ul { display:none; }
.lev1 ul:target { display:block; }
</style>
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul id="lev2a">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul id="lev2b">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul id="lev2c">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
*Minor adjustments = change ul's classes to an unique id's. That way the :target pseudo element can affect it.
Hope it solve it, and good luck!
Here is one example on how to do this. Whilst this shows expanding menus, it also has some visual effects. Try it out and adapt yourself. The code itself is from http://www.dynamicdrive.com/
<style>
.bgslidemenu {
font: bold 16px 'Bitter', sans-serif; /* use google font */
position: relative;
width: 100%;
}
.bgslidemenu:after { /* clear menu */
content: '';
clear: both;
display: block;
}
/* Top Level Menu */
.bgslidemenu ul {
z-index: 100;
margin: 0;
padding: 0;
position: relative;
list-style: none;
float: right; /* change to "left" to left align menu */
}
/* Top level list items */
.bgslidemenu ul li {
position: relative;
display: inline;
margin-right: 20px; /* spacing between each top level menu item */
float: left;
}
/* Top level menu items link style */
.bgslidemenu ul li a {
display: block;
position: relative;
color: white;
padding: 14px 10px;
color: black;
text-decoration: none;
}
.bgslidemenu ul li a:link, .bgslidemenu ul li a:visited {
color: black;
}
/* Top level menu items link style on hover */
.bgslidemenu ul li:hover > a {
color: purple !important;
}
/* LIs links with a sub UL style */
.bgslidemenu ul li > a {
/* add padding to accomodate arrow inside LIs */
padding-right: 25px;
}
/* LIs links with NO sub UL style */
.bgslidemenu ul li > a:only-child {
/* undo padding for non submenu LIs */
padding-right: 10px;
}
/* LIs links with a sub UL pseudo class (Add down arrow) */
.bgslidemenu ul li > a:after {
/* add arrow inside LIs */
content: "";
position: absolute;
height: 0;
width: 0;
border: 5px solid transparent;
border-top-color: black;
top: 50%;
transform: translateY(-20%);
right: 8px;
}
/* LIs links with NO sub UL pseudo class */
.bgslidemenu ul li > a:only-child:after {
/* undo arrow for non submenu LIs */
display: none;
}
/* Sub ULs style */
.bgslidemenu ul li ul {
position: absolute;
left: -5000px;
top: auto;
opacity: 0;
width: 200px; /* width of drop down menus */
visibility: hidden;
padding-top: 80px; /* Add large top padding to drop down menu */
z-index: -1;
background: #F3F3F3;
transform: translateY(100px);
-webkit-transition: opacity .3s, transform .5s, visibility 0s .3s, left 0s .3s;
transition: opacity .3s, transform .5s, visibility 0s .3s, left 0s .3s;
}
/* Sub UL style on hover */
.bgslidemenu ul li:hover > ul {
visibility: visible;
left: -30px;
transform: translateY(-80px); /* move drop down menu upwards (should be smaller than padding-top value above) */
opacity: 1;
-webkit-transition: opacity 1s, transform .5s;
transition: opacity 1s, transform .5s;
}
/* Sub level Menu list items (alters style from Top level List Items) */
.bgslidemenu ul li ul li {
display: list-item;
float: none;
overflow: hidden;
}
/* Add animated line to sub menu item on Mouseover */
.bgslidemenu ul li ul li:after {
content: '';
position: absolute;
width: 30px;
height: 5px;
background: purple;
left: 0;
top: 50%;
transform: translate3d(-100%, -50%, 0);
transition: transform .3s;
}
.bgslidemenu ul ul li:hover:after {
transform: translate3d(0, -50%, 0);
}
/* Sub Levels link style on hover and when active */
.bgslidemenu ul ul li:hover > a {
color: purple !important;
}
/* Sub level menu links style */
.bgslidemenu ul li ul li a {
font: normal 16px 'Bitter', sans-serif;
padding: 10px;
padding-left: 40px; /* Add left padding to sub menu links to accommodate animated line */
position: relative;
margin: 0;
}
/* ####### responsive layout CSS ####### */
#media (max-width: 700px) {
/*
For mobile and screen browser windows
Get Sub ULs to expand entire width of document and drop down
Changes menu reveal type from "visibility" to "display" to overcome bug. See comments below
*/
.bgslidemenu ul {
float: none;
}
.bgslidemenu ul li {
position: relative;
display: block;
width: 100%;
}
.bgslidemenu ul li ul {
width: 100%;
position: relative;
border-top: 2px solid #eee;
/* change menu reveal type from "visibility" to "display". Former seems to cause browser to jump to top of page sometimes when menu header is tapped on */
display: none;
}
.bgslidemenu ul li:hover > ul {
display: block;
padding-top: 0;
transform: none;
z-index: 10000;
left: 0 !important;
top: auto;
}
.bgslidemenu ul ul li:hover > ul {
left: 0 !important;
top: auto;
}
}
</style>
<div class="bgslidemenu">
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>Sub Item 1.1</li>
<li>Sub Item 1.2</li>
<li>Sub Item 1.3</li>
<li>Sub Item 1.4</li>
<li>Sub Item 1.2</li>
<li>Sub Item 1.3</li>
<li>Sub Item 1.4</li>
</ul>
</li>
<li>Item 3</li>
<li>Item 4
<ul>
<li>Sub Item 3.1</li>
<li>Sub Item 3.2</li>
</ul>
</li>
<li>Home</li>
</ul>
</div>
Related
I am trying to make a nice transition for my site's menu: when the user hovers over the parent item, the children slide down and when the mouse leaves, they should go back up. But they don't, as seen in this fiddle. Then I asked for help here on SO and was pointed to using the overflow: hidden; approach and only transition the height. Okay. But that hid my child item's children items as seen in the snippet below:
ul {
list-style: none;
padding: 0;
}
li {
width: 180px;
}
#menu ul ul {
position: absolute;
overflow: hidden;
background: red none repeat scroll 0 0;
}
#menu ul li {
position: relative;
}
#menu ul ul li {
height: 0;
transition: height 0.2s ease 0s;
}
#menu ul ul ul {
margin-left: 100%;
top: 0;
}
#menu ul li:hover>ul>li {
height: 32px;
}
<nav id="menu">
<ul>
<li>Parent Item
<ul>
<li>Child Item</li>
<li>Child Item >
<ul>
<li>Child Child Item 1</li>
<li>Child Child Item 2</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
So basically the "Child Child Item 1" and "Child Child Item 2" items are invisible to the user. How can I have both the slide down/up transition on mouse hover/leave and display both child items? Can it be done in pure CSS?
You can set an initial opacity on the li to 0, in addition to a height of 0.
On :hover, change the opacity to 1. Note that I haven't included opacity in the transition effect. I think it may look better for it to change immediately.
updated
Instead of using opacity, I think setting the initial font-size to 0 and scaling up will produce a smoother effect. I've added classes to the triggers and menus for clarity.
* {
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
width: 180px;
}
#menu ul ul {
position: absolute;
background: red none repeat scroll 0 0;
}
#menu ul li {
position: relative;
}
.dropdown-one li,
.dropdown-two li {
height: 0;
font-size: 0;
transition: height 0.2s ease 0s;
}
#menu ul ul ul {
margin-left: 100%;
top: 0;
}
.dropdown-trigger-one:hover .dropdown-one>li,
.dropdown-trigger-two:hover .dropdown-two>li {
height: 32px;
font-size: 1em;
}
<nav id="menu">
<ul>
<li class="dropdown-trigger-one">Parent Item
<ul class="dropdown-one">
<li>Child Item</li>
<li class="dropdown-trigger-two">Child Item >
<ul class="dropdown-two">
<li>Child Child Item 1</li>
<li>Child Child Item 2</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
I am using the code of this page:
https://codepen.io/shshaw/pen/gsFch
Now, my CSS looks like:
.subs {
visibility: hidden;
opacity: 0;
transform: translateY(-2em);
z-index: -1;
transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
width: auto;
float: left;
position: absolute;
text-align: left;
border: 2px solid #66ec95;
background: #f4f7f5;
border-radius: 0 5px 5px 5px;
margin-top: -10px;
margin-left: -10px;
}
/* SELECTORS */
#nav li:hover > .subs { /* with this seletor, only the last property is working */
visibility: visible;
opacity: 1;
z-index: 1;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s;
margin-top: 10px; /* just for test, only this is working */
}
#nav li :hover + .subs { /* this time, the animation are working but other problems occured (see above) */
visibility: visible;
opacity: 1;
z-index: 1;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s;
}
My HTML:
<ul id="nav">
<li aria-haspopup="true">
Services
<div class="subs" id="service-subs" aria-haspopup="false">
Product List<br />
Projects<br />
</div>
</li>
... and so on
So, the second selector for .subs displays the animation but it breaks when I move my cursor into .subs and it's also interrupted many times on displaying because of cursor detects .subs while showing the menu.
Furthermore, I can't understand why does it work with sibling selector when .subs is a child of #nav li.
What am I doing wrong?
Thanks!
I am not 100% sure on the issue of why your code wasn't working. I did re-write it to try to get what you were going for using some methods I think are easier to work with.
Utilizing #keyframes is a great way to build your application with reusable chunks of code.
Utilizing translate3d will tap into the GPU of a computer/device for smoother translations.
Please note I did not take the time to add an HTML wrapper that would hide the drop down menus as they fall. This would easily be done by wrapping the entire menu in a div and setting the overflow to hidden.
I apologize I couldn't help you learn more of why you were getting the bug you had.
.sub-menu-parent {
position: relative;
}
.sub-menu {
position: absolute;
width: 100%;
display: none;
z-index:-1;
}
.sub-menu-parent:hover .sub-menu {
display: block;
animation: 1s slideDown forwards;
}
#keyframes slideDown {
0% {
transform: translate3d(0px, -200px, 0px);
opacity: 0;
z-index:-1;
}
99% {
transform: translate3d(0px, 0px, 0px);
opacity: 1;
z-index:-1;
}
100% {
z-index:0;
}
}
/* presentational */
body {
font: 18px/1.4 sans-serif;
}
nav a {
color: #E00;
display: block;
padding: 0.5em 1em;
text-decoration: none;
}
nav a:hover {
color: #F55;
}
nav ul, nav ul li {
list-style-type: none;
padding: 0;
margin: 0;
}
nav > ul {
background: #EEE;
text-align: center;
}
nav > ul > li {
display: inline-block;
border-left: solid 1px #aaa;
}
nav > ul > li:first-child {
border-left: none;
}
.sub-menu {
background: #DDD;
}
<nav>
<ul>
<li class="sub-menu-parent">
Menu Item 1
<ul class="sub-menu">
<li>Sub Item 1</li>
<li>Sub Item 2</li>
<li>Sub Item 3</li>
<li>Sub Item 4</li>
</ul>
</li>
<li class="sub-menu-parent">Menu Item 2
<ul class="sub-menu">
<li>Sub Item 1</li>
<li>Sub Item 2</li>
<li>Sub Item 3</li>
<li>Sub Item 4</li>
<li>Sub Item 5</li>
<li>Sub Item 6</li>
</ul>
</li>
<li class="sub-menu-parent">Menu Item 3
<ul class="sub-menu">
<li>Sub Item 1</li>
<li>Sub Item 2</li>
</ul></li>
</ul>
</nav>
I'm trying to create a nested dropdown menu that may potentially be very long and overflow off the page.
What I'd like to do is, when the menu is too long it will display a scroll bar. I'm doing this with overflow: auto. However, when I do this, it traps any submenus within the same 'scroll space' as defined by the first scroll bar.
I've also tried various iterations of overflow: none with the :not(:hover) selector, but nothing I've tried seems to work.
What I'd like it to do is show the scrollbar on each level, only if necessary (i.e. that submenu would scroll off the page). Each submenu should 'pop' out of the previous scroll bar, if any, as if it was not there.
I'd like to do this in all CSS, but I'm open to a JS solution as well.
I have a code pen showing the issue here:
https://codepen.io/mcmurphy510/pen/ZyGLKd
I'm not sure if I'm understanding your question correctly, but try isolating your desired element by using ID or CLASS. See the third level menu.
#primary_nav_wrap {
margin-top: 15px
}
#primary_nav_wrap ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0;
}
#primary_nav_wrap ul a {
display: block;
color: #333;
text-decoration: none;
font-weight: 700;
font-size: 12px;
line-height: 32px;
padding: 0 15px;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0;
}
#primary_nav_wrap ul li.current-menu-item {
background: #ddd
}
#primary_nav_wrap ul li:hover {
background: #f6f6f6
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0;
}
#primary_nav_wrap ul ul li {
float: none;
width: 200px
}
#primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px
}
#primary_nav_wrap ul ul ul {
top: 0;
left: 100%
}
#primary_nav_wrap ul li:hover > ul {
display: block;
height: 200px;
}
#primary_nav_wrap ul li ul li:not(:hover) {
}
/* ul li ul li ul li {
overflow: auto;
} */
#subdeep {
overflow: auto;
height: 50px !important;
}
<h1>Simple Pure CSS Drop Down Menu</h1>
<nav id="primary_nav_wrap">
<ul>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul id="subdeep">
<li>Sub Deep 1</li>
<li>Sub Deep 2</li>
<li>Sub Deep 3</li>
<li>Sub Deep 4</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</li>
</ul>
</nav>
Probably you could use the proposed solution as the elements are positioned relative to each other and therefore the menu can set up some branches, you would "just" require to ensure that the parent element(s) remain visible
Mouse over on item "Link 3" will shows its sub-menu on the right side of it and then mouse over on "Link 31" for further sub menu.
.menu {
position: relative;
}
ul {
width: 200px;
margin: 0;
color: black;
list-style:none;
padding:0;
max-height:100px;
overflow-x: hidden;
overflow-y: auto;
}
li {
padding:0.5em;
}
li:hover{
background-color:blue;
color:white;
}
li .menu {
position: absolute;
z-index: 10;
background-color:lightgrey;
opacity: 0;
transition: opacity 0.5s;
}
li:hover > .menu,
.menu:hover {
opacity: 1;
}
li.parent {
cursor: pointer;
}
.level2 {
top: 0px;
left: 200px;
}
<div class="menu">
<ul>
<li>Link1</li>
<li class="parent">Link3...
<div class="menu level2">
<ul>
<li class="parent">Link31...
<div class="menu level2">
<ul>
<li>Link 311</li>
<li>Link 312</li>
<li>Link 313</li>
<li>Link 314</li>
</ul>
</div>
</li>
<li>Link 32</li>
<li>Link 33</li>
<li>Link 34</li>
</ul>
</div>
</li>
<li>Link2</li>
<li>Link1</li>
<li>Link2</li>
</ul>
</div>
I have a CSS dropdown navigation menu with a submenu. My problem is that the submenu closes before I click on it. When I move the cursor halfway to the submenu, it disappears. For example when I move my mouse to click on "Item 3 sub 1.1", it disappears.
The main dropdown menu works fine but the submenu is the problem.
What can I do to make the submenu stay until I click on it?... Thanks for the help
This is my CSS for the menu:
.nav {
padding: 0px;
text-align: center;
border: 0px;
vertical-align: middle;
display: table-row;
width: 100%;
margin: 0px auto;
background-color:#660000;
overflow: hidden;
position:relative;
height: 30;
text-align: center;
margin: 0px auto 0px auto;
}
ul {
list-style: none;
padding: 0px;
margin: 0px;
float:left;
display:inline;
}
ul li {
display:block;
position: relative;
float: left;
left: 85px;
}
li ul {
display: none;
margin:0;
}
ul li a {
display: block;
background: #660000;
padding: 5px 10px 5px 10px;
text-decoration: none;
white-space: nowrap;
color: #fff;
border-left:1px solid #660000;
border-right:1px solid #660000;
}
ul li a:hover {
background: #3300cc;
display: block;
}
li:hover ul {
position: fixed;
display: block;
}
li:hover li {
float: none;
}
li:hover a {
background: #3300cc;
}
li:hover li a:hover {
background: #660000;
}
.drop-nav li ul li {
border-top: 0px;
z-index: 100;
border-bottom:0;
right:0;
left:0;
}
li li:hover a {
display: block;
}
li li ul a {
margin-top:-25px;
margin-bottom: 25px;
margin-left: 85px;
display: none;
}
li:hover li:hover ul li a:hover {
margin-top:-25px;
margin-bottom:25px;
margin-left: 85px;
overflow: none;
}
This is the html:
<div class="nav" >
<ul class="drop-nav">
<li>Item 1</li>
<li>Item 2
<ul>
<li>Item 2 sub 1</li>
<li> Item 2 sub 2</li>
<li > Item 2 sub 2</li>
</ul>
</li>
<li> Item 3
<ul>
<li> Item 3 sub 1 »
<ul>
<li> Item 3 sub 1.1</li>
<li> Item 3 sub 1.2</li>
<li > Item 3 sub 1.3</li>
</ul>
</li>
<li> Item 3 sub 2 »
<ul>
<li> Item 3 sub 2.1 </li>
<li> Item 3 sub 2.2 </li>
<li> Item 3 sub 2.3 </li>
</ul>
</li>
<li> Item 3 sub 3 </li>
<li> Item 3 sub 4 </li>
<li> Item 3 sub 5 </li>
</ul>
</li>
</ul>
</div>
Your css selectors are very confusing. Only using ul li and then li ul or li li ul a on the next line is too vague and not really targeting an element. While this approach might work for nesting of one level deep, it seriously get's confusing (for the browser) when you start nesting 2 level deep.
I'd give each UL element a certain, descriptive class, which you can precisely target.
Additonally, make use of the position attribute for your UL and don't forget to think about z-index when menus overlap.
Something like this, not very pretty, but it works the way you propably expect.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.nav {
position: relative;
top: 0;
width: 80%;
height: auto;
border: 1px solid #e6e6e6;
margin: 0 auto;
font-family: 'Helvetica', Arial, sans-serif;
}
/* Root Level */
.dropdown-menu {
position: relative;
top: 0;
left: 0;
padding: 0;
margin: 0;
background: #eeeeee;
}
/* Clear the float */
.dropdown-menu:after {
display: table;
content: "";
clear: both;
}
.dropdown-menu li {
display: block;
border-left: 1px solid #e6e6e6;
border-right: 1px solid #e6e6e6;
min-width: 150px;
float: left;
}
.dropdown-menu li a:link,
.dropdown-menu li a:visited {
padding: 10px 15px;
text-decoration: none;
color: #111111;
display: block;
}
.dropdown-menu li a:hover,
.dropdown-menu li a:focus {
color: #222222;
}
/* Level 1 */
.dropdown-level-1 {
position: absolute;
padding: 0;
margin: 0;
display: none;
background: grey;
width: 150px;
}
.dropdown-level-1 li {
}
/* The ">" triggers the direct child to show up as "block" */
.dropdown-menu li:hover > .dropdown-level-1 {
display: block;
}
/* Level 2 */
.dropdown-level-2 {
position: absolute;
display: none;
background: #888888;
padding: 0;
margin: -38px 0 0 148px;
}
/* same trick here for level 2 */
.dropdown-level-1 li:hover > .dropdown-level-2 {
display: block;
}
/* Target all links at once */
.dropdown-level-0 li a:link,
.dropdown-level-0 li a:visited,
.dropdown-level-1 li a:link,
.dropdown-level-1 li a:visited,
.dropdown-level-2 li a:link,
.dropdown-level-2 li a:visited {
padding: 10px;
color: purple;
display: block;
}
.dropdown-level-0 li a:hover,
.dropdown-level-0 li a:focus,
.dropdown-level-1 li a:hover,
.dropdown-level-1 li a:focus,
.dropdown-level-2 li a:hover,
.dropdown-level-2 li a:focus {
color: orange;
}
</style>
</head>
<body>
<div class="nav">
<ul class="dropdown-menu">
<li>Item 1</li>
<li class="dropdown">Item 2
<ul class="dropdown-level-1">
<li>Item 2 sub 2
<ul class="dropdown-level-2">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2</li>
</ul>
</li>
<li>Item 2 sub 2
<ul class="dropdown-level-2">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2</li>
</ul>
</li>
<li>Item 2 sub 2
<ul class="dropdown-level-2">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">Item 2
<ul class="dropdown-level-1">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2
<ul class="dropdown-level-2">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">Item 2
<ul class="dropdown-level-1">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2
<ul class="dropdown-level-2">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li>Item 2 sub 2</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">Item 2
<ul class="dropdown-level-1">
<li>Item 2 sub 1</li>
<li>Item 2 sub 1</li>
<li> Item 2 sub 2</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
I'm trying to add a "sub menu" to a drop down menu. Say I wanted to add a sub menu to Item 3 (see html), how would I go about doing that?
Thanks,
Here's my CSS:
.nav_menu {
width:100%;
background-color:#EFEFEF;
z-index:2000;
border:1px solid #ccc;
}
.selected {
background-color:#ccc;
color:#333;
}
.nav_menu a:link {
color:#007dc1;
}
.nav_menu a:visited {
color:#007dc1;
}
.nav_menu a:hover {
color:#007dc1;
}
.nav_menu ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
}
.nav_menu ul li {
font-size:16px;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 8px 22px;
font-weight:600;
transition: all 50ms linear;
transition-delay: 0s;
}
.nav_menu ul li ul {
padding: 0;
position: absolute;
top: 37px;
left: 0;
width: 230px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
display: none;
opacity: 0;
visibility: hidden;
display: block;
opacity: 0;
-webkit-transition: opacity .2s;
z-index:50000;
}
.nav_menu ul li ul li {
background-color:#535353;
border-top:1px solid #fff;
display: block;
font-size:12px;
color:#fff;
}
.nav_menu ul li ul li:hover {
background: #B2B2B2;
}
.nav_menu ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
Here's my HTML:
<ul>
<li>All Items
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 with Sub Menu</li>
</ul>
</li>
</ul>
Firstly, since your menu is based simply on the CSS :hover pseudo-class, make sure that your ul and li elements do not have any space between them, because this will lead to the entire menu dissapearing.
The HTML code
<div class='nav_menu'>
<ul>
<li>All Items
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li class='nav_menu_sub'>Item 3 with Sub Menu
<ul>
<li>SubItem 3.1</li>
<li>SubItem 3.2</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Just like the drop down that you already provided, simply adding a ul element within the li element should suffice to create the sub menu. I added a nav_menu_sub class to the li that opens the sub menu making it easier to select via CSS (avoiding .nav_menu ul li ul li).
The CSS code
.nav_menu_sub {
padding:0;
margin:0;
}
.nav_menu_sub ul {
margin-top:-7px;
display: none !important;
}
.nav_menu_sub:hover ul {
display: block !important;
opacity: 1;
visibility: visible;
}
The margin-top:-7px on the ul element was added to ensure that it fits nicely up against the li.
Add the !important to the display attribute to get it overwrite the previously declared styling.
Working jsFiddle: http://jsfiddle.net/akhrbkug/
Judging from the css you posted:
.nav_menu ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
It looks like you have to add another ul in the submenu li:
<ul>
<li class='nav-menu'>All Items
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 with Sub Menu
<ul>
<li>SubItem 3.1</li>
<li>SubItem 3.2</li>
</ul>
</li>
</ul>
</li>
</ul>
Fiddle for the demo
http://jsfiddle.net/ee9ebv2s/