Css3 opacity: transparency of an element in css

Tuesday 27 January 09 by Onsitus in CSS3

The css3 property opacity allows to change the transparency of an element.

The property opacity needs a value in between 0.0 (equal to 100% transparency) and 1.0 (no transparency).

Example for a css transparency of 60%:

opacity: 0.6;

opacity is not supported by Internet Explorer for which we can use the property ‘alpha filter’ implemented by the Microsoft.
This property ‘alpha filter’ needs a value in between 0 (completely transparent) and 100 (no transparency).

Example of the ‘alpha filter’ for a transparency of 60%:

filter: alpha(opacity=60); /*for Internet Explorer 6/7*/
filter: “alpha(opacity=60)”; /*for Internet Explorer 8*/

NB: Internet Explorer 8 requires a Doctype in Strict mode to work

Next, I created a few examples of the property opacity for images, hover effect and text. Attention that for the last example with the text, Internet Explorer requires other parameters to render the effect of transparency properly.

1. Opacity of 50% on a image:

The css rules:

img{
opacity:.50;
filter:alpha(opacity=50);
filter: “alpha(opacity=50)”;
}

css opacity

2. Opacity of 100% on a image, equal to no transparency, only to demonstrate the effect of the maximum value:

The css rules:

img{
opacity:1;
filter:alpha(opacity=100);
filter: “alpha(opacity=100)”;
}

css opacity

3. Effect of the opacity from 100 to 40% on the hover state:

The css rules:

a:hover img{
opacity:.40;
filter:alpha(opacity=40);
filter: “alpha(opacity=40)”;
}

css opacity

4. Effect of the opacity from 40 to 100% on the hover state:

The css rules:

a img{
opacity:.40;
filter:alpha(opacity=40);
filter: “alpha(opacity=40)”;
}
a:hover img{
opacity:1;
filter:alpha(opacity=100);
filter: “alpha(opacity=100)”;
}

css opacity

5. Difference between a normal text and a text with opacity of 60%:

The css rules:

I impost a font measure of 2em (200%) to better see the effect:

p.opacity{
font-size:2em;

The following to lines are necessary for Internet Explorer…

I impost a width of 100% as Internet Explorer does not apply the effect to a text if a width or height is not specified o also if the text is not positioned such as with the float.

width:100%;

Still for Internet Explorer, I impost a white background. With no background color, the edgess of the text are slightly distorted. Adding a color to the background correct this problem with IE.

background-color: #FFFFFF;

The opacity effect as explained before:

opacity:.60;
filter:alpha(opacity=60);
filter: “alpha(opacity=60)”;
}

The result:

Css property opacity 100

Css property opacity 60

Note: for older browsers version was in use the equivalent css3 ‘-moz-opacity’. Its use is no longer necessary.

Last update: May 2009

16 comments

Stay updated with our RSS Feed

{ 12 trackbacks }

CSS3 Exciting Functions and Features: 30+ Useful Tutorials | Noupe
05.21.09 at 21:18
30个CSS3新功能的实用教程 « SonicHtml工作室- PSD转HTML / XHTML,CSS / W3C验证 / 多浏览器支持 / WordPress模板 / Joomla模板
05.29.09 at 11:59
CSS3 Exciting Functions and Features: 30+ Useful Tutorials
07.22.09 at 16:36
Opacidad en Textos e Imágenes con CSS | Area Photoshop
08.08.09 at 03:39
Webbrelaterat » Blog Archive » N
08.18.09 at 12:10
AMB Album » CSS3 Exciting Functions and Features: 30+ Useful Tutorials
08.23.09 at 06:50
CSS3的新功能快速体验和应用 | CssRainBow.cn
09.21.09 at 07:11
IE8 img opacity css conundrum - Any gurus here? - Webmaster forum
10.01.09 at 14:40
Ce que nous réserve CSS3 en 2010 | traffic-internet.net
12.16.09 at 19:39
Css Christmas card
12.21.09 at 01:59
Ce que nous réserve CSS3 en 2010 « Mon blog
01.13.10 at 16:00
Agence communication Offshore Marrakech-Maroc » Blog Archive » Ce que nous réserve CSS3 en 2010
01.13.10 at 19:55

{ 4 comments… read them below or add one }

Jürgen Jeka 05.08.09 at 13:14

Hello,

I think “-moz-opacity” is dead and useless these days. Browsers since Mozilla 1.7.1/ Firefox 0.9 (June 2004) are fine with plain “opacity”. Firefox 3.5 drops support for “-moz-opacity” completely.

See https://developer.mozilla.org/en/CSS/opacity

j.j.

Jürgen Jeka 05.08.09 at 13:17

btw, IE8 in strict mode supports filter values only in quotes:
filter: “alpha(opacity=60)”

Onsitus 05.11.09 at 06:53

Thank you for the update. I modified the article! ;)

cba 10.14.09 at 16:20

try to validate some of this code in the css3 validator ( http://www.w3c.org )

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Module CSS3 Web Fonts and property @font-face

Next post: How to add a flash background to an html element