如何创建:图像叠加标题

学习如何在鼠标悬停时创建图像叠加标题。

图像叠加标题

请将鼠标悬停在图像上,即可查看叠加效果。

Avatar
My Name is Bill

亲自试一试

如何创建叠加图像标题

第一步 - 添加 HTML:

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">My Name is John</div>
</div>

第二步 - 添加 CSS:

* {box-sizing: border-box}

/* 需要放置覆盖层的容器。根据需要调整宽度 */
.container {
  position: relative;
  width: 50%;
  max-width: 300px;
}

/* 使图像可响应 */
.image {
  display: block;
  width: 100%;
  height: auto;
}

/* 叠加效果 - 位于容器顶部和图像上方 */
.overlay {
  position: absolute;
  bottom: 0;
  background: rgb(0, 0, 0);
  background: rgba(0, 0, 0, 0.5); /* Black see-through */
  color: #f1f1f1;
  width: 100%;
  transition: .5s ease;
  opacity:0;
  color: white;
  font-size: 20px;
  padding: 20px;
  text-align: center;
}

/* 当您将鼠标悬停在容器上时,淡入叠加标题 */
.container:hover .overlay {
  opacity: 1;
}

亲自试一试

相关页面

教程:CSS 图像

教程:如何创建图像叠加悬停效果