Transparent Backgrounds with CSS

Posted Nov 23, 2008 // 1 comments
Irakli:

How do you create transparent-background DIVs, that works across browsers? If your first thought is “I have no clue” or “just use opacity CSS property”,read on for some useful tips.

The best way, in my opinion is CSS 3.0: rgba(R,G,B,ALPHA), but since only Safari supports CSS 3.0 currently (to the best of my knowledge, but you bet IE does not) that won’t work.

You can also try using the combined CSS properties:


filter:alpha(opacity=50);-moz-opacity:.5;opacity:.5;
and think to yourself: “OK, I used three properties for one thing, so the cross-browser demons should be happy, right? What can possibly go wrong?”

Unfortunately, a lot can go wrong. Namely, all child elements of the div, the background of which you just made transparent, will also become transparent. Even if you have no children divs: all your text, within the div, will be transparent.

What to do? What to do? You rush to Google and find all kinds of MMFW-CSS (make-me-feel-worse, CSS) tricks that do not help – like the one suggesting the inside elements should not really be children of the initial div, but be overlapped using, nothing less than, an absolute positioning. Yeah, like that really helps: adding the pain of absolute positioning to the already existing list of pains with your page’s CSS! But, do not despair! There is a solution. Here is a working solution, tested in most browsers:
How do you create transparent-background DIVs, that works across browsers? If your first thought is “I have no clue” or “just use opacity CSS property”,read on for some useful tips.

The best way, in my opinion is CSS 3.0:

rgba(R,G,B,ALPHA), but since only Safari supports CSS 3.0 currently (to the best of my knowledge, but you bet IE does not) that won’t work.

You can also try using the combined CSS properties: filter:alpha(opacity=50);-moz-opacity:.5;opacity:.5; and think to yourself: “OK, I used three properties for one thing, so the cross-browser demons should be happy, right? What can possibly go wrong?”

Unfortunately, a lot can go wrong.

Namely, all child elements of the div, the background of which you just made transparent, will also become transparent. Even if you have no children divs: all your text, within the div, will be transparent.

What to do? What to do? You rush to Google and find all kinds of MMFW-CSS (make-me-feel-worse, CSS) tricks that do not help – like the one suggesting the inside elements should not really be children of the initial div, but be overlapped using, nothing less than, an absolute positioning. Yeah, like that really helps: adding the pain of absolute positioning to the already existing list of pains with your page’s CSS!

But, do not despair! There is a solution.

Here is a working

solution, tested in most browsers:


<!—[if lte IE 6]>
div.transp {
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src='/full
/path/to/70percent.png');
}
<![endif]—>

div.transp[class] { background-image:url(70percent.png); }

The 70percent.png is a PNG image with 70% percent alpha transparency.

The “filter:” CSS style fixes IE’s shortcoming of having no clue about PNG Alpha channels. The [class] attribute of the .transp class hides that style from IE6. Otherwise you’d get a solid-color background.

You have to hide filter: attribute from IE version > 6 since they do understand the [class] thingie and do handle aplha channels correctly, so they will both apply the filter and the transparent PNG, giving you a comletely different color than what you expected.

About Irakli

As our Director of Product Development, Irakli revels in developing packaged, turn-key solutions using open-source technologies and cutting-edge, semantic APIs. He brings vast expertise in the areas of: product design, development, ...

more >

Read Irakli's Blog

Comments

by Jeremy Edgell (not verified) on Mon, 11/23/2009 - 11:55

a little off topic, but useful, maybe?

I've been working with a lot of designers recently who have been putting semi-transparent backgrounds over images and then text over the semi-transparent backgrounds. I blogged my approach here: http://kiveo.net/transparent-backgrounds/

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.