不影响文本的背景图像不透明度[复制]

问题描述 投票:0回答:4

我的背景图片和文字有问题。我只想将不透明度应用于背景图像,但我的文本受到了影响。

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;
}
html css background-image opacity
4个回答
0
投票

点击此链接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>

2
投票

没有背景不透明度属性,但您可以使用伪元素伪造它:

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

0
投票

您可以在之前(或)之后使用并设置不透明度

    .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
投票

为了能够将父不透明度与其子元素分开,您需要为父元素使用伪选择器:

.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%;
}
© www.soinside.com 2019 - 2024. All rights reserved.