1

This question is in regards to precedence. I have an <ul> that contains my nav items in <li> I have an id for the <ul> which I use to reference the <li> and <a> tags nested within for styling. This all works fine, but when I use JavaScript to add a class to these specific <a> tags, to change the appearance when hovering over the links nothing happens.

I know the code is correct as I have tested it, but it just seems the ID styles take precedence over the Class's in when referencing styles.

How would I go about finding a solution, one idea I have is to remove the existing ID on mouseenter before adding the class, and then visa versa on mouseleave. Is there an easier solution?

2
  • We will need to see your selectors for both the CSS and JavaScript. Commented Feb 21, 2013 at 16:40
  • can you show us some of your code? Commented Feb 21, 2013 at 16:41

2 Answers 2

2

ID styles are stronger, but you can use it when adding class for styling links as well.

So, if your menu is

<ul id="main">
    <li><a href=#></a></li>
</ul>

And JS adds class "hovered" to element, then use CSS

#main .hovered {
.. styles here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Here's some additional reading on CSS Specificity: coding.smashingmagazine.com/2007/07/27/…
-1

Try adding !important to the end of your css for values which require overriding.

.class { color: #fbfbfb !important; }

1 Comment

This worked perfectly, thank you. Never used !important before, simple solution, really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.