CSS Drop Down – Sub Menu Color - css

I'm looking for some advice on this issue.
I went through a tutorial a while back to build a CSS drop down menu and can't seem to change the default color of the sub menus – it always matches the default red color for the a tag.
I've been messing around with this for a while now and can't seem to find a solution. Can someone help me out with this please?
Here is the html:
<nav>
<ul>
<li><a class="selected" href="index.html">Home</a></li>
<li>Clothing</li>
<li>Gear</li>
<li>Brand
<ul>
<li>XXXXXX</li>
<li>XXXXXX</li>
</ul>
</li>
<li>Fighters
<ul>
<li>XXXXXX</li>
<li>XXXXXX</li>
</ul>
</li>
<li>My Account</li>
</ul>
</nav>
And here is the CSS:
nav {
position:relative;
float:right;
font-size:14px;
margin-top:35px;
font-weight:bold;
padding-right:178px;
z-index:4;
}
nav ul ul {
display:none; /* hide sub menus */
}
nav ul li:hover > ul {
display:block; /* show sub menus on hover */
}
nav ul {
float:right;
font-size:14px;
margin-top:-3px;
text-transform:uppercase;
list-style:none;
position:relative; /* position sub menu according to nav */
display:inline-table; /* condense with of sub menu to fit */
}
nav ul:after {
content:"";
clear:both;
display:block; /* clear floats on other list items */
}
nav ul li {
float:left;
}
nav ul li:hover a {
color:#ee1f3b;
text-decoration:none;
-webkit-transition-property:color;
-webkit-transition-duration:0.2s, 0.2s;
-webkit-transition-timing-function:linear, ease-in;
-moz-transition-property:color;
-moz-transition-duration:0.2s, 0.2s;
-moz-transition-timing-function:linear, ease-in;
}
nav ul li a {
padding:4px 11px;
text-decoration:none;
color:#000000;
display:block;
text-decoration:none;
}
nav ul ul {
background:#cacaca;
position:absolute;
top:25px; /* sub position */
}
nav ul ul li {
float:none;
border-bottom:1px solid rgba(0, 0, 0, 0.2);
position:relative;
}
nav ul ul li:last-child {
border-bottom:1px solid rgba(0, 0, 0, 0.2);
}
.selected {
color:#ee1f3b;
}
nav ul ul li a:hover {
color:#000000;
}
Thanks for your time.

From the above code for changing the color of submenus, you have not targeted the child elements of the main menus. For that you need to target them and add new rules to specifically target that element and change the color. Here is the solution.
On hover of the items with submenus, the color change for instance here green color on display of the submenus.
nav ul li:hover ul li a{color:green;}
On hover of the submenus, change of color from green to yellow for instance.
nav ul li:hover ul li a:hover{color:yellow;}
To elaborate this,
The HTML:
<nav>
<ul>
<li><a class="selected" href="index.html">Home</a></li>
<li>Clothing</li>
<li>Gear</li>
<li>Brand
<ul>
<li>XXXXXX</li>
<li>XXXXXX</li>
</ul>
</li>
<li>Fighters
<ul>
<li>XXXXXX</li>
<li>XXXXXX</li>
</ul>
</li>
<li>My Account</li>
</ul>
</nav>
The CSS:
nav {
position:relative;
float:right;
font-size:14px;
margin-top:35px;
font-weight:bold;
padding-right:178px;
z-index:4;
}
nav ul ul {
display:none; /* hide sub menus */
}
nav ul li:hover > ul {
display:block; /* show sub menus on hover */
}
nav ul {
float:right;
font-size:14px;
margin-top:-3px;
text-transform:uppercase;
list-style:none;
position:relative; /* position sub menu according to nav */
display:inline-table; /* condense with of sub menu to fit */
}
nav ul:after {
content:"";
clear:both;
display:block; /* clear floats on other list items */
}
nav ul li {
float:left;
}
nav ul li:hover a {
color:#ee1f3b;
text-decoration:none;
-webkit-transition-property:color;
-webkit-transition-duration:0.2s, 0.2s;
-webkit-transition-timing-function:linear, ease-in;
-moz-transition-property:color;
-moz-transition-duration:0.2s, 0.2s;
-moz-transition-timing-function:linear, ease-in;
}
nav ul li a {
padding:4px 11px;
text-decoration:none;
color:#000000;
display:block;
text-decoration:none;
}
nav ul ul {
background:#cacaca;
position:absolute;
top:25px; /* sub position */
}
nav ul ul li {
float:none;
border-bottom:1px solid rgba(0, 0, 0, 0.2);
position:relative;
}
nav ul ul li:last-child {
border-bottom:1px solid rgba(0, 0, 0, 0.2);
}
.selected {
color:#ee1f3b;
}
nav ul ul li a:hover {
color:#000000;
}
nav ul li:hover ul li a{color:green;}
nav ul li:hover ul li a:hover{color:yellow;}
Hope this helps.

Another method is to give the ul a id in the submenu something like this
<li>Brand
<ul id="submenu">
<li>AAAAAA</li>
<li>BBBBBB</li>
</ul>
</li>
CSS
#submenu li a
{
color:green;
}
See the full Jsfiddle here

Related

Second level submenu CSS

I was hoping if you can help me with my little problem. I am just updating one of my developers style sheets. I am a beginner in CSS so I am having difficulties adding the second level submenu in it.
Here's the link DEMO PAGE
This is my HTML
<div id="sidebar"><i class="icon icon-home"> </i>Dashboard<ul style="display: block;">
<li class="active"><i class="icon icon-home"></i> <span>Dashboard</span> </li>
<li> <i class="icon icon-signal"></i> <span>Charts & graphs</span> </li>
<li><i class="icon icon-th"></i> <span>Tables</span></li>
<li><i class="icon icon-fullscreen"></i> <span>Full width</span></li>
<li class="submenu"> <i class="icon icon-th-list"></i> <span>API</span> <span class="label label-important">4</span>
<ul>
<li>
boom
<ul>
</ul>
</li>
<li>% of National Roads Paved</li>
<li>Process</li>
<li>Regional Profiles</li>
</ul>
</li>
</ul>
</div>
and this is my CSS
/* Top user navigation */
#sidebar{ width:100%; background:#252125; position:absolute; clear:both; top:62px;}
#sidebar > ul{ margin:0px; padding:0px; width:100%; display:block; z-index:999;}
/*Border right sidebar */
#sidebar > ul > li { list-style-type:none; float:left; display:block; margin:0px; border-right:1px solid #464652; position:relative; padding:10px; cursor:pointer}
/*Border right */
#sidebar > ul > li a{ padding:12px 0;}
#sidebar > ul > li:hover ul { display:block;}
/*#sidebar > ul > li:hover { background-color:#41bedd;} */
/*On hover menu */
#sidebar > ul > li:hover { background-color:#464652;}
/*On hover menu */
#sidebar > ul > li:hover a{ background:none;}
/*Modules color */
#sidebar > ul li ul { margin:0px; padding:0px; display:none; z-index:999; position:absolute; left:0px; top:40px; background:#464652; min-width:200px;}
/*Modules color */
#sidebar > ul li ul li { list-style-type:none; margin:0px; font-size:12px;line-height:30px; }
#sidebar > ul li ul li a { display:block; padding:5px 10px; color:#fff; text-decoration:none; font-weight:bold; }
/*Modules color on hover */
#sidebar > ul li ul li:hover a{ background-color:#5A5A69;}
/*Modules color on hover*/
#sidebar > ul li span { cursor:pointer; margin:0px 2px 0 5px; font-weight:bold; color:#fff; font-size:11px; }
#sidebar > ul li a i{ background-image: url("../img/glyphicons-halflings-white.png"); margin-top:4px; vertical-align: top;}
I know this post has been posted from different threads. I really cant understand them. Hope you understand. Thanks in advance.
First of all, in your sample HTML, you don't have any lis in your tertiary level. Put some in there.
As for your CSS, it's presently set up to handle the showing and hiding of UL's below the top level one on hover. We need to get more specific so that it excludes the 3rd level--as well as then get more specific to and add a second level hover style.
Where you have this:
#sidebar > ul > li:hover ul { display:block;}
Change it to this:
#sidebar > ul > li:hover > ul { display:block;}
The first will show ALL child ULs of the LI on hover. The latter will show only the direct child of the LI on hover.
Now we need to add a trigger for the 3rd level:
#sidebar > ul > li > ul > li:hover > ul { display:block; top: 0; left: 200px}
While we're at it, we included a new set of positioning so that it appears to the right of the current hover rather than below (which would cover up the second level navigation.
Working example: http://jsfiddle.net/wS9t3/

Different Level Navigation CSS

I have a left hand navigation that I am trying to code with CSS.
At the moment the sub-categories (level 1) are showing as the 'active' color when the main one is selected (from level 0). When you click on a sub-category (level 1) it is then following the same css rules as the main category (level 0) but not until clicked.
What I want, is the subcategories (level 1) to show with a white background when the main category is selected (level 0) (opening the subcategories). Then, when an item is chosen within the subcategory (level 1) it follows a different rules (font to be in a different color for example).
Sorry, it's a little confusing to try to explain! Here is my CSS.. any help would be greatly appreciated! I just can't get my head around this one. The site is created using Magento, so I can't alter the html easily.
.vertnav-container {
margin-top:10px;
}
#vertnav li .vertnav-cat {
display:block;
width:210px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 10px;
font-size: 12px;
}
#vertnav li a {
}
#vertnav .inactive .vertnav-cat {
background-image: url(../images/inactive_bgd.jpg);
background-repeat: repeat-x;
}
#vertnav .next .vertnav-cat {
background-color:#b7de70;
}
#vertnav .prev .vertnav-cat {
background-color:#b7de70;
}
#vertnav li.parent .vertnav-cat {
background-color:#FFFFFF;
font-weight:bold;
}
#vertnav li.active .vertnav-cat {
background-color:#f59942;
background-image: url(../images/active_bgd.jpg);
font-weight:bold;
}
#vertnav li.inactive .vertnav-cat {
font-weight:normal;
}
#vertnav .level0 .vertnav-cat {
}
#vertnav .level1 .vertnav-cat {
padding-left:20px;
width:183px;
height: 5px;
border-bottom:1px dotted gray;
}
#vertnav .level2 .vertnav-cat {
padding-left:20px;
width:172px;
}
#vertnav .level3 .vertnav-cat {
padding-left:30px;
width:162px;
}
UPDATE:
I think this is the html, but am not sure how I edit it as it's through Magento, will need to look this end if you think that's what needs to be done...
<div class="col-left sidebar"><div class="vertnav-container">
<div class="">
<h4 class="no-display">Category Navigation:</h4>
<ul id="vertnav">
<li class="first prev level0-inactive has-children level0 inactive fruit">
<span class="vertnav-cat"><span>Fruit</span> </span>
</li>
<li class="level0-active level0 active vegetables">
<span class="vertnav-cat"> <span>Vegetables</span></span>
</li>
<li class="next level0-inactive level0 inactive meat">
<span class="vertnav-cat"><span>Meat</span> </span>
</li>
<li class="level0-inactive level0 inactive dairy">
<span class="vertnav-cat"><span>Dairy</span></span>
</li>
<li class="last level0-inactive level0 inactive for-the-pantry">
<span class="vertnav-cat"><span>For the Pantry</span></span>
</li>
</ul>
</div>
</div>
Well, you haven't responded. Here is the most simplistic method I could come up to easily target different elements. The best I could do without seeing your HTML, but it should provide some guidance into how to structure your layout.
Few things. I'd use lists instead of a ton of classes, as your CSS would indicate you're doing.
Also, using a:focus psuedo class is an easy way to target the actively clicked link without using a bunch of additional classes and whatnot.
JS Fiddle: http://jsfiddle.net/SinisterSystems/v4n7G/3/
HTML:
<ul id="leftNav">
<li>Head One
<ul>
<li>Sub 1</li>
<li>Sub 2</li>
<li>Sub 3</li>
</ul>
</li>
<li>Head Two
<ul>
<li>Sub 1</li>
<li>Sub 2</li>
<li>Sub 3</li>
</ul>
</li>
</ul>
CSS:
a, a:visited, a:hover, a:active {
color:#AAA;
}
ul#leftNav {
width:200px;
}
ul#leftNav li {
list-style:none;
color:#666;
}
ul#leftNav a:focus {
color:green;
}
ul#leftNav ul a{
color:red;
}
ul#leftNav ul a:focus {
color:blue;
}
<ul id="nav" class="sixteen columns">
<li>Home
</li>
<li>Portfolio
<ul>
<li>Asia
<ul>
<li>Korea</li>
<li>China</li>
<li>Japan</li>
</ul>
</li>
<li>Europe
<ul>
<li>France</li>
<li>Germany</li>
<li>Italy</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
#nav {
width:800px;
margin:30px 50px;
padding: 0;
float:left;
}
#nav li {
list-style: none;
float: left;
padding:0 10px;
background-color:#367FB3;
color:white;
}
#nav li a:hover {
background-color:#52baff;
color:#fff;
}
//daf adf
/*--temp--*/
#nav ul ul li {
clear:left;
}
#nav ul ul {
position:absolute;
left:14em;
top:0;
}
#nav ul ul li a {
display:block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
/*--end temp--*/
#nav li a {
display: block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
#nav a:hover {
color:#367FB3;
}
#nav a:active {
color:#367FB3;
}
#nav li ul {
display: none;
width: 14em;
/* Width to help Opera out */
background-color:transparent;
z-index:666;
}
#nav li:hover ul, #nav li.hover ul {
display: block;
position: absolute;
margin:0px -10px;
padding:0px;
}
#nav li:hover ul ul {
display:none;
}
#nav li ul li:hover ul {
display:block
}
#nav li:hover li, #nav li.hover li {
float: none;
line-height:30px;
}
#nav li:hover li a, #nav li.hover li a {
background-color:#367FB3;
color:#fff;
font-size:13px;
font-family:"Lato" !important;
}
#nav li li a:hover {
background-color:#52baff;
color:#fff;
}
Working Fiddle

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).

css styling not rendering properly, possible div issue?

I'm trying to make a horizontal menu layout in CSS. I was using the guide listed here:
http://www.devinrolsen.com/pure-css-horizontal-menu/
I've got a css file looking like this, called navigation.css:
.navigation{
width:100%;
height:30px;
background-color:lightgray;
}
.navigation ul {
margin:0px;
padding:0px;
}
.navigation ul li {
display:inline;
height:30px;
float:left;
list-style:none;
margin-left:15px;
}
.navigation li a {
color:black;
text-decoration:none;
}
.navigation li a:hover {
color:black;
text-decoration:underline;
}
.navigation li ul {
margin:0px;
padding:0px;
display:none;
}
.navigation li:hover ul {
display:block;
width:160px;
}
.navigation li li{
list-style:none;
display:block;
}
and in my actual php page, I have this
<div class="navigation">
<ul>
<li>
something
<ul>
<li>Hello</li>
<li>hello2</li>
</ul>
</li>
<li>
Browse database
<ul>
<li>test</li>
<li>Test2</li>
<li>Search</li>
</ul>
</li>
</ul>
</div>
For reasons I cannot determine, their is no drop-down menu effect. Consequently, if I change navigation to an id instead of a class by replacing .navigation with #navigation, then none of the layout affects the HTML.
In case you're still having the problem, have you tried changing:
.navigation li li{
list-style:none;
display:block;
}
To:
.navigation li li{
list-style:none;
display:none;
}
.navigation li:hover li{
display:block;
}

Why does this text pop to the left in Webkit?

Maybe somebody has seen this before? When you hover over the "First List" and "Second List" text, the text pops a couple pixels to the left. This only happens in Webkit browsers. Tested in Chrome on Linux and Mac, Safari on Mac, and Android browser. Here's the code. I also made a fiddle: http://jsfiddle.net/XzVLt
#navigation {
line-height:30px;
height:30px;
clear:left;
text-align:left;
position:relative;
}
#navigation li {
padding:0 5px;
margin-left:-1px;
width:100px;
text-align:center;
float:left;
display:inline;
}
#navigation ul ul {
display:block;
position:absolute;
}
#navigation li ul {
margin-left:-5px;
padding-right:10px;
}
#navigation li ul li {
margin-right:-2px;
text-align:left;
line-height:15px;
padding:5px;
clear:both;
width:100%;
}
#navigation ul li {
list-style:none;
}
#navigation ul li {
float:left;
list-style:none;
}
#navigation li ul {
display:none;
}
#navigation li:hover ul {
display:block;
}
#navigation a {
font-size: 0.8em;
}
#navigation li li a {
display:block;
margin-left:10px;
text-indent:-10px;
}
​
<div id="navigation">
<ul>
<li>
First list
<ul>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
</li>
<li>
Second list
<ul>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
</li>
</ul>
</div>​
I'm guessing that it has something to do with your margins and padding that comes into play as soon as you change the drop-down from display:none to display:block
I use the suckerfish method for drop-downs; you can easily implement this in your method as well. Just change how you phase in the drop-down content as follows:
Replace ...
#navigation li ul {
display:none;
}
#navigation li:hover ul {
display:block;
}
... with ...
#navigation li ul {
position:absolute;
left:-999em;
}
#navigation li:hover ul {
left:auto;
}
Quick tests in Firefox and Safari - it works just fine ... you'll need some JS to create an element to replace the li:hover pseudo or it won't work in IE.
Read more about the method here http://htmldog.com/articles/suckerfish/dropdowns/

Resources