This is what Shawn Morton would look like if he had been made of LEGO sMoRTy71.com -- the personal website of Shawn Morton
UPDATE: This blog has been retired as of August 2011. See this post for more information or connect with me on Twitter.
Thursday, September 23, 2004
Changing a layer's opacity
I was working on a project at work yesterday and discovered a really cool CSS attribute that I hadn't used before. I wanted to create a layer that covered the page, but allowed the portion of the page behind the layer to still be visible (but slightly muted).

The attribute is "opacity." Of course, there are different implementations for IE, Mozilla and Safari; but, unlike other browser CSS differences, this one is actually easy to implement. See the code below:

For IE: filter:alpha(opacity=50);
For Mozilla: -moz-opacity:0.5;
For Safari: opacity: 0.5;

You can place any or all of these in your CSS class depending on which browsers you care about. For example, to make a white box that has 50% transparency (and works in all 3 browsers), you could create the following class:

.whitebox {
background-color:#ffffff;
filter:alpha(opacity=50);
-moz-opacity:0.5;
opacity: 0.5;
width:350px;
height:350px;
}

IE requires a height and width attribute in order for their filter to render the transparency.

About Shawn Morton

Married father of 6; VP of Social Media at JPMorgan Chase; gluten-free; gadget enthusiast; hair metal aficionado; #Movember man View more on LinkedIn.