Transparent Background Images
There is no CSS property background-opacity, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it.
div {
width: 200px;
height: 200px;
display: block;
position: relative;
}
div:after {
content: "";
background: url(http://subtlepatterns.com/patterns/bo_play_pattern.png);
opacity: 0.5;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
โ
Why not use
instead of fixed width and height?
What is the browser support for this snippet?
Known support: Firefox 3.5+, Opera 10+, Safari 4+, Chrome 4+, IE 9+
I use
filter: alpha(Opacity=90);
opacity: 0.9;
yeah, but it opacifies everything,not just the background.
then what is your result?
Nice!
Thanks for your trick and for this star here in comment box :D.
Any hack to work it with IE 7 and 8?
This works great, but I’m not able to select/click the text in the ‘top’ div. Anyone found a fix for that?
how do you change the opacity of the background image only? trying this made everything but the background image change opacity..
nice tricks, this help me :)
Mother of god. This is perfect. I could not find a way to apply a background with opacity (which would fit its parent perfectly and not display over content), and thought I’d have to add a presentation div. This is one of the sickest tricks I’ve seen this year. I can expect mure more from my future website visually, thanks to you. Really, thanks, thanks, thanks, thank you so much.
This is the css codes to make the image background transparent!
TRY IT…! :)
background: rgb(255, 255, 255) transparent ;
background: rgba(255, 255, 255, 0.8) ;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#CCFFFFFF, endColorstr=#CCFFFFFF) ;
-ms-filter: “progid:DXImageTransform.Microsoft.gradient(startColorstr=#CCFFFFFF, endColorstr=#CCFFFFFF)” ;
*Only supported by IE
Why use code for background-image opacity?
The easiest way is exporting your .PNG allready with the right transparancy. That way you don’t need to code opacity. And it will be supported by all browsers including IE6 (if using png fix).
How do I change the opacity on mouseover? Without loading a new image or using a sprite.
Or,
How do I set the opacity to some arbitrary percentage on the front end?
Main reason I’d imagine would be to save download time. Semi-transparent PNGs in particular are quite heavy. I am looking at a 10k JPG vs a 100k PNG.
When I do this trick on the over the body element (so I can get a transparent color overlaying the background), it only displays the :after element in the initial view port frame. When I scroll down, it disappears. Any ideas around that?
JSFiddle here illustrating the problem:
http://jsfiddle.net/ccnokes/sqnKb/
Cool trick though.
Cameron, you can set your position from “absolute” to “fixed”. It should fix your problem.
JSFiddle here fix the problem:
http://jsfiddle.net/sqnKb/20/
Hi, how could I reduce the background image opacity inside the textbox? I’ve got a username textbox within a user logo and want to reduce its opacity without effecting the textbox. Thanks before hand.
Hello,
How could I make the background image transparent other than using background : transparent or background : none