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
  • 3,871
  • 11
  • 28
  • 48
Ven
  • 381
  • 4
  • 11

4 Answers4

2

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
  • 4,120
  • 1
  • 24
  • 38
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
  • 23,011
  • 7
  • 48
  • 55
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
  • 394
  • 3
  • 19