Want inline semitransparent text background with no overlap?
Here's how on @codepen
https://codepen.io/thebabydino/pen/gbOwxGL?editors=1100
(you can see the overlap problem and the desired result below)
The `filter` combines 2 `feColorMatrix` techniques:
using a channel as an alpha mask
solid fill
using a channel as an alpha mask - the bottom row lets us choose which input channel is used for the output alpha; if we set one of the RGB channels (first 3 values) to 1 and zero all else on the last row, that channel is used as the alpha mask.
Here is the interactive demo illustrating this on @codepen
https://codepen.io/thebabydino/full/OJqZvQO
solid fill - first three values on the last column are set to the desired RGB values (decimal representation of percentage RGB)
Interactive demo on @codepen
https://codepen.io/thebabydino/full/LYamwrz
@anatudor hah, i requested something like this ages ago on the CSS WG list ... https://codepen.io/patrickhlauke/pen/jOzJwvZ
@patrick_h_lauke Wait, that's a slightly different problem and doesn't even require an SVG filter (like the semi-transparency overlap does). Here's a fork https://codepen.io/thebabydino/pen/ogNzGpw?editors=1100
@anatudor blend mode works in that case against white page background, but it's not quite a general approach for varying page backgrounds etc. just after some way of "merging" all backgrounds together as one unit there that sits behind all the text (and can then be opaque, or transparent, or whatever really). but yeah, can hack it for very specific controlled uses, sure...
@patrick_h_lauke You don't need a white page background. Refresh to see. In general, it works for any text that's super close to either white or black, regardless of the page backdrop. And with the SVG filter method I'm using in my transparency overlap demo, it can be made to work for any text/ highlight combo, like blue text + yellow highlight.
@anatudor Probably worth a comment somewhere that your demo forces pure white text on a black background color, since you're setting color in the filter & using input color to distinguish text vs background.
I'd instead use feComponentTransfer on the alpha channel to normalize the semi-transparent background sections while preserving the opaque text regardless of color:
https://codepen.io/AmeliaBR/pen/XJWjEmM?editors=1100
Only a little annoying because of the component transfer syntax (explained in the pen description).
@anatudor Aside: It's been a while since I've played with SVG filters, and the definitions in the spec (https://drafts.fxtf.org/filter-effects/#feComponentTransferElement) were making my head spin.
Then I remembered, I wrote a whole book on SVG with a chapter on filters, I'll go look up what I said! Except, I didn't actually discuss feComponentTransfer in the book ().
But, my SVG elements & attributes reference page was enough to help jog my memory: https://oreillymedia.github.io/Using_SVG/guide/markup.html#filters
I used `table` not `discrete` to avoid harsh pixel aliasing.
@AmeliaBR But not regardless of alpha. It still shows overlap if alpha is .75 and it breaks the text edges for an alpha of .25 :(
@anatudor Yeah, that's the annoying bit with the syntax. You need to adjust the number of repeats in the `tableValues` attribute to make sure it can cover closer-to-opaque levels.
I hadn't realized that less opaque backgrounds would result in more damage to the text anti-aliasing. But you could make *fewer* repeats in the table function for that.
Since the SVG code is being generated by a pre-processor anyway, I guess you could come up with a function for that? But it's definitely not optimal.