Use CSS to hide elements by class name dependent on body element attribute

14 hours ago 2
ARTICLE AD BOX

I like to implement a kind of view-mode switch by hiding all elements of a certain class but show only those matching another class named by a body element attribute. Like so:

<body mode="mode_2"> <p class="mode_switched mode_0">Mode 0 - Hidden!</p> <p class="mode_switched mode_1">Mode 1 - Hidden!</p> <p class="mode_switched mode_2">Mode 2 - should be visible</p> <p class="mode_switched mode_3">Mode 1 - Hidden!</p> </body>

So now body sets mode=mode_2, and I expect only the p element with class mode_2 to be visible.

While it is possible to write a matching CSS for this case, I like to have a generic CSS that will handle all future mode_... classes without having to expand the CSS every time a new mode is introduced.

Is there any way to do this with CSS only?

Read Entire Article