tags:

views:

56

answers:

4

I want to have a div that is semi opaque filled with text that is not opaque at all. My problem is that when I make the div semi opaque the text is semi opaque too. Is there way to have my text appear as it normally would?

Below is what I'm using now:

.opac
{
    opacity:.2;
    background-color:black; 
}
A: 

IIRC, you have to get tricky.

Essentially, you want to create the semi-transparent div, empty. Then put your text in another div and position it so it lies on top of the first.

timdev
i've used my daily up-vote allowance, but this solution definitely deserves one - there are problems with this approach though - for example, you might have a bunch of dynamic text that you want to put inside your transparent div, and don't know how high to make your div - I can think of three ways to solve those issues 1) **use tables**, 2) **JavaScript**, 3) I know it's a totally whack idea, but you could render your text twice, once inside the div and then once outside it making sure that you position the text that is initially inside the div off-screen so that only the opaque text is seen
stephenmurdoch
@stephenmurdoch - clever idea, #3. You could also just set the color of the text in the semi-transparent div to the same color as the div's background. Should be invisible to the user, since the opaque-text div sits on top.
timdev
+1  A: 

No, there isn't.

Either use a semi transparent 24-bit PNG to create a semi transparent background for an opaque element, or layer two elements on top of each other.

Guffa
There is a way, it's just not well-supported right now but may be in a couple of years: give the background an `rgba()` value.
BoltClock
@BoltClock's a Unicorn: Yes, there is a lot of useful stuff in CSS 3, but we are still struggling with IE 6 users, so there will be some time until we have got enough IE 6, IE 7 and IE 8 users to upgrade to IE 9...
Guffa
@Guffa: yeah, I'm just pointing out that there is a way. I still recognize our old-browser support woes :)
BoltClock
A: 

Even though this has already been answered, here's another option that won't require images, but does require jQuery.

jQuery

var c = $('#container');
$('#transparent').css({
    width: c.outerWidth(),
    height: c.outerHeight(),
    top: c.offset().top,
    left: c.offset().left
});

CSS

​#transparent {
    background-color: black;
    position: absolute;
    z-index:-1;
    opacity: 0.2;
}

HTML

​<div id='container'>
Text
<div/>
<div id='transparent'>
</div>​

Try it out

http://jsfiddle.net/yEmUn/

Robert
A: 

You may use 2 DIVs (none of them either's child). The bottom layered DIV would be semi transparent and the top layered DIV would be for holding the text.

WGF