I know I can Select elements by attribute in CSS, i.e. match all elements that have [data-state=42]
. But can I use the dynamic content of a parent's data attribute to select different subsets of its children?
Say I have a nested structure like this:
<ul class="focusable" data-focus="benefit">
<li class="drawback">Puppies can chew up your shoes.</li>
<li class="benefit">But they are very friendly.</li>
<li class="benefit">And will protect you from the mailman.</li>
<li class="drawback">Sometimes they wet on the floor.</li>
<li class="benefit">But their droppings are good for the lawn.</li>
</ul>
With just two states I could set up some CSS to highlight only certain items depending on the value of the data-focus
attribute:
.focusable[data-focus=benefit] > .benefit {
outline: 1px solid green;
}
.focusable[data-focus=drawback] > .drawback {
outline: 1px solid red;
}
That's reasonable because there's only two potential values — benefit
or drawback
— or three if you include unsetting the data-focus
entirely. But in my real world case, I have a much more complicated situation where a visualization could contain an ever-growing number of values I would want to target. Is there a way I could make my CSS more generic somehow?
/* INVALID CSS, just a sketch of desired outcome… */
.focusable[data-focus=assignTo(--focused-class)] > [class~=var(--focused-class)] {
outline: 1px solid blue;
}
Given that the answer to just Is it possible to use CSS vars in CSS3 selectors? is already "no", the above CSS is off to a poor start. Is there any other approach that would let me accomplish this in pure CSS — i.e. without using JavaScript to update styles/classes on any of the child elements?