views:

284

answers:

3

I would like to know if there is any way I can apply 100% transparency to text so that we can see the background picture of the page.

i.e. imagine I’ve got a <div> with a white background, and a background image on <body>. I’d like to set the text inside the <div> so that the background image on <body> can be seen through the text, despite the white background on the <div>.

I could probably use an inverted font but I would prefer a better way to do it if there is one.

+1  A: 

I’m not exactly clear what you’re asking (100% transparency means that something’s invisible, and invisible text isn’t generally a great idea), but in general:

  1. The CSS opacity property applies to an entire element, not just its text. So if you have this HTML:

    <div class="opacity-50">
        This is a div with a background colour and 50% opacity
    </div>
    

    And this CSS:

    .opacity-50 {
        background: #ccc;
        color: #000;
        opacity: 0.5;
    }
    

    Then both its background and its text will have 50% opacity.

  2. rgba colour values allow you to specify semi-transparent colours. So, if you have this HTML:

    <div class="text-opacity-50">
        This is a div with semi-transparent text
    </div>
    

    And this CSS:

    .text-opacity-50 {
        background: #ccc;
        color: rgba(0,0,0,0.5);
    }
    

    Then only its text will have 50% opacity.

I think rgba colour values are supported by slightly fewer browses than opacity.

Paul D. Waite
What I meant was:[transparent-text/InsideA/colored-Div]-[colored-background]thanks for the explanation
mnml
I’m not super-clear on what that combination of punctuation means either, to be honest.
Paul D. Waite
I was just trying to explain the "layers" of what I want to do
mnml
+4  A: 

Does it have to be dynamic? The only way to do that is with an image with transparency (GIF or, better, PNG).

I'm not sure if this is what you want, but will explain it anyway.

Situation: you have a non plain background that you want to bee seen through your text.

Solution: no CSS is coming to the rescue for this. You'll have to use your trusty image editor to do something like this

With a background set this looks like this

In order to do this, you have to create a layer with text

and another layer that will be the negative of your text, ending up with something like this (background added for clarity)

This could allow you to have some interesting effects, but if you want it to be dynamic, you'll have to generate the images on the fly serverside.

This kind of trickery is currently impossible with pure CSS (might be possible with Javascript).


Edit

Seeing Paul's find on webkit got me thinking on how to fake that behavior in Firefox, Opera and IE. So far I've had good luck using the canvas element on Firefox, and I'm trying to find some behavior in filter:progid:DXImageTransform.Microsoft.

So far with canvas, this is what I did

<html>
<body>
<canvas id="c" width="150" height="150">
</canvas>
<script>
ctx = document.getElementById("c").getContext("2d");
// draw rectangle filling the canvas element
ctx.fillStyle = "red";
ctx.fillRect(0,0,150,150);

// set composite property
ctx.globalCompositeOperation = 'destination-out'; 
// the text to be added now will "crop out" the red rectangle
ctx.strokeText("Cropping the", 10, 20);  
ctx.strokeText("red rectangle", 10, 40);  

</script>
</body>
</html>

and this is what you get

by using a detination-out compositing and drawing text on the canvas.

voyager
Oh! Now I get what mnml was asking. Sorry, seems obvious now.
Paul D. Waite
+4  A: 

Ah — if you’re talking about “punch-through” transparency, no, CSS doesn’t do this.

Except for WebKit (the rendering engine in Safari and Chrome), which has a totally custom, made-up-by-Dave-Hyatt, not-even-in-CSS-3 property value, -webkit-background-clip: text;.

No other browser other than Safari and Chrome supports it.

Paul D. Waite
+1 Interesting find! Confirmed on Chrome. No other browser (not even Konqueror) supports it right now. I imagine that you can fake it with some trickery on IE, and on Firefox it could be done with javascript (I know I've seen something like that somewhere). As for Opera...
voyager
Very nice, I would use that if firefox was using webkit. I'm going to stick with voyager's answer for now I guess.
mnml
Yeah, WebKit is the place where CSS innovation is happening fastest. (Although most of their innovations do seem to be Mac OS X internals being exposed by CSS, which I guess is sort of cheating — Mozilla doesn’t sell an operating system, so they don’t have a similar body of code to call on.)
Paul D. Waite
Oh — could you fake this with JavaScript in Firefox? I can’t imagine how.
Paul D. Waite
On IE, I imagine that there is something in `filter:progid:DXImageTransform.Microsoft` that you could use. On FF you might be able to do some Canvas or SVG trickery.
voyager
For FF: https://developer.mozilla.org/en/Drawing_text_using_a_canvas + https://developer.mozilla.org/en/Canvas_tutorial/Compositing (source-out compositing)
voyager
Ah yes, intriguing.
Paul D. Waite