3

You have an object (span or div e.g.) with text. And this text should mask out the colored background of the object so that you can see the very background (of body element e.g.).

I know there is "mask-image". But they don't want to develop this CSS3 technique further. Is there a Javascript solution maybe so anyone can see this?

Is there any chance to do this cross-browser?

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
quadronom
  • 652
  • 1
  • 5
  • 15

2 Answers2

0

It looks like the closest you can get to full cross-browser support (without using images for each character in the text) is the very-limited tricks shown here. These tricks wouldn't work with a photographic bg image though.

If you don't require support for IE8 or earlier, you could investigate doing this in SVG or Canvas. Note however that Android prior to 3.0 doesn't support SVG, which rules out as much as 85% of existing Android devices.

Webkit has a proprietary background-clip:text style, but it only works on Chrome and Safari.

Using color:transparent for the text turned out to be completely useless for this.

On the whole, the best bet may be canvas, especially if you can find a JS library or jQuery plug-in that provides a VML alternative for IE8 and earlier (assuming the VML solution supports text masks), or if you can provide a graceful-degradation fallback option for IE8 and earlier that looks adequate even without a text mask.

Another solution that's easily overlooked: If you can dynamically render an image on the server using the current text on the page (caching it as needed), and then serve that image to the client, then there are no cross-browser issues to deal with. This could possibly be done in PHP using the GD graphics library, for instance.

Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
  • Well, no. Please tell me more! – quadronom Aug 11 '12 at 23:42
  • Added a few more notes above. Is there a particular solution you're considering? – Matt Coughlin Aug 12 '12 at 00:13
  • Your link is very interesting - but it is unapplicable for my case here. `background-clip:text` is the same like `mask-image`. Won't work in Firefox. IE doesn't matter anyway... ;) Imagerendering in PHP is really good idea. Didn't though of it. – quadronom Aug 12 '12 at 12:39
  • But working with canvas or svg seems really the best idea to me. I found this [link](http://dev.w3.org/SVG/profiles/1.1F2/test/harness/htmlObject/masking-path-11-b.html), which really the thing I looked for, but I don't actually know how to do this. – quadronom Aug 12 '12 at 13:10
  • Do you require Android support though? Because if you do, that pretty much forces choosing canvas over SVG. – Matt Coughlin Aug 12 '12 at 13:29
0

Can't think of a technique to do this in javascript or html. However, an approach that could be used (although not entirely desirable - I realize that this is going to be time consuming) would be to create a transparent .png for each letter, set the background of the letter to transparent and the rest of the image to some color, and then use those. You would have to make a whole set for each color you wished to use. You could also just make an image which was one word in a .png and use that which would be easier. This would be the only current way to get this to work with all browsers including older ones such as IE6 where the newer techniques are not available.

The reason, in my opinion, that this will not currently work, is that when you have text inside of a <div> or <span> even if the text is transparent, it would merely default to the color of the <div> or <span>. If their background was set to transparent, then nothing would even show up. Conversely, if their color was set, the text would not show up because it would merely inherit that color.

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Thanks for your answer. I also thought of this possibility. The problem is the SEO issue and also clients can't change it by their own. – quadronom Aug 11 '12 at 23:41
  • @quadronom - As for the SEO, you could just have text which mirrored the effect in there which had `visibility: hidden; position: absolute;` set after page load. As for clients, if you went the whole word approach then that would be a large issue. If you made a whole array of 26 letters, and perhaps some numbers and symbols, then you could convert their text to the `.png`. Not really ideal, but perhaps it could work. Seems like a lot of effort for not much payoff. – Travis J Aug 11 '12 at 23:44
  • Kerning (getting the correct spacing between letters) becomes an issue when using images for letters, especially if the text is editable on the server. – Matt Coughlin Aug 11 '12 at 23:44
  • @MattCoughlin - It would definitely require a deft hand in the graphics department. Obviously a space image would need to be made also for separation between words. – Travis J Aug 11 '12 at 23:46