css - 如何使用CSS制作图片叠加效果?

标签 css

我使用以下代码设计了一些带有 CSS 的超链接以添加背景图像(使其看起来像一个按钮):
<a class="btnImg" id="btnImgConfig" href="#"></a>

.btnImg {
    width:100px;
    height:100px;
    display:inline-block;
    border:1px solid #e4e4e4;
}

.btnImg:hover {
    opacity: .2;
    background-color: #878787;
}

#btnImgConfig {
    background: url("http://www.icecub.nl/images/config.png") no-repeat scroll 0 0 transparent;
}

如您所见,我试图在鼠标悬停时为图像创建更暗的效果。这是想要的效果:

Desired hover effect

但是目前效果是这样的:

Current hover effect

我知道我可以通过用较暗的版本替换悬停时的图像来轻松地做到这一点。但不知何故,我觉得在这种情况下不应该这样做。除了上面提到的,我也试过rgba{..}悬停。然而,这根本没有效果。

这是一个 JSFiddle上面的代码。

最佳答案

您也可以使用伪元素,然后覆盖。这将为您提供所需的效果。

.btnImg {
  width: 100px;
  height: 100px;
  display: inline-block;
  border: 1px solid #e4e4e4;
  position: relative;
}
.btnImg:hover::after {
  background-color: #878787;
  opacity: 0.4;
  position: absolute;
  content: '';
  width: 100%;
  height: 100%;
}
#btnImgConfig {
  background: url("http://www.icecub.nl/images/config.png") no-repeat scroll 0 0 transparent;
}
<a class="btnImg" id="btnImgConfig" href="#"></a>

关于css - 如何使用CSS制作图片叠加效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31651877/

相关文章:

ios - 在 Mobile Safari 上缩放 iframe 会破坏 iframe 的链接

移动 : Blog page appearing too narrow 的 CSS 问题

javascript - 我如何创建一个带有滚动的 html 表格

javascript - WordPress 网站 : homepage redirects to "login" page (non-existent now)

html - 如何将表格样式设置为容器的全宽并使单元格使用宽度的百分比?

javascript - 在 React 中悬停时叠加,JS 方式

高度为 100% 的 html、body 和 div

android - 在移动设备上覆盖默认下拉(选择)样式

html - 如何使用 twitter bootstrap 和 rails 简单表单创建水平表单

css - Bootstrap - 调整浏览器大小时背景确实扩展了 100% 的宽度