仅将CSS悬停在图像上的黑色透明覆盖层?


101

每当鼠标仅用CSS悬停在图像上时,我都试图在图像上添加透明的黑色覆盖层。这可能吗?我尝试了这个:

http://jsfiddle.net/Zf5am/565/

但是我无法让div出现。

<div class="image">
    <img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
    <div class="overlay" />
</div> 

.image {
  position: relative;
  border: 1px solid black;
  width: 200px;
  height: 200px;
}
.image img {
  max-width: 100%;
  max-height: 100%;
}
.overlay {
  position: absolute;
  top: 0;
  left: 0;
  display: none;
  background-color: red;
  z-index: 200;
}
.overlay:hover {
  display: block;
}

您的意思是将:hover放在图片div上,而不是叠加div上?
andi

1
请注意:为什么要在覆盖层上放置如此高的Z索引?Z索引不是全局索引;无需“砍”高或低的价值来使它们起作用。
2013年

1
这终于帮助我解决了这个问题:jsfiddle.net/4Dfpm不需要覆盖div。图像在悬停时变得不透明,下面的div应该具有黑色背景,瞧。
凯诺克

Answers:


211

我建议使用伪元素代替overlay元素。由于伪元素不能添加到封闭的img元素上,因此您仍然需要包装该img元素。

现场示例 -带有文本的示例

<div class="image">
    <img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

至于CSS,请在元素上设置可选尺寸.image,并相对放置它。如果您希望获得自适应图像,则只需省略尺寸即可,但仍然可以使用(示例)。值得注意的是,尺寸必须位于父元素上,而不是img元素本身,请参见

.image {
    position: relative;
    width: 400px;
    height: 400px;
}

给子img元素赋予100%父元素的宽度,然后添加vertical-align:top以解决默认的基线对齐问题。

.image img {
    width: 100%;
    vertical-align: top;
}

对于伪元素,请设置内容值并将其相对于.image元素绝对定位。的宽度/高度100%将确保它适用于变化的img尺寸。如果要过渡元素,请设置其不透明度0并添加过渡属性/值。

.image:after {
    content: '\A';
    position: absolute;
    width: 100%; height:100%;
    top:0; left:0;
    background:rgba(0,0,0,0.6);
    opacity: 0;
    transition: all 1s;
    -webkit-transition: all 1s;
}

将鼠标1悬停在伪元素上时,请使用不透明度,以方便过渡:

.image:hover:after {
    opacity: 1;
}

最终结果在这里


如果要在悬停时添加文本,请执行以下操作:

对于最简单的方法,只需将文本添加为​​伪元素的content值即可:

这里的例子

.image:after {
    content: 'Here is some text..';
    color: #fff;

    /* Other styling.. */
}

这在大多数情况下应该有效;但是,如果您有多个img元素,则可能不希望在悬停时显示相同的文本。因此,您可以在data-*属性中设置文本,因此每个img元素都有唯一的文本。

这里的例子

.image:after {
    content: attr(data-content);
    color: #fff;
}

随着content中值attr(data-content),伪元素添加从文本.image元素的data-content属性:

<div data-content="Text added on hover" class="image">
    <img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

您可以添加一些样式并执行以下操作:

这里的例子

在上面的示例中,:after伪元素用作黑色覆盖,而:before伪元素是标题/文本。由于元素彼此独立,因此可以使用单独的样式来获得最佳的定位。

.image:after, .image:before {
    position: absolute;
    opacity: 0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.image:after {
    content: '\A';
    width: 100%; height:100%;
    top: 0; left:0;
    background:rgba(0,0,0,0.6);
}
.image:before {
    content: attr(data-content);
    width: 100%;
    color: #fff;
    z-index: 1;
    bottom: 0;
    padding: 4px 10px;
    text-align: center;
    background: #f00;
    box-sizing: border-box;
    -moz-box-sizing:border-box;
}
.image:hover:after, .image:hover:before {
    opacity: 1;
}

1
谢谢乔希。我最喜欢这个版本。如果您想在该透明黑色之上显示一些白色文本,该怎么办?您是否要添加另一个div并分别对其应用不透明度?
RobVious

是的,实际上只有悬停了。
RobVious

1
谢谢,这是一个比我以前尝试过的解决方案更好的解决方案
wingskush

如何将标题设置为图像的中心? top:30%可以,请参阅我的小提琴,但是对于响应式布局,将其设置为准确的中心会很好。顺便说一句:感谢您的回答。
p2或2015年

3
@poor这是一种响应式方法(更新的示例) ..它使用top: 50%/ transform: translateY(-50%)进行垂直居中。正是在这种对方的回答中提到的方法之一,如果雷- stackoverflow.com/questions/5703552/...
乔希克罗泽

44

11

你近了 这将起作用:

.image { position: relative; border: 1px solid black; width: 200px; height: 200px; }
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; display: none; background-color: rgba(0,0,0,0.5); }
.image:hover .overlay { display: block; }

您需要放上:hover图像,并.overlay通过添加right:0;和覆盖整个图像bottom:0

jsfiddle:http : //jsfiddle.net/Zf5am/569/


我还建议使用CSS3动画为兼容的浏览器进行更平滑的过渡(IE用户将不得不受苦)。
2013年

7

这是在图像div上使用:after而不是额外的覆盖div的好方法:http : //jsfiddle.net/Zf5am/576/

<div class="image">
    <img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
</div>

.image {position:relative; border:1px solid black; width:200px; height:200px;}
.image img {max-width:100%; max-height:100%;}
.image:hover:after {content:""; position:absolute; top:0; left:0; bottom:0; right:0; background-color: rgba(0,0,0,0.3);}

2

.overlay没有高度或宽度,没有内容,您也不能将鼠标悬停在display:none

我改为给div相同的大小和位置,.imageRGBA在悬停时更改值。

http://jsfiddle.net/Zf5am/566/

.image { position: absolute; border: 1px solid black; width: 200px; height: 200px; z-index:1;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; background:rgba(255,0,0,0); z-index: 200; width:200px; height:200px; }
.overlay:hover { background:rgba(255,0,0,.7); }

1

您可以通过处理图像的不透明度并将图像的背景色设置为黑色来实现此目的。通过使图像透明,它将显得更暗。

<div class="image">
    <img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
</div> 

CSS:

.image { position: relative; border: 1px solid black; width: 200px; height: 200px; background: black; }
.image img { max-width: 100%; max-height: 100%; }
.image img:hover { opacity: .5 }

您可能还需要设置特定于浏览器的不透明度,以使其在其他浏览器中也起作用。


0

我会给您的图像尺寸的覆盖层div最小高度和最小宽度,并在悬停时更改背景色

.overlay { position: absolute; top: 0; left: 0;  z-index: 200; min-height:200px; min-width:200px; background-color: none;}
.overlay:hover { background-color: red;}

0

在这里查看我的操作:http : //jsfiddle.net/dyarbrough93/c8wEC/

首先,您永远不会设置叠加层的尺寸,这意味着它并不是一开始就显示出来的。其次,当您将鼠标悬停在图像上时,建议仅更改覆盖图的z索引。更改覆盖层的不透明度/颜色以适合您的需求。

.image { position: relative; width: 200px; height: 200px;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; background-color: gray; z-index: -10; width: 200px; height: 200px; opacity: 0.5}
.image:hover .overlay { z-index: 10}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.