如何使用 CSS 创建悬停时显示的图片叠加标题?

cssweb developmentfront end technology更新于 2025/9/28 5:32:17

以下是使用 CSS 创建悬停时显示的图片叠加标题的代码 −

示例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}
.card-container {
   position: relative;
   width: 50%;
}
.image {
   display: block;
   width: 100%;
   height: auto;
}
.overlay {
   position: absolute;
   bottom: 0;
   background: rgba(48, 9, 155, 0.5);
   width: 78%;
   transition: .5s ease;
   opacity:0;
   color: rgb(251, 255, 0);
   font-size: 25px;
   font-weight: bolder;
   padding: 20px;
   text-align: center;
}
.card-container:hover .overlay {
   opacity: 1;
}
</style>
</head>
<body>
<h2>Image Overlay Title Example</h2>
<div class="card-container">
<img src="https://i.picsum.photos/id/237/536/354.jpg" >
<div class="overlay">Dog</div>
</div>
</body>
</html>

输出

上述代码将产生以下输出 −

当鼠标悬停在图片上时,标题将显示为底部 −


相关文章


有用资源