用rgba背景颜色叠加背景图像

50 css background background-image css3 rgba

我有div一个background-image.rgba(0,0,0,0.1)当用户悬停div时,我想用rgba颜色()覆盖背景图像.

我想知道是否有一个div解决方案(即没有多个div,一个用于图像,一个用于颜色等).

我尝试过多种方法:

<div class="the-div" id="test-1"></div>
<div class="the-div" id="test-2"></div>
<div class="the-div" id="test-3"></div>
Run Code Online (Sandbox Code Playgroud)

这个CSS:

.the-div {
    background-image: url('the-image');
    margin: 10px;
    width: 200px;
    height: 80px;
}

#test-1:hover {
    background-color: rgba(0,0,0,0.1);
}

#test-2:hover {
    background: url('the-image'), rgba(0,0,0,0.1);
}

#test-3:hover {
    background: rgba(0,0,0,0.1);
}
Run Code Online (Sandbox Code Playgroud)

看到这个小提琴.

我看到的唯一选择是制作另一个图像,使用叠加,使用JavaScript预加载然后使用.the-div:hover { background: url('the-new-image'); }.但是,我想要一个仅限CSS的解决方案(更整洁;更少的HTTP请求;更少的硬盘).有没有?

Wla*_*ant 48

PeterVR的解决方案的缺点是,整个HTML块顶部会显示额外的颜色 - 这意味着它也会显示在div内容之上,而不仅仅是在背景图像的顶部.如果div为空,这很好,但如果它不使用线性渐变,则可能是更好的解决方案:

<div class="the-div">Red text</div>

<style type="text/css">
  .the-div
  {
    background-image: url("the-image.png");
    color: #f00;
    margin: 10px;
    width: 200px;
    height: 80px;
  }
  .the-div:hover
  {
    background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url("the-image.png");
    background-image: -moz-linear-gradient(top, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url("the-image.png");
    background-image: -o-linear-gradient(top, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url("the-image.png");
    background-image: -ms-linear-gradient(top, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url("the-image.png");
    background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0.1)), to(rgba(0, 0, 0, 0.1))), url("the-image.png");
    background-image: -webkit-linear-gradient(top, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.1)), url("the-image.png");
  }
</style>
Run Code Online (Sandbox Code Playgroud)

小提琴.太糟糕了,梯度规格目前是一团糟.请参阅兼容性表,上述代码应适用于任何具有值得注意的市场份额的浏览器 - MSIE 9.0及更早版本除外.

编辑(2017年3月):到目前为止,网络的状态变得更加混乱.因此linear-gradient(由Firefox和Internet Explorer -webkit-linear-gradient支持)和(由Chrome,Opera和Safari支持)行就足够了,不再需要额外的前缀版本.


Pev*_*ara 36

是的,有办法做到这一点.您可以使用伪元素after将块定位在背景图像的顶部.像这样:http: //jsfiddle.net/Pevara/N2U6B/

:after看起来像这样的CSS :

#the-div:hover:after {
    content: ' ';
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0,0,0,.5);
}
Run Code Online (Sandbox Code Playgroud)

编辑:
当你想将它应用于非空元素,并且只是在背景上得到叠加时,你可以通过z-index对元素应用正数而对元素应用负数来实现:after.像这样的东西:

#the-div {
    ...
    z-index: 1;
}
#the-div:hover:after {
    ...
    z-index: -1;
}
Run Code Online (Sandbox Code Playgroud)

更新的小提琴:http://jsfiddle.net/N2U6B/255/

  • 这是一个很好的技巧但不幸的是,这一层也覆盖了文本,而不仅仅是背景图像.所以这只适用于空div. (5认同)

小智 6

/* Working method */
.tinted-image {
  background: 
    /* top, transparent red, faked with gradient */ 
    linear-gradient(
      rgba(255, 0, 0, 0.45), 
      rgba(255, 0, 0, 0.45)
    ),
    /* bottom, image */
    url(https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg);
    height: 1280px;
    width: 960px;
    background-size: cover;
}

.tinted-image p {
    color: #fff;
    padding: 100px;
  }
Run Code Online (Sandbox Code Playgroud)
<div class="tinted-image">
  
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam distinctio, temporibus tempora a eveniet quas  qui veritatis sunt perferendis harum!</p>
  
</div>
Run Code Online (Sandbox Code Playgroud)

来源:https://css-tricks.com/tinted-images-multiple-backgrounds/