CSS Hover Menu Showing active menu item - html

I have the following css:
#nav ul li {float: left; margin-right: 10px; }
#nav ul li a { display: block; color: #fff; width: 100px; line-height:35px; background: #15612e; padding-left:16px;}
#nav ul li a:hover { background-color: #ea8206;}
This works as expected. What I would now like to do is have a class like this:
a.active { background-color: #ea8206;}
Which I would like to assign to whichever menu item is selected on a page, but this is not working.
I assume it's to do with precedence, and have tried various combinations of:
#nav ul li a.active {background-color: #ea8206;}
I think I am just struggling with the right combination.
And the HTML is:
<div id="nav">
<ul>
<li>Home</li>
<li>Buy</li>
<li>Sell</li>
<li>Search</li>
<li>Contact</li>
</ul>
</div>
Could anyone point me in the right direction?
Many thanks

Here's a fiddle : http://jsfiddle.net/moonspace/aKLUS/
This is all about specificity...
As you've already referred to 'a' classes in the CSS but prefixed them with '#nav ul' these will override a simple 'a.active' definition. So, add the '#nav ul' before a.active (as per the fiddle) and it'll work . . .
#nav ul li a { display: block; color: #fff; width: 100px; line-height:35px; background: #15612e; padding-left:16px;}
#nav ul li a:hover { background-color: #ea8206;}
#nav ul li a.active{ background-color: red; }
More useful stuff here : http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

Related

Active is not working on link in css

I am working on a navigation bar Active color on my navigation bar is not working.Hover is working fine but not the active.In when from browser i select toogle element state and click on active browser change the color on clicking active state but in normal condition its not working.i am stucked and very confused , can someone help me please ? Thanks in advance.
.main-nav {
color: #FFF;
width: 100%;
background-color: #5e2d91;
float: right;
line-height: 42px;
margin-top: -3px;
}
.main-nav ul li {
display: inline;
padding: 0px 10px;
}
.main-nav ul li a {
color: #FFF;
text-decoration: none;
padding: 20px 14px;
}
.main-nav ul {
margin-bottom: 7px !important;
}
.main-nav ul li a:hover {
background-color: #0098aa;
}
.main-nav ul li a:active {
background-color: #0098aa;
}
<nav class="main-nav">
<ul>
<li> Home
</li>
<li> Trade Now
</li>
<li> Transactions
</li>
<li> Performance
</li>
<li>History
</li>
<li class="time">US Markets Open in <span id="hm_timer" class="style colorDefinition size_sm">08:05:35</span> hours</li>
</ul>
</nav>
your code working fine on fiddle there is might be some other css overwriting your code
try this
body .main-nav ul li a:active{
background-color:#0098aa;
}
if its not works try adding important // not recommended
body .main-nav ul li a:active{
background-color:#0098aa!important;
}
I recommend you to inspect element on that link and check active state, there might be some other css overwriting ur code
In your code, the background-color for :active is same as hover, so it's working but you can't see it. Change it to some other color and it would work.
In case of your website, I don't see any CSS selector as :active. Are you sure you've written it there?
In your site marketinthepocket.com you have mentioned background color as white. changing for color will work out.
body .main-nav ul li a:active {
background-color: red;
}

CSS: drop-down causes page to jump up

When user clicks on my drop-down menu, it jumps the page back to the very top (like a page reload).
See this jsFiddle with stripped down code.
I know that if I remove the # in href="#", it should work, but that is not good practice.
How do I make it so it doesn't jump the page to top?
HTML:
<div class="nav">
<ul>
<li>
Drop
<ul class="nav-user nav-li-cont">
<li> Hello
</li>
<li> World
</li>
</ul>
</li>
</ul>
</div>
CSS:
div.nav {
display: inline-block;
margin-left:50px;
}
div.nav ul {
padding: 0;
margin: 0;
list-style-type: none;
position: relative;
}
div.nav ul li {
float:left;
}
div.nav ul li a {
text-decoration: none;
}
div.nav ul li ul {
display:none;
}
div.nav ul li:hover ul {
display: list-item;
position: absolute;
}
div.nav ul li:hover ul li {
float:none;
}
div.nav ul li ul li:hover {
float:none;
}
div.nav ul li ul li a {
text-decoration: none;
cursor: pointer;
float:left;
width:100%;
}
.nav-li-cont {
border-radius: 4px;
float: left !important;
padding: 10px !important;
}
That's because there's no js/jquery assigned to that link. And the browser will assume that this is a "link for a new page", and actually if you check the URL of the website, it's propably changed to this after you click on that button (you can't see this on jsfiddle): example.com/currentPage/#.
If you change this line
Drop
to this instead
Drop
it will not jump to the top anymore. The void operator is often used merely to obtain the undefined primitive value (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).
http://jsfiddle.net/3837z3b0/3/
Update
If you have multiple links using href="#" you can either exchange the links as I mentioned above to this href="javascript:void(0)" or you can add a class called noclick for example for every link that has href="#" and add the following:
$(".noClick").attr('href','javascript:void(0)');
http://jsfiddle.net/3837z3b0/4/

How to center a menu li without centering sub menu

I want to center a menu without centering sub-menu, this is my code:
<ul class="drop_menu">
<li>Home</li>
<li>hhh
<ul class="sub_drop_menu">
<li>Home</li>
<li>Home</li>
<li>Home</li>
</ul>
</li>
</ul>
this is the css that i'm using :
.drop_menu {
list-style-type:none;
height:30px;
}
.drop_menu > li {
float:left;
font-weight: bold;
font-size: 16px;
}
ul.drop_menu > li {
text-align: center;
}
add this css to your code hope it will work
.sub_drop_menu li {
text-align: left;
}
HOW CSS WORKS
"Cascading" Style Sheets.
a "Rule" - a selector and then 1 or more "declarations" of property and value.
.your-selector {
property: value;
}
Read it from right to left...
.menu li {
text-align: center;
}
"Any (and all) li - within .menu --- text-align: center;
So this means your .sub-menu's li are going to be text align center, because it is an li within .menu. --- you make the rules.
You can do one of two things in this situation.
.sub-menu li { /* this overrides the "cascading" rule from before */
text-align: right;
}
or
.menu > li {
text-align: center;
}
Which would mean, all li in the first tier --- the first children of .menu
Here is a jsFiddle

How to make the link text change color instead of underlining when hovered over

I have this menu I set up and it underlines when hovered over, but I would like it to change color, which is the default for my wordpress theme. The title, "BOLI STYLUS" changes color exactly like I want.
Here is the code for the title:
<hgroup>
<h1 id="site-title"><span><?php bloginfo( 'name' ); ?></span></h1>
<h2 id="site-description"><?php bloginfo( 'description' ); ?></h2>
</hgroup>
Here is the menu:
Here is the style.css code for it:
.header_nav ul{
margin: 0;
padding: 0;
text-align: center;
}
.header_nav ul li{
display: inline;
}
.header_nav ul li a{
float: center;
padding: 10.5px 11px;
}
.header_nav ul li a:hover, .menu ul li .current{
color: #50a9cb;
}
Here is the header.php code:
<div class="header_nav">
<ul>
<li>BOLI</li>
<li>BOLI+</li>
<li>ABOUT US</li>
<li>FAQ</li>
<li>PRESS</li>
<li>YOUR CART (0)</li>
</ul>
</div>
You can use the following rule, and replace XXXXXX with your desired color. I assume you want to keep the styles in the same spirit, i.e. have the selected item also have the same style as when you hover it.
.header_nav ul li a:hover, .menu ul li .current
{
color: #XXXXXX; /* replace with real value */ text-decoration: none;
}
The code that you showed already does color the text when you hover it. It also, however, underlines it. To change that add text-decoration: none; like I did below.
.header_nav ul li a{
float: center;
padding: 10.5px 11px;
**text-decoration: none;**
}
For the complete code, see here:
http://jsfiddle.net/8KPcy/1/

Drop-down with CSS

I got a reallly simple drop-down menu but got a problem with the submenus width.
See it here:
http://dl.dropbox.com/u/70953/SOSfrontpage.html
My HTML is:
<div id="navigation">
<div id="menu-dropdown">
<ul class="menu">
<li class="menu_punkt">Frontpage</li>
<li class="menu_punkt">Who are we?</li>
<li class="menu_punkt">This is a test
<ul>
<li>Your profile</li>
<li>New profile</li>
</ul>
</li>
<li class="menu_punkt">SOS Profile
<ul>
<li>Your profile</li>
<li>New user</li>
</ul>
</li><li class="menu_punkt">Log ind</li>
</ul>
</div>
</div>
and my CSS is:
/*horisontal navbar*/
#menu-dropdown {
list-style: none;
position: absolute;
top: 600px;
}
#menu-dropdown ul li {
float:left;
list-style-type: none;
font-size: 0.8em;
}
#menu-dropdown li ul {
position: absolute;
display: none;
background-color:#cdc3a2;
padding: 0px;
margin-bottom:1px;
}
#menu-dropdown ul ul li {
clear: both;
}
#menu-dropdown ul li a {
display: block;
padding: 10px;
color:#102B47;
text-decoration:none;
font-family:Verdana, Geneva, sans-serif;
font-size: 1em;
}
#menu-dropdown ul li a:hover {
background-color: #cdc3a2;
}
#menu-dropdown li:hover ul, li.over ul {
display: block;
}
You can see my problem here:
http://dl.dropbox.com/u/70953/SOSfrontpage.html
Regards
- Mestika
Add a width to the submenu anchors
.menu ul li a { width:200px;}
Also add the hover to the li (not teh anchor) that way the top menu stays selected when you are in the submenus
#menu-dropdown ul li:hover, #menu-dropdown ul li.hover {
background-color: #cdc3a2;
}
I think you should add a width to the menu-dropdown ul li class.
A great way to build a css drop down menu is son of a suckerfish.
Yes JAO is right u shoud give width to li like this
#menu-dropdown ul ul li {
clear:both;
width:107px;}
you can get more clue from here http://www.cssnewbie.com/example/css-dropdown-menu/
Try:
.menu ul li li {width: 100%}
when I learnt to write css dropdown menus I based a lot of experiments on the ton of examples on this site : http://www.cssplay.co.uk/menus/ - very clear css / html examples, minimal, clean code
hope it helps :)