背景颜色覆盖背景图像

背景颜色覆盖背景图像

本文介绍了用 rgba 背景颜色覆盖背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a div with a background-image. I want to overlay the background-image with an rgba color (rgba(0,0,0,0.1)) when the user hovers the div.

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

I was wondering if there's a one-div solution (i.e. not with multiple divs, one for the image and one for the color, etc.).

我尝试了多种方法:

<div class="the-div" id="test-1"></div>
<div class="the-div" id="test-2"></div>
<div class="the-div" id="test-3"></div>

还有这个 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);
}

参见这个小提琴.

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

The only option I saw is to make another image, with overlay, preload it using JavaScript and then use .the-div:hover { background: url('the-new-image'); }. However, I'd like a CSS-only solution (neater; less HTTP requests; less harddisk). Is there any?

推荐答案

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

The solution by PeterVR has the disadvantage that the additional color displays on top of the entire HTML block - meaning that it also shows up on top of div content, not just on top of the background image. This is fine if your div is empty, but if it is not using a linear gradient might be a better solution:

<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>

参见 fiddle.太糟糕了,梯度规范目前一团糟.请参阅 兼容性表,上面的代码应该可以工作在任何具有显着市场份额的浏览器中 - MSIE 9.0 和更早版本除外.

See fiddle. Too bad that gradient specifications are currently a mess. See compatibility table, the code above should work in any browser with a noteworthy market share - with the exception of MSIE 9.0 and older.

编辑(2017 年 3 月):网络状态现在变得不那么混乱了.所以 linear-gradient(Firefox 和 Internet Explorer 支持)和 -webkit-linear-gradient(Chrome、Opera 和 Safari 支持)行就足够了,额外的前缀版本不再需要.

Edit (March 2017): The state of the web got far less messy by now. So the linear-gradient (supported by Firefox and Internet Explorer) and -webkit-linear-gradient (supported by Chrome, Opera and Safari) lines are sufficient, additional prefixed versions are no longer necessary.

这篇关于用 rgba 背景颜色覆盖背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:21