简体   繁体   English

如何在使用CSS悬停时创建此透明图像叠加?

[英]How do I create this transparent image overlay on hover with CSS?

I'm looking for advice to reproduce ( see this image ) effect for my image hovers. 我正在寻找建议,为我的图像悬停重现( 见图 )效果。 My problem is that my images are fluid, and I haven't really been able to find any good tutorials on that subject combined with overlays. 我的问题是我的图像很流畅,而且我还没有真正找到关于该主题的任何好的教程以及叠加层。

I'm assuming I have to create a transparent png (white area + circle) which overlays the image on hover, and then the text overlaying that? 我假设我必须创建一个透明的png(白色区域+圆圈),它在悬停时覆盖图像,然后文本覆盖了那个? And it all needs to resize accordingly with the image itself. 所有这些都需要根据图像本身进行相应的调整。 Also, the top border is not part of the image, it's generated with CSS, and I don't want that to be overlayed if possible. 此外,顶部边框不是图像的一部分,它是用CSS生成的,我不希望在可能的情况下覆盖它。

Could anyone kindly point me in the right direction, or give advice if there's a better implementation? 任何人都可以指出我正确的方向,或者提供更好的实施建议? I'm rather lost. 我很失落。

Thank you in advance. 先感谢您。 :) :)

If the image is going to be contained in a div with a defined width, you can add an absolutely positioned div to that containing div that'll act as the overlay. 如果图像将包含在具有已定义宽度的div中,则可以将包含div的绝对定位div添加到将用作叠加的div中。 Assuming this snippet and that the opacity of the overlay is set to zero 假设此片段并且叠加层的不透明度设置为零

<div class="picholder">
    <img class="fancypics" src=http://placehold.it/500x650></img>
    <div class="overlay"><p class="text_box">Hello World!</p></div>
</div>

the css for the hover effect would be 悬停效果的CSS将是

.picholder:hover .overlay{opacity:1;}
.picholder:hover .fancypics{opacity:0.7;}

That should create the hover effect, I believe you're going for. 这应该会产生悬停效果,我相信你会这样做。 The following css should center the overlay and some other stuff. 以下css应该将叠加和其他一些东西居中。 see here for more on centering divs vertically and horizontally 有关垂直和水平居中div的更多信息,请参阅此处

.overlay {
  bottom: 0;left: 0; top: 0; right: 0;
  height: 150px;
  width: 150px;
  margin: auto;
  position: absolute;
  background-color:#3f3f3f;
  border-radius: 50%;
  opacity:0;
}
.fancypics{width:100%;}
.text_box{
  color:white;
  weight:bold;
  font-size:2em;
  padding:10px;
  padding-bottom:50%;
  text-align:center;
}

and of course the fiddle 当然还有小提琴

Just use background-color to set a transparent color: 只需使用背景色设置透明色:

Demo here 在这里演示

在此输入图像描述

HTML HTML

<div class="overlay">
    <div>Hello</div>
    <span>January 16. 2014</span>
</div>

CSS CSS

.overlay {
    position:relative;
    width:200px;
    height:200px;
    border-radius:50%;
}
.overlay:hover {
    background:rgba(0,0,0,0.5);
}
.overlay > div {
    position:absolute;
    color:#fff;
    font:50px sans-serif;
    width:100%;
    top:33%;
    text-align:center;
}
.overlay > span {
    position:absolute;
    color:#fff;
    font:12px sans-serif;
    width:100%;
    top:67%;
    text-align:center;
}

The stippled line at the border of the upper text can be achieved using either a border-bottom or a single-line image which you attach as background to the div. 可以使用边框底部或单线图像来实现上部文本边框处的点画线,并将其作为div的背景附加。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM