Seeking a solution with CSS selectors, I am faced with the challenge of creating automatic color overrides for Tailwind to ensure our site meets WCAG guidelines consistently.
A tailwind plugin I developed can fix this issue automatically, but I have realized that my current approach to targeting CSS selectors is incorrect.
For instance, when the color light-grey is applied to white, I intend to automatically adjust it to black.
My current method involves using the following selectors:
.bg-white .text-light-grey {
color: black;
}
However, this approach presents complications. It works well in this HTML structure:
<div class="bg-white">
<span class="text-light-grey">This is now black.</span>
</div>
But it disrupts this structure:
<div class="bg-white">
<span class="text-light-grey">This is now black.</span>
<div class="bg-black">
<span class="text-light-grey">This is also now black, which is unintended as it resides within a black container!</span>
</div>
</div>
I need guidance on how to adjust the CSS selectors so that if another background is specified in between them, the text color remains intact without corruption.
Perhaps something like this:
.bg-white :not(some fancy way to ensure no other backgrounds come between bg-white and the text color) .text-light-grey {
color: black;
}
Is there a solution utilizing class~=
or possibly the :has()
selector?