Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


6,232 Points

Setting opacity in the background image

Hi guys, I was wondering if there was some css shorthand to alter the opacity in just the background image. here is what I have so far

background: $black url('/images/fashionpic.jpeg')no-repeat;

3 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

position your div relative, and then set :after for your div absolute with a background image and an opacity.

<style type= "text/css">
#main {
   position: relative;
  height: 300px;
#main:after {
    content : "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    background-image: url(http://baseballwallpapers10.net/wp-content/uploads/images/7d/cleveland-indians-0.jpg); 
    width: 100%;
    height: 100%;
    opacity : 0.2;
    z-index: -1;

<div id="main">foo</div>

Live in CodePen

I did just reread your question and notice you asked specifically for a shorthand version. A shorthand version doesn't exists of yet for this.

David Wible
David Wible
7,473 Points

Instead of a bunch of extra code. The main method is to alter the background image in photo editor. :o) But Andrew's answer is correct as well, but I do seem to remember having to add, "filter: alpha(opacity=20);" as well to get it to work in certain browsers. Editing the photo makes it work no matter what. ;O

Andrew this was really helpful top me, thanks!