如何在CSS中覆盖图像

我试图实现这样的事情:

我试图实现这样的事情:

this effect

当我将鼠标悬停在图像上时,我想用一些文本和图标将该图像置于这种深色。

我被困在这里。我找到了一些教程,但他们没有为这种情况工作。另外,另一个问题-每个图像都有不同的高度。宽度总是相同的。

如何达到这种效果?

138

你可以用这个简单的 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);
}

HTML格式

<div cl="image-container">
    <img src="http://lorempixel.com/300/200" />
    <div cl="after">This is some content</div>
</div>
Demo: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;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet"/>
<div cl="image-container">
    <img src="http://lorempixel.com/300/180" />
    <div cl="after">
        <span cl="content">This is some content. It can be long and span several lines.</span>
        <span cl="zoom">
            <i cl="fa fa-search"></i>
        </span>
    </div>
</div>
40

你可以使用一个伪元素,并在悬停你的形象:

.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;
}
<div cl="image"></div>
38

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

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

    filter: brightness(0.2);

* 与 IE 不兼容

20

有点晚了,但这个线程出现在谷歌作为搜索覆盖方法时的最高结果。

您可以简单地使用background-blend-mode

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

这是什么,它需要第二个图像,并通过使用多重混合模式将其与背景颜色混合,然后通过使用屏幕混合模式将第一个图像与第二个图像和背景颜色混合。

乘法,屏幕,叠加,变暗,变亮,颜色闪避,颜色烧伤,硬光,柔光,差异,排除,色调,饱和度,颜色和亮度。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(577)
'时间倒退' xkcd
上一篇
@ react-native-voice/voice不适用于最新的博览会
下一篇

相关推荐

  • css设置滚动条宽度设置:This is a title

    CSS设置滚动条宽度的方法:使用CSS3的。-webkit-属性:…

    2023-06-06 10:53:01
    0 42 57
  • css 子样式:标题

    CSS 子样式是指在父样式的基础上,对其子元素进行单独定义的样式。它可以使用类选择器、ID 选择器或者属性选择器来定义,如下所示:…

    2023-04-13 14:42:51
    0 82 64
  • css 间隔:如何利用 CSS 进行间隔调整

    示例示例CSS 间隔是指在 CSS 中使用的间距属性,它可以用来设置元素之间的距离。有几种常用的 CSS 间隔属性,包括:…

    2023-06-05 02:47:09
    0 46 67
  • css导入字体:标题

    示例示例CSS导入字体可以通过@font-face规则来实现,代码示例如下:…

    2023-04-22 12:42:42
    0 99 42
  • css背景图片:Unlock the Power of Creative Backgrounds

    示例示例CSS背景图片是一种使用CSS样式来定义HTML元素的背景图像。它可以让你在网页中添加图片,而不需要使用标签。CSS背景图片的语法如下:…

    2023-04-27 07:58:21
    0 59 75
  • sci和cssci哪个高级一种新的科学出版标准

    SCI和CSSCI是两个学术期刊收录索引,它们都用于衡量学术期刊的发表质量。SCI(Science Index)是由美国科学信息研究所(ISI)提供的一种学术期刊收录索引,它收录了来自全球各地的学术期刊,并以引文索引的方式收录其中的文章。SCI的收录索引覆盖了超过8000种期刊,其中包括了许多国际一流期刊,如Nature、Science、Cell、PNAS等。…

    2023-05-14 11:16:30
    0 48 82
  • css文本垂直居中:标题

    CSS文本垂直居中的方法有很多,以下是其中常用的几种:使用 line-height 属性:…

    2023-05-09 10:53:27
    0 10 14
  • css选择第三个元素:Third Element

    CSS选择第三个元素的代码如下:上述代码中,element表示要选择的元素,nth-child()是CSS中的伪类,用于选择某个父元素的第n个子元素,圆括号中的数字表示第几个,上述代码表示选择第三个元素。…

    2023-04-12 07:21:21
    0 55 91

发表评论

登录 后才能评论

评论列表(57条)