I have a CSS template where I want to use a minimal amount of attributes within some HTML markup, while allowing for easy customization of that markup via classes (not IDs) later, if needed.
<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>
The problem is that due to CSS specificity, I am faced with either making all of my selectors classes anyway, or else forcing any stylesheet modification of my content to be extremely specific:
<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>
Is there a way to force a specificity yield without using !important
, for example hypothetically:
@yield to classes {
[data-role = "a"] { }
[data-role = "a"] > :first-of-type { }
[data-role = "a"] > :last-of-type { }
/* etc */
}
or
@specify('[data-role="a"] > :last-of-type' , 10) { ... }
where 10
is the lowest internal specificity assigned, etc.
Or am I just forced to use classes everywhere?
> li
to override the earlier> :first-of-type
? Then you can just write> li:nth-child(n)
instead. – Downingattribute > element
selector. I know how to increase specificity the regular ways. – Roughandready