I am trying to create a CSS container that has a red overlay with dynamically assigned with using javascript.
The problem is I need to change the color of text which is overlapping red background to white while keeping the other part still black.
I found plenty of solutions to invert colors using mix-blend-mode: difference
property, but this makes the non-overlapping text matching the color of the background which is not what I want.
Here is the closest example that I could achieve.
2
Answers
You just need to change this code:
To:
This will make 60% of the text black and the other 40% text color aqua.
Addressing this part of your question:
If I understand correctly you have a piece of text that you wish to be coloured as absolute white (regardless of any blending) in the region where it’s overlapping the
.bg
element background and to be coloured as black otherwise. I’m not perfectly sure then why in your code snippet you have the text coloured asaqua
then.Since you are not strictly dealing with colour blending but you want to replace the overlapping colour with a fixed one (white), the only solution I can come up with is just having two perfectly equal text lines. One of those will be nested inside the
.bg
element (and cropped) while the other one won’t.The colour driving the text based on the overlapping condition is set inside two css variables.
And this is a second version of the same approach that strips down the complexity to the bare minimum hiding the details by using pseudo-elements and data attributes: