如何创建:翻转卡片

学习如何使用 CSS 创建翻转卡片。

请将鼠标移至下图:

Avatar
Bill Gates

Architect & Engineer

We love that guy

亲自试一试

如何创建翻转卡片

第一步 - 添加 HTML:

<div class="flip-card">
  <div class="flip-card-inner">
    <div class="flip-card-front">
      <img src="img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
    </div>
    <div class="flip-card-back">
      <h1>Bill Gates</h1>
      <p>Architect & Engineer</p>
      <p>We love that guy</p>
    </div>
  </div>
</div>

第二步 - 添加 CSS:

/* 翻转卡片的容器 - 设置你想要的宽度和高度。我们添加了 border 属性来演示翻转本身在悬停时会超出容器(如果您不想要 3D 效果,请删除透视图) */
.flip-card {
  background-color: transparent;
  width: 300px;
  height: 200px;
  border: 1px solid #f1f1f1;
  perspective: 1000px; /* 如果您不想要 3D 效果,请将其删除 */
}

/* 这个容器用于定位正面和背面 */
.flip-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}

/* 当鼠标悬停在翻转卡片容器上时,进行水平翻转 */
.flip-card:hover .flip-card-inner {
  transform: rotateY(180deg);
}

/* 定位正面和背面 */
.flip-card-front, .flip-card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backface-visibility: hidden; /* Safari */
  backface-visibility: hidden;
}

/* 设置正面的样式(如果图像缺失则备用) */
.flip-card-front {
  background-color: #bbb;
  color: black;
}

/* 设置背面的样式 */
.flip-card-back {
  background-color: dodgerblue;
  color: white;
  transform: rotateY(180deg);
}

亲自试一试