CSS Multi-column Dropdown Menu not Behaving - css

I have a horizontal hover dropdown menu working almost flawlessly. The goal is if I hover over a Link, I will see a row of Sub Links. Each row has X amount of Child Links that should display down one column. However, currently the Child Links display across in a row not down the column under their Sub Link.
HTML:
<div class="menu">
<ul>
<li><a href='#'>Link 1</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<ul>
<li><a href='#'>Sub Child Link 1</a></li>
<li><a href='#'>Sub Child Link 2</a></li>
<li><a href='#'>Sub Child Link 3</a></li>
</ul>
<li><a href='#'>Sub Link 2</a></li>
</ul>
</li>
<li><a href='#'>Link 2</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
<li><a href='#'>Link 3</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
</ul>
CSS:
.menu{
border:none;
border:0px;
margin:0px;
padding:0px;
border-bottom: 1px solid #ccc;
}
.menu ul{
background:#fff;
height:35px;
list-style:none;
margin:0;
padding:0;
}
.menu li{
float:left;
padding:0px;
}
.menu li a{
background:#fff;
color:#000;
display:block;
line-height:35px;
margin:0px;
padding:0px 25px;
text-align:center;
text-decoration:none;
text-transform: uppercase;
}
.menu li a:hover {
border-bottom: 1px solid #ff0000;
}
.menu li a:hover, .menu ul li:hover a{
background: #fff;
text-decoration:none;
}
.menu li ul{
background:#fff;
display:none;
height:auto;
padding:0px;
margin:0px;
border:0px;
position:absolute;
width:400px;
z-index:200;
}
.menu li:hover ul{
display:block;
}
.menu li li {
background: #fff;
display:block;
float:left;
margin:0px;
padding:10px;
width:100px;
}
.menu li:hover li a{
background:none;
}
.menu li ul a{
display:block;
height:30px;
font-size:12px;
font-style:normal;
color: #999;
margin:0px;
padding:0px 10px 0px 15px;
text-align:left;
text-transform: uppercase;
}
.menu li ul a:hover, .menu li ul li:hover a{
background:#fff;
border:0px;
text-decoration:none;
}
.menu p{
clear:left;
}

Okay, so the answer to your question comes in a few parts. All of it is available in this fiddle. Here goes.
The Markup
First, it's easier to deal with things if a sub-menu is nested in it's link's parent li. Also (side note) if your css fails for whatever reason, this will preserve the parent-child relationships. The new basic structure looks like this:
<div class="menu">
<ul>
<li>
<a href='#'>Link</a>
<ul>
<li>
<a href='#'>Child</a>
<ul>
<li><a href='#'>Grandchild</a></li>
</ul>
</li>
</ul>
<li>
</ul>
</div>
The Styles
In the fiddle, I have the styles separated into a couple parts, and I'll go over each.
The Reset
This is pretty straight forward. It just resets everything. It's not relevant to the solution, though, really, so I've left it out of the answer. It's in the fiddle if you need it.
The Flavor
In the fiddle, the flavor is just my own personal taste. It's mostly colors, but there is one important bit. We apply the padding to the a elements. This'll help things line up properly and will also provide as a side-effect a nice big clickable area.
.menu a {
display: block;
padding: 10px; /* padding on the a */
color: #000;
}
The Positioning
This is the real core of the answer. What we're doing is what you've already done, except we're un-setting the float on the third-level li. The short answer is just that line, honestly.
.menu li {
position: relative;
float: left;
}
.menu li ul { position: absolute; width: 400px; }
.menu li li ul { width: 120px; }
.menu li li li { float: none; } /* this lets columns happen again */
The Hover
To accomplish the hiding in a simpler way, we'll employ the use of the child selector (>). If you've never used it, Chris Coyier explains it very well, but basically it only gets immediate children. That way we can say any ul nested directly under an li should be displayed only if that li is being hovered. We do so like this:
.menu li > ul { display: none; }
.menu li:hover > ul { display: block; }
Edit: The MDN also has a great entry on the child selector. Check it out here.
The Demo
Again, the whole thing is demonstrated in a fiddle at http://jsfiddle.net/4tPcL/. Hope this helps!

Related

chrome nested navigation drop down hover issue

Chrome version: 41.0.2272.101 m
I have a list that is displayed horizontally, inside one of the list items I have some text and another list that is displayed vertically when the hover effect is active.
This works in IE and firefox without any issue & sometimes chrome.
When the developer tools plugin is open, it works, and when another tab is open it works.
I am pretty sure there is no issue with chrome, or my virtual server. I am confident that the issue is in my code.
<div id='unity'>
<div id='nav'>
<ul>
<li>
<a href='#'>Home</a>
</li>
<li>
<a href='#'>Team</a>
</li>
<li>
<a href='#'>About us</a>
</li>
<li id='nolink' class='drop'>
Service
<ul>
<li>
<a href='#'>Some awesome service</a>
</li>
<li>
<a href='#'>Some awesome service</a>
</li>
<li>
<a href='#'>Some awesome service</a>
</li>
<li>
<a href='#'>Some awesome service</a>
</li>
<li>
<a href='#'>Some awesome service</a>
</li>
</ul>
</li>
<li>
<a href='#'>Portfolio</a>
</li>
<li>
<a href='#'>Newsroom</a>
</li>
<li>
<a href='#'>Upcomming events</a>
</li>
<li>
<a href='#'>Contact us</a>
</li>
</ul>
</div>
html, body{
margin:0;
background:#ebebeb;
font-family: 'Roboto', sans-serif;
}
#unity{
width:1000px;
margin:0 auto;
background:white;
position:relative;
padding-top:35px;
position:relative;
}
#nav{
position:fixed;
top:0;
height:35px;
width:1000px;
background:rgba(255,255,255,0.9);
z-index:2;
}
#nav > ul{
margin:0;
padding:0;
font-size:0;
}
#nav > ul > li{
list-style-type: none;
display: inline-block;
font-size:12px;
}
#nav > ul > li{
}
#nav > ul > li > a, #nav > ul > li#nolink{
display:inline-block;
line-height: 35px;
height:35px;
padding:0 10px;
text-decoration: none;
color:#1c1c1c;
cursor:pointer;
}
#nav > ul > li > a:hover, #nav > ul > li#nolink:hover{
color:white;
background:#1c1c1c;
}
#nav > ul > li > ul > li{
display:block;
}
#nav > ul > li.drop > ul{
display:none;
background: #1c1c1c;
padding:0;
}
#nav > ul > li.drop:hover > ul{
display:block;
position:absolute;
margin-left:-10px;
border-radius: 1px;
}
#nav > ul > li.drop > ul > li{
text-align: center;
color:white;
line-height: 30px;
height:30px;
}
#nav > ul > li.drop > ul > li > a{
color:white;
display: block;
text-decoration: none;
padding:0 25px;
}
#nav > ul > li.drop > ul > li:hover > a{
color:#1c1c1c;
background:white;
}
#nav > ul > li.drop > ul > li, #nav > ul > li.drop > ul > li > a{
}
this bug also not happening in jsfiddle.
https://jsfiddle.net/575hbxzb/
youtube video:
https://www.youtube.com/watch?v=4XK5DprGr6k&feature=youtu.be
same issue here with a different chrome version 41.0.2272.118 m.
We use the same technique with our menu and in the last few month this issue was appear on chrome only.
If you open new tab or open debugging tools or resize the window the problem disappear.
I've found only a workaround: it is to enlarge the height of the top <li> so the child menu element and the <li> will overlap.
In this way the menu works but if the overlap area is too small and you move the mouse too fast the issue will appear again.
I think this is a chrome issue because some month ago we don't have any problem with our menu and all other browsers work well.
Hope this help.
I have the same problem and my workaround solution: add a margin-top:15px; to my header, is very ugly especily if the layout requires the menu at the first pixels but works for me until Google fix this bug.

css dropdown menu glitch

i recently changed the design of this webpage but now my dropdown menu lists the "hidden" items vertically instead of horizontally even though i use the inline-block display option.
this is the html code from my menu:
<ul id="menu">
<li id="bordered">News</li>
<li id="bordered">About</li>
<li id="bordered">Research </li>
<li id="bordered">Artists
<ul>
<li id="bordered">1234 1234</li>
<li id="bordered">1234 1234</li>
</ul></li>
<li>Municipalities
<ul>
<li id="bordered">1234567</li>
<li id="bordered">1234 </li>
</ul></li>
<li>
<form id="searchbox" action=" " method=" ">
<input type="text" name="search" value="" />
<input type="submit" value="Search" />
</form>
</li>
</ul>
and this is the CSS:
/*header navigation menu*/
/*STYLING*/
#menu{
margin-top:2px;
font-size:0.8em;
list-style:none;
}
#menu li{
padding:1% 1.5% 1% 1.5%;
display:inline-block;
text-align:center;
vertical-align:middle;
}
#menu li a{
text-align:center;
vertical-align:middle;
font-weight:bold;
}
#bordered{
border-right:1px dotted #f9f9f3;
}
/*DROPDOWN*/
#menu li ul{
visibility:hidden;
position:absolute;
margin:1% 0 0 -2px;
border-top: 1px dotted #f9f9f3;
}
#menu li ul li{
float:left;
white-space:nowrap;
}
#menu li:hover ul, #menu li:active ul{
visibility:visible;
}
help would be very much appreciated :)
You should apply a float:left on #menu li instead of a display:inline-block
It fixes your problem, and it will work on IE7, when inline-block won't.
Also be careful with your bordered IDs. You should use classes.
Demo : JSFiddle
Try giving the #menu li ul a larger width:
#menu li ul{
visibility:hidden;
position:absolute;
border-top: 1px dotted #f9f9f3;
width: 10em;
}
and remove margin:1% 0 0 -2px;

How to Set all children start with same line of main parent

I trying to build my own multi dropdown menu, and i encounter this problem and have to ideal how to solve it. The best i get is using margin-left:-100px but it will run out of alignment when dropdown more then level 2.
this is what i try to develop
and this is my BEST solution so far... but NOT what i want
this are my html code
<div id="menuBox">
<li class="mainMenu">home</li>
<li class="mainMenu">about</li>
<li class="mainMenu">product
<ul class="w200">
<li>money maker</li>
<li>personal coarch
<ul class="w200">
<li>1 to 1</li>
<li>1 to 5</li>
<li>1 to 10</li>
</ul>
</li>
</ul>
</li>
<li class="mainMenu">consult</li>
<li class="mainMenu">contact</li>
</div>
this is my CSS setting
li.mainMenu{
list-style:none;
display:inline-block;
padding:25px 35px;
border-top:1px solid #CCCDDD;
margin:0px;
font-size:1.3em;
background:#CCCCCC;
}
li{
background:#CCCCCC;
cursor:pointer;
}
ul{
float:left;
position:absolute;
z-index:999;
list-style:none;
}
ul>li{
padding:5px 20px;
}
So which/how should i modify my code?
First you need to change the div to ul since the li items are only allowed to be inside ul/ol elements.
Try with this CSS
#menuBox, #menuBox ul{ /*reset ul styling for menu/submenu*/
padding:0;
margin:0;
}
#menuBox{
white-space:nowrap;
list-style:none;
font-size:1.3em;
}
#menuBox > li{ /*first level li elements*/
display:inline-block;
padding:25px 35px;
border-top:1px solid #CCCDDD;
margin:0px;
}
#menuBox li{ /*all li elements*/
position:relative;
background:#CCCCCC;
cursor:pointer;
}
#menuBox li:hover{ /*hovered li elements*/
background:black;
color:white;
}
#menuBox li li{ /*sub li elements - all levels after 1st*/
color:black; /*hide all submenus*/
padding:5px 20px;
}
#menuBox li:hover > ul { /*submenu ul elements*/
display:block; /*show submenu when parent li is hovered*/
}
#menuBox ul{ /*all submenu ul elements*/
z-index:999;
list-style:none;
position:absolute;
top:80%;
left:50%;
border:1px solid black;
display:none;
}
Demo at http://jsfiddle.net/gaby/g6yX2/
it because in your li.mainMenu padding left is set to 35, remove it.
if you want to keep that padding:
HTML:
<li class="mainMenu"><label>product</label>
<ul class="w200">
<li><label>money maker</label></li>
<li><label>personal coarch</label>
<ul class="w200">
<li>1 to 1</li>
<li>1 to 5</li>
<li>1 to 10</li>
</ul>
</li>
</ul>
</li>
CSS:
li.mainMenu{
list-style:none;
display:inline-block;
padding:25px 0px;
border-top:1px solid #CCCDDD;
margin:0px;
font-size:1.3em;
background:#CCCCCC;
}
li.mainMenu label {
padding: 0px 35px;
}
Example: http://jsfiddle.net/RaPK9/

css sub-menu vanishes before I can click, need help to identify bug(s)

I have a horizontal menu coded in html and css only, this menu has sub-menu and some sub-menu has sub-menu of their own.
It works fine with the first level sub-menu only, but when I insert some sub-menu for any individual sub-menu, they still show up. But I can not reach to click them, they vanishes as my cursor moves. A demo can be found here : http://example.bojroninad.net/pages/menu_demo1.html
However, I was able to watch some steady behavior of this menu sometimes, but most of the time they vanishes before I reach to them.
Here is my html code(pardon me for bad structured code):
`<html>
<link rel="stylesheet" href="menu1_css.css" media="screen" type="text/css">
<div id="menu1">
<ul>
<li>Home</li>
<li>About
<ul>
<li>History</li>
<li>Team</li>
<li>Offices</li>
</ul>
</li>
<li>Services
<ul>
<li>Web Design </li>
<li>Internet Marketing</li>
<li>Hosting </li>
<li>Domain Names
<ul>
<li>.ORG</li>
<li>.COM</li>
<li>.NET</li>
</ul>
</li>
<li>Broadband </li>
</ul>
</li>
<li>Contact Us
<ul>
<li>United Kingdom</li>
<li>France </li>
<li>USA </li>
<li>Australia </li>
</ul>
</li>
</ul>
</div>
</html>`
and my css code goes here:
#menu1 {
background-color:#ccc;
width:100%;
height:30px;
}
#menu1 ul{
padding:0px;
margin:0px;
}
#menu1 ul li
{
list-style:none;
display:inline;
margin-left:10px;
float:left;
height:30px;
position:relative;
}
#menu1 ul li a{
text-decoration:none;
font-weight:bold;
font-family:Bitstream Cyberbit,Garamond, Minion Web, ITC Stone Serif, MS Georgia;
color:green;
}
#menu1 li a:hover{
text-decoration:underline;
}
#menu1 li ul {
display:none;
margin:0px;
padding:0px;
position:absolute;
left:0px;
top:20px;
width:120px;
background-color:#999;
}
#menu1 li:hover ul{
display:block;
width:160px;
}
#menu1 li li {
display:list-item;
list-style:none;
}
/* second level sub menu */
#menu1 li li ul {
padding:0px;
margin-left:150px;
background-color:white;
top:0px;
position:relative;
}
#menu1 li li li{
display:none;
list-style:none;
position:absolute;
width:120px;
background-color:red;
}
#menu1 li li:hover li{
display:block;
width:100px;
position:relative;
margin-left:0px;
float:left;
height:30px;
}
#menu1 ul li li li a{
text-decoration:none;
font-weight:bold;
font-family:Bitstream Cyberbit,Garamond, Minion Web, ITC Stone Serif, MS Georgia;
color:yellow;
}
#menu1 li li li a:hover{
text-decoration:underline;
}
The problem is that your second and third level list elements are also floated left, making them smaller than the menu itself.
You can solve it by setting:
#menu1 li li {
float: none; /* line 50 */
}
#menu1 li li ul {
position: absolute; /* line 57, is now relative, to avoid growth of li li */
}
As far as I can see, the menu works fine - except that the second-level menu is positioned a bit too low
Usability would also be much better if you changed background color on li:hover + cursor:pointer - so you can see which button in the menu is active (and the user gets a visual feedback when he hovers out).

Got lost in CSS dropdown menu

i was able to make the dropdown menu already but when i tried to view it on other pc the secondlevel ul was misplaced and not below on it's li. So i tried to recode but i got lost. Please help.
Here is my html markup:
<div id="main-nav" class="menufont">
<ul id="nav">
<li>ONE</li>
<li class="sec">TWO
<ul id="twosecond" class="leveltwo">
<li>TWO A</li>
<li>TWO B</li>
</ul>
</li>
<li class="sec1">THREE
<ul id="threesecond" class="leveltwo">
<li>THREE A</li>
<li>THREE B</li>
<li>THREE C</li>
<li>THREE D</li>
</ul>
</li>
<li>FOUR</li>
<li>FIVE</li>
</ul>
</div>
And here is my messed up css:
ul.leveltwo {
background: #c7000d;
border: 1px solid black;
padding: 7px;
font: 16px Helvetica, Arial;
}
ul.leveltwo li {
color: #fff;
}
#twosecond li, #threesecond li {
font: 16px Arial;
display:block;
padding: 3px;
border-bottom: 1px dashed #fff;
}
#twosecond, #threesecond {
display: none;
position: absolute;
}
#nav li.sec:hover ul, #nav li.sec1:hover ul {
display: block;
}
The ul with class .leveltwo shows when i hover on it's main li but the ul of "TWO" is not aligned with it. Thanks alot in advance.
Just add display:none to your .leveltwo class and take out the #twosecond #threesecond tags. The `position:absolute is making the sub menus look like they're showing up under THREE.
Example: http://jsfiddle.net/r4gAQ/3/
Though I will say, I'd look into a jQuery solution, maybe a premade one like MLDDM. They have nice solutions that allow for vertical and horizontal menus. Also, hovering using pure CSS breaks in IE6... not that anyone should use that anymore.
Rechecked my post. This might be the CSS that would help you out. Try it out!
#nav li{
width:200px;
position:relative;
border: 1px solid black;
}
#nav li ul.leveltwo li{
border:none;
}
#nav li ul{
background: #c7000d;
border: 1px solid black;
padding: 7px;
font: 16px Helvetica, Arial;
color:white;
position:absolute;
left:200px;
top:0;
display:none;
}
#nav .sec:hover ul, #nav .sec1:hover ul{
display:block;
}

Resources