简体   繁体   English

如何在不影响背景颜色的情况下更改背景图像的不透明度?

[英]How can I change the background image opacity without affecting the background color?

I've got a div with a background color and a transparent background image. 我有一个背景颜色和透明背景图像的div。

HTML HTML

<div class="watermark">
    <div class="col-md-12">Something else</div>
    <div class="col-md-12">Something more..</div>
    <div class="col-md-12">Something at the end</div>
</div>

CSS CSS

body{
  background-color:white;
}

.watermark {
  display: block;
  position: relative;
}

.watermark::after {
  content: "";
 background:#C52F11 url(https://www.google.co.in/images/srpr/logo11w.png)no-repeat;
  opacity: 0.2;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}

jsfiddle 的jsfiddle

I want to be change the opacity of the image, but leave the background color unaffected. 我想改变图像的不透明度,但不要影响背景颜色。 But when I set the opacity, both change. 但是当我设置不透明度时,两者都会改变。

Is there a way to do this? 有没有办法做到这一点?

Use an rgba color value and remove the opacity . 使用rgba颜色值并删除opacity For a white overlay you may use background:rgba(255,255,255, 0.5); 对于白色叠加,您可以使用background:rgba(255,255,255, 0.5); while the last value (in this case 0.5) defines your transparency. 而最后一个值(在本例中为0.5)定义了您的透明度。

You can check this fiddle . 你可以检查这个小提琴

You can add a ::before pseudo-element to handle the background color, so that the ::after element has the image and opacity change, and the background-color can be unaffected. 您可以添加::before伪元素来处理背景颜色,以便::after元素具有图像和不透明度更改,并且background-color可以不受影响。 Note that the background-color of the actual .watermark element needs to be transparent, as the z-index:-1 will push the pseudo-elements behind the actual one. 请注意,实际.watermark元素的background-color需要是透明的,因为z-index:-1会将伪元素推送到实际元素后面。

 .watermark { width: 300px; height: 100px; display: block; position: relative; } .watermark::before, .watermark::after { content: ""; top: 0; left: 0; bottom: 0; right: 0; position: absolute; z-index: -1; } .watermark::before { background:#C52F11; } .watermark::after { background: url(https://www.google.com/images/srpr/logo11w.png) no-repeat; opacity: 0.2; } 
 <div class="watermark"> <div class="col-md-12">Something else</div> <div class="col-md-12">Something more..</div> <div class="col-md-12">Something at the end</div> </div> 

Updated fiddle 更新了小提琴

CSS for body , whatever you want 身体的CSS,无论你想要什么

body{
    background-color:white;
}

main div(.watermark) with background color, width and height of your choice 主div(.watermark),背景颜色,宽度和高度可供选择

.watermark {
    width: 538px;
    height: 190px;
    display: block;
    position: relative;
    background: #C52F11;
}

watermark after CSS , image with opacity CSS后的水印,不透明的图像

.watermark::after {
    content: "";
    background: url('https://www.google.co.in/images/srpr/logo11w.png') no-repeat;
    opacity: 0.4;
    top: 0;
    left: 0;
    position: absolute;
    z-index: 1;
    width: 538px;
    height: 190px;
}

I would recommend to use two divs. 我建议使用两个div。 Its always a good idea to have two divs in overlapping stuffs with relative and absolute. 在相对和绝对重叠的东西中有两个div总是一个好主意。 Moreover, it adds long life to your code structure before you have to change it otherwise. 此外,它还会为您的代码结构增加使用寿命,否则必须进行更改。

It's a trick: 这是一个诀窍:

  1. Insert the image into the webpage anywhere(regardless of the size). 将图像插入网页的任何位置(无论大小)。

  2. Set the desired opacity by style="opacity:0.7;" 通过style="opacity:0.7;"设置所需的不style="opacity:0.7;" (for opacity= 0.7). (对于不透明度= 0.7)。

  3. Take a snapshot of that image. 拍摄该图像的快照。

  4. Remove that image and insert the snapped image where ever you want. 删除该图像并将捕捉的图像插入任何您想要的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM