简体   繁体   English

CSS属性选择器优先级

[英]CSS attribute selector priority

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)? 任何人都知道如何实现第二个选择器(.resource-list li a [data-type =“XXX”])的优先级高于第一个(.resource-list [data-type =“XXX”] li a )? Without rearranging the CSS? 没有重新排列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/ 看小提琴: http//jsfiddle.net/8bG2j/

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

This should do it. 这应该做到这一点。 Simply use a pseudoclass which is always true like a:not(s) . 只需使用一个总是如下的伪类a:not(s)

It will raise your priority by the priority of the selector inside the :not statement. 它将通过:not语句中的选择器的优先级提高您的优先级。

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). 或者, 如评论中所述 ,当您不想破坏旧浏览器(如IE 8)时,可以在其前面加上ul You can do this if .ressource-list will be always a class of an <ul> tag. 如果.ressource-list将始终是<ul>标记的类,则可以执行此操作。

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 你也可以简单地在类名前面绑定一个<ul>类型的选择器,为两个后面的选择器绑定.resource-list - 因为:not() IE9下面:not() 支持 :not()小提琴

.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);
}

bwoebi's solution is good enough, if you don't care about IE8 (which does not support :not() ) bwoebi的解决方案已经足够好了,如果你不关心IE8(不支持: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/ http://jsfiddle.net/8bG2j/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM