为什么当鼠标悬停在图像上时,文本不显示?如何在css中使用visibility属性获得文本的可见性
null
img{
margin-top: 110px;
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
margin-top: 50px;
position: relative;
}
.txt{
font-size: 16px;
line-height: 20px;
margin-top: 10px;
text-align: center;
margin-left: 440px;
margin-right: 440px;
visibility: hidden;
}
img:hover .txt{
cursor: pointer;
visibility: visible;
}
<div class="container">
<img src="empty-boxes.svg" style="width:520px;height:190px;">
<p class="number">404</p>
<p class="txt">When communicating via HTTP, the server is required
.</p>
</div>
null
添加CSS“~
”选择器。MDN
null
img {
margin-top: 110px;
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
margin-top: 50px;
position: relative;
}
.txt {
font-size: 16px;
line-height: 20px;
margin-top: 10px;
text-align: center;
margin-left: 440px;
margin-right: 440px;
visibility: hidden;
}
img:hover ~ .txt {
cursor: pointer;
visibility: visible;
}
<div class="container">
<img src="empty-boxes.svg" style="width:520px;height:190px;">
<p class="number">404</p>
<p class="txt">When communicating via HTTP, the server is required .
</p>
</div>