I’m trying to create a filterable list of items that shows unique values when a combination of filters are checked. I need to be able to do this with only with CSS and HTML because the internal corporate wiki page where I’m building this does not allow the "script" tag.
My list is : A B C
My Values are A, B, C, D
I want value A to show when just filter A is checked, value B to show when just filter B is selected, and so on.
I want value D to show only when filters A, B & C are all checked.
This is what I currently have:
.items>[data-type] {
display: none;
}
input[name=a]:checked~.items div[data-type=a],
input[name=b]:checked~.items div[data-type=b],
input[name=c]:checked~.items div[data-type=c] {
display: block;
}
<input type="checkbox" name="a" style="display: inline-block;"> A
<br/><input type="checkbox" name="b" style="display: inline-block;"> B
<br/><input type="checkbox" name="c" style="display: inline-block;"> C
<hr/>
<div class="items">
<div data-type="a">A</div>
<div data-type="b">B</div>
<div data-type="c">C</div>
</div>
I’m not sure how to specify with the CSS that data-type=d is the result of input a, b, and c being checked.
2
Answers
You were in the right way
Here is a verbose version. Pretty verbose and ugly. Starting to get a bit MUCH when we do a show/hide of AB, BC, ABC etc. I did not take time to dive deep but there might be a simpler method using just CSS – or just enlist JavaScript to do some of this.
Here is a verbose set of notes:
Since this is a cascade (C in css) we go top/down
Format the
label
with a left pad between it and the checkbox:Format the
label
with a new line after:Hide them all initially:
Now show the single checked target:
Now when there are TWO (or three) checked we target that ones siblings:
Now when two are checked we turn off visibility of the others:
Trick here is the
~.items :is([data-type])
finds the non-targeted siblings to turn those off.THEN when three are checked we just show the ABC one.