Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opacity of background-color, but not the text [duplicate]

Tags:

css

opacity

How do I make the cross-browser (including Internet Explorer 6) transparency for the background of a div while the text remains opaque?

I need to do it without using any library such as jQuery, etc. (But if you know of a library that does it I'd love to know so I can look at their code).

like image 901
Nir Avatar asked Mar 12 '09 09:03

Nir


People also ask

How do I change the opacity of the background image but not the text?

One approach you can use is to put the background-image styles in a pseudo-element of the parent element. Since the pseudo-element is a sort of child of the parent, you can change the opacity of it without affecting the text content.

How do I change the background color without affecting text?

Simply use rgba to define your background color and specify opacity with it at the same time by adjusting the last value, for alpha, in your rgba code. For scaling, bringing the value closer to 0 increases transparency. To simplify your HTML, you don't even need the parent div around your block of text to do this.

How do you apply the background color to only half of the text?

We basically use a data-content attribute with the same content as the span holds, and then copy this to a layered :after element which displays it. We then hide the original text and apply a 50% height to the after element, this way the background color can only be applied to the bottom half.

How do I set opacity for background only?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.


1 Answers

Use rgba!

.alpha60 {     /* Fallback for web browsers that don't support RGBa */     background-color: rgb(0, 0, 0);     /* RGBa with 0.6 opacity */     background-color: rgba(0, 0, 0, 0.6);     /* For IE 5.5 - 7*/     filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);     /* For IE 8*/     -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)"; } 

In addition to this, you have to declare background: transparent for IE web browsers, preferably served via conditional comments or similar!

via http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/

like image 197
Austin Adams Avatar answered Dec 09 '22 08:12

Austin Adams