I have a span with 2 classes, like this <span class="tag invalid_tag">abc</span>. I add the second class through jQuery correctly and i need the properties from the second class to override the first, without actually removing it. For instance, the properties are the font color and background-color. Usually the tag is green but when i add the class “invalid_tag” it should turn red but it doesn’t because the first class has the priority. I would like to know how to change this priority, or if i can achieve this in a different way.
I have a span with 2 classes, like this <span class=tag invalid_tag>abc</span> . I
Share
The priority of CSS declarations relies on their order in the CSS (later declarations override earlier ones) and their specificity (more specific declarations override less-specific ones). (It has nothing to do with the order you specify the class names in the attribute.) More in Section 6 the specification.
Here’s an example of order. This CSS
with this HTML
…results in two blue paragraphs, because the second declaration takes precedence over the first. (Live example) That happens because both declarations have the same specificity.
Here’s a demonstration of specificity:
This CSS
…with the same HTML results in two red paragraphs, because the selector
p.foois more specific than the selector.bar, so it takes precedence even though it’s earlier in the CSS. (Live example)