treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Transparent Background Images

Last updated on:

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;   
}


โ€‹

Reference URL

View Comments

Comments

  1. Why not use

    top: 0; bottom: 0; left: 0; right: 0;

    instead of fixed width and height?

  2. Permalink to comment#

    What is the browser support for this snippet?

    • NONAME
      Permalink to comment#

      Known support: Firefox 3.5+, Opera 10+, Safari 4+, Chrome 4+, IE 9+

  3. Permalink to comment#

    I use
    filter: alpha(Opacity=90);
    opacity: 0.9;

  4. Nice!
    Thanks for your trick and for this star here in comment box :D.

  5. Permalink to comment#

    Any hack to work it with IE 7 and 8?

  6. Permalink to comment#

    This works great, but I’m not able to select/click the text in the ‘top’ div. Anyone found a fix for that?

  7. mary
    Permalink to comment#

    how do you change the opacity of the background image only? trying this made everything but the background image change opacity..

  8. Permalink to comment#

    nice tricks, this help me :)

  9. Permalink to comment#

    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.

  10. babyice
    Permalink to comment#

    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

  11. Bart
    Permalink to comment#

    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).

    • ian.pvd
      Permalink to comment#

      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?

    • Jt

      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.

  12. Cameron
    Permalink to comment#

    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.

  13. 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.

  14. Sneha

    Hello,
    How could I make the background image transparent other than using background : transparent or background : none

Leave a Comment

Use markdown or basic HTML and be nice.