1

Anybody knows how I can achieve that the second selector (.resource-list li a[data-type="XXX"]) has a higher priority than the first (.resource-list[data-type="XXX"] li a)? Without rearranging the CSS?

.resource-list[data-type="jpg"] li a,
.resource-list li a[data-type="jpg"] /* This should have the highest priority */ {
    background-image: url(jpg.png);
}

.resource-list[data-type="pdf"] li a,
.resource-list li a[data-type="pdf"] /* This should have the highest priority */ {
    background-image: url(pdf.png);
}

See the fiddle: http://jsfiddle.net/8bG2j/

3
  • all you need to do is to change the order of the 2 blocks Commented Jun 6, 2013 at 13:13
  • @w3jimmy I quote from the question: Without rearranging the CSS? Commented Jun 6, 2013 at 13:14
  • @w3jimmy then you would still have the same issue when the situation (html structure) is reversed Commented Jun 6, 2013 at 13:24

3 Answers 3

1

bwoebi's solution is good enough, if you don't care about IE8 (which does not support :not())

An alternative is to decrease the specificity of the first part of the selectors.

.resource-list[data-type="jpg"] a, /* less specific after omitting the "li" */
.resource-list li a[data-type="jpg"] {
    color: black;
}

.resource-list[data-type="pdf"] a,
.resource-list li a[data-type="pdf"] {
    color: red;
}

http://jsfiddle.net/8bG2j/4/

Sign up to request clarification or add additional context in comments.

1 Comment

This was the key! I've tried this sooner, but it did not workend - maybe I misstyped something. Anyways - now it's working. Thank you!
1

http://jsfiddle.net/8bG2j/2/

This should do it. Simply use a pseudoclass which is always true like a:not(s).

It will raise your priority by the priority of the selector inside the :not statement.

Alternatively, as stated in the comments, you can prefix it with an ul when you don't want to break with older browsers (like IE 8). You can do this if .ressource-list will be always a class of an <ul> tag.

3 Comments

@Andy yes, but the it'll bound to the ul. I didn't want to change the possibly targeted elements.
:not() on its own does not have any specificity. the added specificity comes from the selector inside ("s" in this case)
@xec thanks, just reread the w3 specification; they used there an example with #id:not(FOO) and I thought FOO + pseudoclass would give 101; and not because of the #id...
1

You can also simply bound a <ul> type selector in front of the class name, .resource-list for two latter selectors - as :not() isnt supported below IE9: fiddle

.resource-list[data-type="jpg"] li a,
ul.resource-list li a[data-type="jpg"]  {
    background-image: url(jpg.png);
}

.resource-list[data-type="pdf"] li a,
ul.resource-list li a[data-type="pdf"]  { 
    background-image: url(pdf.png);
}

Comments

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.