0

I have a problem with my background image and text. I only want to apply the opacity to background image but mine, the text was affected.

HTML

<div class="content">
        <div class="box">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </div>
</div>

CSS

.content{
    background: url(../img/103_n.jpg) left top no-repeat, url(../img/103_n.jpg) right bottom no-repeat;
    opacity: 0.5;
}

.content .box p{
    opacity: 1;
}
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Ven
  • 431
  • 5
  • 12

4 Answers4

3

There is no background opacity property but you can fake it with pseudo element:

.content {
  position: relative;
}

.content::after {
  content: "";
  background: url(../img/103_n.jpg) left top no-repeat, url(../img/103_n.jpg) right bottom no-repeat;
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}
Miroslav Jonas
  • 5,407
  • 1
  • 27
  • 41
  • `z-index: -1` actually just hid the image from me - maybe it was because the had a bg-color and the image was just hiding behind the body. Regardless, I had to throw `z-index: 1;` on the non-pseudo background element and keep `z-index: -1;` on the pseudo element. Side not: using just `:` as opposed to `::` is supposed to support at least IE 8, but maybe even further back. – Shmack Oct 12 '22 at 20:07
0

Follow this link How can i change background image opacity without changing on div content?

.content {
  position: relative;
}

.content::after {
  content: "";
  background: url('https://images.pexels.com/photos/259915/pexels-photo-259915.jpeg') left top no-repeat, url('https://images.pexels.com/photos/259915/pexels-photo-259915.jpeg') right bottom no-repeat;
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}
<div class="content">
        <div class="box">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </div>
</div>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

You can using before (or) after and set opacity

    .content {
      position: relative;
    }

    .content ::after {
    content: "";
    position: absolute;
    background:#000 \also give any color for background;
    opacity: 0.5;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width:100%;
    height:100%;  
    }
0

To be able to separate the parent opacity from its children, you need to use a pseudo selector for the parent:

.content:after {
    content: '';    
    background: url(../img/103_n.jpg) no-repeat, url(../img/103_n.jpg) right bottom no-repeat;
    opacity: 0.5;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
Demian
  • 536
  • 5
  • 26