html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自己试着做做</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<div class="image-container">
<img src="1.jpg" alt="图片加载失败">
<div class="overlay">这是小猫</div>
</div>
</body>
</html>
css
.image-container{
position: relative;
width: 300px;
height: 300px;
margin: auto;
}
.image-container img{
width: 100%;
height: 100%;
}
.overlay{
position: absolute;
top: 50%;
left: 50%;
color: black;
/* 字体居中 */
transform: translate(-50%,-50%);
font-size: large;
/* 悬停前完全透明 */
opacity: 0;
transition: 0.3s ease-in-out;
}
.image-container:hover .overlay{
transition: 0.3s ease-in-out;
/* 悬停时完全不透明 */
opacity: 1;
}
效果展示
未悬停:
悬停时: