Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css stylesheet stronger than jquery addClass?

Hey guys this thing drives me crazy. I defined in my stylesheet:

ul#menuCnt li a {
   color:"red";
}

If I want to make a hover effect, using jquery this won't change any color.

$("ul#menuCnt li a").hover(function() {
        $(this).addClass("brown");
    }, function() {
        $(this).removeClass("brown");
    });

I am really confused. If I don't define a color in my css stylesheet the hovereffect works.

Hope you can help me. You guys helped me so much by learning jquery and css :)

Thank you!

like image 495
user134282 Avatar asked Nov 26 '25 23:11

user134282


2 Answers

According to css priorities, if your class .brown is defined like this in your css file :

.brown{}

The rules inside will not override same rules in your # selector.

You can make it override them using the !important keyword.

.brown {
    color: brown !important;
}

Altough this is not the best solution here, it will work... The less you use !important, the more your code will be easy to read.

See http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html.

like image 134
vvo Avatar answered Nov 28 '25 14:11

vvo


Do you have a .brown selector in your CSS? Also an a tag might be a bad choice because it has a very good :hover selector build in.

like image 34
Daniel A. White Avatar answered Nov 28 '25 14:11

Daniel A. White