如何在CSS中对图像进行叠加?

use*_*621 54 html css image hover opacity

我想要实现这样的事情:

这个效果

当我将鼠标悬停在图像上时,我想在图像上添加一些带有文字和图标的深色图像.

我被困在这里.我找到了一些教程,但他们没有解决这个问题.另外,另一个问题 - 每个图像都有不同的高度.宽度始终相同.

如何实现这种效果?

dfs*_*fsq 101

你可以用这个简单的CSS/HTML实现这个目的:

.image-container {
    position: relative;
    width: 200px;
    height: 300px;
}
.image-container .after {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    color: #FFF;
}
.image-container:hover .after {
    display: block;
    background: rgba(0, 0, 0, .6);
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="image-container">
    <img src="http://lorempixel.com/300/200" />
    <div class="after">This is some content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/6Mt3Q/


UPD:这是一个不错的最终演示,带有一些额外的风格.

.image-container {
    position: relative;
    display: inline-block;
}
.image-container img {display: block;}
.image-container .after {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    color: #FFF;
}
.image-container:hover .after {
    display: block;
    background: rgba(0, 0, 0, .6);
}
.image-container .after .content {
    position: absolute;
    bottom: 0;
    font-family: Arial;
    text-align: center;
    width: 100%;
    box-sizing: border-box;
    padding: 5px;
}
.image-container .after .zoom {
    color: #DDD;
    font-size: 48px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -30px 0 0 -19px;
    height: 50px;
    width: 45px;
    cursor: pointer;
}
.image-container .after .zoom:hover {
    color: #FFF;
}
Run Code Online (Sandbox Code Playgroud)
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet"/>

<div class="image-container">
    <img src="http://lorempixel.com/300/180" />
    <div class="after">
        <span class="content">This is some content. It can be long and span several lines.</span>
        <span class="zoom">
            <i class="fa fa-search"></i>
        </span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 只需将`display:inline-block;`设置为容器即可.http://jsfiddle.net/6Mt3Q/1/ (2认同)

jbu*_*483 30

您可以为此使用伪元素,并将您的图像放在悬停上:

.image {
  position: relative;
  height: 300px;
  width: 300px;
  background: url(http://lorempixel.com/300/300);
}
.image:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  transition: all 0.8s;
  opacity: 0;
  background: url(http://lorempixel.com/300/200);
  background-size: 100% 100%;
}
.image:hover:before {
  opacity: 0.8;
}
Run Code Online (Sandbox Code Playgroud)
<div class="image"></div>
Run Code Online (Sandbox Code Playgroud)


And*_*ole 18

把这个答案放在这里,因为它是谷歌的最高结果。

如果你想要一个快速简单的方法:

    filter: brightness(0.2);
Run Code Online (Sandbox Code Playgroud)

*不兼容IE

  • 该帖子已有 6 年历史,接受的答案有 100 多个投票,其中一些是在过去几个月内投票的。你的答案没有任何问题,但也许你可以进一步解释为什么你的答案可能对 2020 年阅读这篇文章的人有用。这是 css 的新功能吗? (3认同)

Emi*_*nov 12

这有点晚了,但是当搜索覆盖方法时,这个帖子在Google中作为最高结果出现.

你可以简单地使用一个 background-blend-mode

.foo {
    background-image: url(images/image1.png), url(images/image2.png);
    background-color: violet;
    background-blend-mode: screen multiply;
}
Run Code Online (Sandbox Code Playgroud)

这样做是它采用第二个图像,并使用多重混合模式将其与背景颜色混合,然后使用屏幕混合模式将第一个图像与第二个图像和背景颜色混合.您可以使用16种不同的混合模式来实现任何叠加.

乘法,屏幕,叠加,变暗,变亮,颜色闪避,颜色燃烧,强光,柔光,差异,排斥,色调,饱和度,颜色和亮度.

  • 这是一个好主意.然而,主要问题是它需要两个图像都是背景图像.如果主图像是HTML"<img rel="nofollow noreferrer" />",则它不起作用.此外,当时"背景混合模式"不受支持:在Safari中部分支持Chrome和FF的后期版本(http://caniuse.com/#search=background-blend-mode) (3认同)