我有一个CSS模板,我想在一些HTML标记中使用尽可能少的属性,同时允许稍后通过类(而不是ID)轻松自定义该标记。
问题在于由于CSS特异性,我要么不得不把所有选择器都变成类,要么强制任何对我的内容进行样式表修改的操作变得非常具体和明确。
有没有一种方法可以强制 specificity 产生作用,而无需使用 !important ,例如假设情况下:
<ul data-role = 'a'>
<li>A</li>
<li>B</li>
</ul>
<style>
[data-role="a"] { /* ... */ }
[data-role="a"] > :first-of-type { /* ... */ }
[data-role="a"] > :last-of-type { /* ... */ }
</style>
问题在于由于CSS特异性,我要么不得不把所有选择器都变成类,要么强制任何对我的内容进行样式表修改的操作变得非常具体和明确。
<style>
[data-role="a"] > li { } /* custom overriding CSS */
</style>
vs
<ul class = 'a'>
<li class = 'a-top'>A</li>
<li class = 'a-bottom'>B</li>
</ul>
<style>
a {/* */}
a-top {/* */}
a-bottom {/* */}
</style>
有没有一种方法可以强制 specificity 产生作用,而无需使用 !important ,例如假设情况下:
@yield to classes {
[data-role = "a"] { }
[data-role = "a"] > :first-of-type { }
[data-role = "a"] > :last-of-type { }
/* etc */
}
或
@specify('[data-role="a"] > :last-of-type' , 10) { ... }
其中10
是分配的最低内部特定性等级,等等。
还是说我被迫在所有地方使用类?
> li
覆盖前面的> :first-of-type
吗?那么你可以直接写> li:nth-child(n)
。 - Mr Lister