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! While you're at it, check out some resources Treehouse students have shared here.

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

CSS

modify background image opacity without touching <P> opacity

Hello,

I am trying to modify a background-image opacity, but the function "opacity" change my image + text opacity. There is a way to change the opacity without touching the P?

I tried to put an class to the paragraph but doesnt work.

Thanks This is my code

.extrait { text-align: left; padding: 18% 24%; border-top: 10px solid #87CEEB; margin: 105px 0 60px; opacity: 0.6; filter: alpha(opacity=40); /For IE8 and erlier/ background: #434a52 url('../img/PP432.jpg') no-repeat center; background-size: cover; box-shadow: inset 0 0 50px 10px rgba(0,0,0, .8); border-radius: 10px; }```

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

There is no way to modify the opacity for a "background-image" (it works if you want to have a transparent solid colour, however). As Devin said, you can put your image in a separate element and then position it behind the element you want to have a transparent image background. A more complicated way would be to use a pseudo element on your ".extrait" selector, like this:

.extrait::before {
 content: '';
 width: 100%;
 height: 100%;
 position: absolute;
 top: 0;
 left: 0;
 background: url('../img/PP432.jpg') no-repeat center;
 display: block;
 z-index: -1;
 opacity: (a number between 0 and 1, such as 0.55);
}

Be aware, however, that you will have to add "position: relative;" to your parent ".extrait" element, and that you may have to add "display: inline-block" if you don't want the paragraph element to stretch all the way across the screen.

Devin Scheu
Devin Scheu
66,191 Points

Yes, give a class to your image then directly apply CSS to that class.