k4r1
k4r1

Reputation: 47

'Active' Class Not Working

I have a problem with my css. I'm trying to set the active class to stay highlighted in pink on the vertical menu, but it's not. how do i keep it lit without the user hovering on top of it. here are my HTML and CSS.

HTML CODE:

<div id="cssmenu">
  <ul>
     <li class="active"><a href="http://www.local-trends.com/cellphones--parts.html">Shop All</a></li>
 <li><a href="#">Cell Phones & Smartphones</a></li>
 <li><a href="#">Smart Watches</a></li>
 <li><a href="#">Batteries</a></li>
 <li><a href="#">Cases, Covers & Skins</a></li>
 <li><a href="#">Chargers & Cradles</a></li>
 <li><a href="#">Headsets</a></li>
 <li><a href="#">Phone Cards & SIM Cards</a></li>
 <li><a href="#">Cell Phone & Smartphone Parts</a></li>
  </ul>
</div>

CSS CODE:

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul ul {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 0;
}
#cssmenu ul {
  position: relative;
  z-index: 597;
  float: left;
}
#cssmenu ul li {
  float: left;
  min-height: 1px;
  line-height: 1em;
  vertical-align: middle;
}
#cssmenu ul li.hover,
#cssmenu ul li:hover {
  position: relative;
  z-index: 599;
  cursor: default;
}
#cssmenu ul ul {
  margin-top: 1px;
  visibility: hidden;
  position: absolute;
  top: 1px;
  left: 99%;
  z-index: 598;
  width: 100%;
}
#cssmenu ul ul li {
  float: none;
}
#cssmenu ul ul ul {
  top: 1px;
  left: 99%;
}
#cssmenu ul li:hover > ul {
  visibility: visible;
}
#cssmenu ul li {
  float: none;
}
#cssmenu ul ul li {
  font-weight: normal;
}
/* Custom CSS Styles */
#cssmenu {
  font-family: 'Lato', sans-serif;
  font-size: 18px;
  width: 200px;
}
#cssmenu ul a,
#cssmenu ul a:link,
#cssmenu ul a:visited {
  display: block;
  color: #848889;
  text-decoration: none;
  font-weight: 300;
}
#cssmenu > ul {
  float: none;
}
#cssmenu ul {
  background: #fff;
}
#cssmenu > ul > li {
  border-left: 3px solid #d7d8da;
}
#cssmenu > ul > li > a {
  padding: 10px 20px;
}
#cssmenu > ul > li:hover {
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a {
  color: #ff0040;
}
#cssmenu > ul > li:hover {
  background: #f6f6f6;
} 

Upvotes: 0

Views: 2652

Answers (3)

P.Surendar
P.Surendar

Reputation: 47

add jQuery plugin and css code

css :

#cssmenu  ul  li.active a{
         background:pink;
}

HTML Code :

    <div id="cssmenu">
     <ul>
     <li class="active"><a href="http://www.local-trends.com/cellphones--parts.html" onclick="myFunction(this)">Shop All</a></li>
     <li><a href="#" onclick="myFunction(this)">Cell Phones & Smartphones</a></li>
     <li><a href="#" onclick="myFunction(this)">Smart Watches</a></li>
     <li><a href="#" onclick="myFunction(this)">Batteries</a></li>
     <li><a href="#" onclick="myFunction(this)">Cases, Covers & Skins</a></li>
     <li><a href="#" onclick="myFunction(this)">Chargers & Cradles</a></li>
     <li><a href="#" onclick="myFunction(this)">Headsets</a></li>
     <li><a href="#" onclick="myFunction(this)">Phone Cards & SIM Cards</a></li>
     <li><a href="#" onclick="myFunction(this)">Cell Phone & Smartphone Parts</a></li>
     </ul>
    </div>

function code :

    <script>
    function myFunction(tag) {
       $("#cssmenu ul li.active").removeClass("active"); //remove previous li active class
       $(tag).closest("li").addClass("active");  //set active class to clicked li
       alert($(tag).text());
    }
    </script>

Upvotes: 0

Jones Joseph
Jones Joseph

Reputation: 4938

There is no .active class in your CSS. You should probably add those to your CSS.

Change

THIS:

#cssmenu > ul > li:hover{
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a{
  color: #ff0040;
}
#cssmenu > ul > li:hover{
  background: #f6f6f6;
} 

TO THIS:

#cssmenu > ul > li:hover, #cssmenu > ul > li.active {
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a, #cssmenu ul li.active > a {
  color: #ff0040;
}
#cssmenu > ul > li:hover, #cssmenu > ul > li.active {
  background: #f6f6f6;
} 

Upvotes: 1

Furquan Khan
Furquan Khan

Reputation: 1594

I cannot see any css applied for active class.

li.active{ color: pink; } //Try using this

Upvotes: 0

Related Questions