我想在悬停的图像上创建一个带有居中文字的黑色透明叠加层。排版Instagram如何通过每张图片here(悬停在图片上方)听到
我尝试过这样的事情:
.parent {
position: relative;
background: black;
}
.child {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
visibility: hidden;
}
.child p {
text-align: center;
color: white;
font-size: 1.2em;
}
.child:hover {
visibility: visible;
opacity: 4;
}

<div class="parent">
<img src="someimageurl">
<div class="child">
<p>some text</p>
</div>
</div>
&#13;
这已经失败了。请注意,该解决方案还应与视频标签配合使用。
答案 0 :(得分:3)
尝试以下方法:
display: flex;
justify-content: center;
align-items: center;
和position: absolute;
分配给您的模式。background-color: rgba(0, 0, 0, 0);
隐藏叠加层,并在悬停时使用background-color: rgba(0, 0, 0, 0.5);
显示该叠加层。 (使用rgba()
不会为您的文字指定不透明度,这就是您应该将其用于不透明度的原因)要隐藏文字,您可以使用visibility: hidden;
和visibility: visible;
来显示文字。< / LI>
醇>
.container {
width: 300px;
height: 300px;
position: relative;
font-family: Helvetica,sans-serif;
}
img {
background-color: red;
width: 300px;
height: 300px;
}
.overlay {
display: flex;
justify-content: center;
align-items: center;
color: red;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
top: 0;
background-color: rgba(0, 0, 0, 0);
visibility: hidden;
}
.container:hover > .overlay {
background-color: rgba(0, 0, 0, 0.5);
visibility: visible;
}
&#13;
<div class="container">
<img src="http://i.stack.imgur.com/Zw1MI.jpg"/>
<div class="overlay">Darth Maul</div>
</div>
&#13;
答案 1 :(得分:1)
您可以考虑的淡入和下滑变体。运行下面的代码段。
.child p {
width: 100%;
text-align: center;
font-size: 1.2em;
margin: auto;
top: 45%;
position: absolute;
}
.child {
position: absolute;
right: 0;
left: 0;
visibility: hidden;
color: transparent;
top: 0;
bottom: 0;
max-width: 100%;
max-height: 300px;
display: block;
vertical-align: middle;
transition: .7s;
}
.parent {
position: relative;
width: auto;
display: block;
max-width: 300px;
overflow: hidden;
}
.parent:hover .child {
visibility: visible;
opacity: .4;
background: black;
color: white;
}
.parent:hover .child.slide-down {
top: 0;
}
.child.slide-down {
top: -500px;
}
<div class="parent">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
<div class="child">
<p>some text</p>
</div>
</div>
<div class="parent">
<img src="https://placeholdit.imgix.net/~text?txtsize=28&txt=300%C3%97300&w=300&h=300">
<div class="child slide-down">
<p>some text</p>
</div>
</div>
答案 2 :(得分:0)
我认为这样做的一个简单方法就是让图像“覆盖”已存在,但隐藏到鼠标悬停:
#parentdiv { background-image:url('myimage.jpg'); }
#parentdiv div { width:100%; height: 100%; opacity:0.2; background: #000; text-align:center; display: none; }
使用HTML
<div id="parentdiv">
<div><!-- text icons in here --></div>
</div>
和javascript:
document.getElementById("parentdiv").addEventListener("mouseover", function () {
this.children[0].style.display = 'block';
});
并且为了躲避:
document.getElementById("parentdiv").addEventListener("mouseout", function () {
this.children[0].style.display = 'none';
});