Forum Moderators: not2easy

Message Too Old, No Replies

CSS Opacity

         

SirTalksalot

4:05 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



Hi all,

I've been trying out the CSS opacity function. I was just wondering if there are any major pitfalls to using it I hadn't considered?

Also, I have a problem with it. When I set a div opacity, and divs nested withing it inherit the same opacity, and I can't seem to over-ride that. Is it just not possible, or is there a way round this?

If there isn't a way round this, maybe they should consider it before it's released as standard code?

birdbrain

4:37 pm on Feb 22, 2010 (gmt 0)



Hi there SirTalksalot,

here is a basic method to remove the total opacity problem...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>

<style type="text/css">
body {
background-color:#00f;
}
#container {
position:relative;
width:440px;
margin:auto;
}
#faded {
width:400px;
height:360px;
padding:40px;
background-color:#f00;
border:4px solid #000;
opacity:0.30;
filter:alpha(opacity=30);
font-size:30px;
font-weight:bold;
text-align:center;
}
#unfaded {
position:absolute;
z-index:1;
left:84px;
top:189px;
width:300px;
padding:10px;
border:2px solid #000;
font-size:30px;
font-weight:bold;
text-align:center;
background-color:#fff;
}
</style>

</head>
<body>

<div id="container">

<div id="faded">this area is effected by opacity</div>

<div id="unfaded">this area is unaffected</div>

</div>

</body>
</html>


birdbrain

swa66

11:30 pm on Feb 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Getting translucency can be set in 2 manners in CSS3:
- the opacity property: [webmasterworld.com...]
- using colors that are translucent themselves [webmasterworld.com...]

The first method is intended to affect the entire stacking context that is created for the element that has been given the opacity statement.

If you want to have just part of an element to be opaque (typically the background), I'd suggest using rgba colors there instead of making the element generate a new stacking context that's entirely made translucent.

The links contain examples.

travissmith

5:17 am on Feb 28, 2010 (gmt 0)

10+ Year Member



The only issue I can think of with using opacity is IE does not support this, even IE8. It is a CSS 3 property which IE 8 is only CSS 2.1 complient. Most other browsers do support it right now. I believe their is an IE dx filter which is propriatary to IE.

Travis Smith

SirTalksalot

1:49 pm on Mar 8, 2010 (gmt 0)

10+ Year Member



Thank you guys!

I found this page too, I think this might have been the one you mentioned anyway.

[webmasterworld.com...]

Can I just ask though. As IE doesn't support translucient colours, I've used the filter for my background colour, and the 'position: relative;' cheat to stop the child elements inheriting the opacity, which works.

Under that in the same CSS element I've added a second background colour rule which firefox can read, which is the translucient colour for the background. SO basically, I've got two background colours in the same element, IE uses the first and ignores the second, while Firefox acknowledges both, but uses the second as it's furth down on the list.

This seems a really ham-fisted way of doing it, but it seems to work perfectly. Are there any flaws to doing this that I haven't spotted? Or was this actually how you were implying it should be done?

All the same, thank you guys, I've learned quite a bit exploring this subject.

StoutFiles

2:14 pm on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anything that IE doesn't support isn't worth the headaches involved with making it work. Unless opacity is crucial to your design I would just avoid it altogether.

SirTalksalot

4:27 pm on Mar 8, 2010 (gmt 0)

10+ Year Member



IN this case though it's actually firefox which has caused the problem. As there's no easy way to prevent child elments from inheriting opacity settings from their parent elemets.

The thing is, it's working now. I just need to know if the way I've done it will cause any problems I haven't considered. Or if relying on one background colour to over-ride another is a bad idea.

My deisgn is set up so that it won't really suffer if Opacity doesn't work. But I think it's a really nice feaure, and am determined to use it more if I can find a consistantly good implimentation for it.