CSS opacity and child elements
- by Rob
<style type="text/css">
div#foo {
background: #0000ff;
width: 200px;
height: 200px;
opacity: 0.30;
filter: alpha(opacity = 30);
}
div#foo>div {
color: black;
opacity:1;
filter: alpha(opacity = 100);
}
</style>
<div id="foo">
<div>Lorem</div>
<div>ipsum</div>
<div>dolor</div>
</div>
In the above example, the opacity of div#foo is inherited by child elements, causing the text to become nearly unreadable. I suppose it's wrong to say it is inherited, the opacity is applied to the parent div and the children are part of that, so attempting to override it for the child elements doesn't work because technically they are opaque.
I typically just use an alpha png background image in such cases, but today i'm wondering if there's a better way to make a background of a div semi-transparent without affecting the contents.