html - 纯 CSS 响应式翻转框
问题描述
我想要一些翻转盒子。鼠标悬停时,图像翻转并显示文本。这有效,但它没有响应(第二行框结束于前一行)。问题是 .f_contenedor 高度,但我希望它具有响应性,所以我使用“padding-top:56%”属性。
如何确保“f_contendor”高度与 .face 相同?
这里有一个例子http://bregna.org/flip.html
这是片段。是相同的代码,只是背景不同。
.f_contenedor:hover .f_card {
transform: rotateY(180deg);
box-shadow: -5px 5px 5px #aaa;
}
.f_contenedor {
position: relative;
padding: 10px;
width: 50%;
height: auto;
z-index: 1;
float: left;
perspective: 1000;
box-sizing:border-box;;
}
.f_card {
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: all .3s linear;
}
@media (min-width: 768px) {
.f_contenedor {
width: 50%;
}
}
@media (min-width: 992px) {
.f_contenedor {
width: 30%;
}
}
.shadow {
-moz-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
-webkit-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
}
.face {
background-size: cover;
background-repeat: no-repeat;
}
.face {
position: absolute;
width: 100%;
height: 0;
padding-top: 56%;
backface-visibility: hidden;
}
.face.back {
display: block;
transform: rotateY(180deg);
box-sizing: border-box;
height: 100%;
color: white;
text-align: center;
background-color: #aaa;
}
.face.back span {
margin-top: -56%;
padding: 12px;
display: block;
}
<!doctype html>
<html lang="es_MX">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" type="image/png" href="/favicon.png"/>
<title>Flip</title>
</head>
<body>
<div class="f_contenedor">
<div class="f_card" style="background-color: black">
<div class="back face text-center shadow">
<span>
<h1>WEB</h1>
<p>Accusantium delectus non quae. Aliquam delectus, dolorem ea id, inventore libero molestias numquam, porro quasi quibusdam reiciendis sequi soluta tenetur veniam voluptatem.
</p></span>
</div>
</div>
</div>
<div class="f_contenedor">
<div class="f_card">
<div class="front face shadow" style="background-color: orange">
</div>
<div class="back face text-center shadow">
<span>
<h1>CMS</h1>
<p>Accusantium delectus non quae. Aliquam delectus, dolorem ea id, inventore libero molestias numquam, porro quasi quibusdam reiciendis sequi.
</p></span>
</div>
</div>
</div>
<div class="f_contenedor">
<div class="f_card">
<div class="front face shadow" style="background-color : firebrick;">
</div>
<div class="back face text-center shadow">
<span>
<h1>THE GRID</h1>
<p>Accusantium delectus non quae. Aliquam delectus, dolorem ea id, inventore libero molestias numquam.
</p></span>
</div>
</div>
</div>
<div class="f_contenedor">
<div class="f_card">
<div class="front face shadow" style="background-color: green">
</div>
<div class="back face text-center shadow">
<span>
<h1>FLOWERS</h1>
<p>Ad, aperiam blanditiis dolorem dolorum ea earum ipsam laboriosam molestias non odio provident .
</p></span>
</div>
</div>
</div>
<div class="f_contenedor">
<div class="f_card">
<div class="front face shadow" style="background-color: yellow">
</div>
<div class="back face text-center shadow">
<span>
<h1>DRINKS</h1>
<p>Accusamus animi earum enim ex impedit nesciunt nihil quod quos vel? Dolores fugit harum modi placeat quasi quam?
</p></span>
</div>
</div>
</div>
<div class="f_contenedor">
<div class="f_card">
<div class="front face shadow" style="background-color: greenyellow">
</div>
<div class="back face text-center shadow">
<span>
<h1>MURALS</h1>
<p>Aperiam aut consequuntur nostrum rem similique temporibus veritatis.
</p></span>
</div>
</div>
</div>
</body>
</html>
解决方案
您可以稍微调整您的 CSS,使.back
面部完全定位为绝对,并与父对象的顶部对齐。此外,您可能希望将 调整.face
为相对定位。
所以沿着这些思路:
.face {
/* position: absolute; Remove this */
position:relative; /* Position the face relatively by default */
width: 100%;
height: 0;
padding-top: 56%;
backface-visibility: hidden;
}
.face.back {
position:absolute; /* For back faces, these should be positioned with absolute */
top:0; /* Align back faces with the top of the parent */
display: block;
transform: rotateY(180deg);
box-sizing: border-box;
height: 100%;
color: white;
text-align: center;
background-color: #aaa;
}
推荐阅读
- python - 如何将用户输入限制为预先指定的单词数?
- pine-script - 布尔值 - 这个栏是从最近的 10 个周期开始的吗?
- ssh - putty 如何在不与用户进行任何交互的情况下运行命令?
- django - 如何在 Django URL 中正确重定向,我在重定向时遇到问题
- python - N阶差和N阶差
- terraform - 使用 Terraform 将 Helm 部署到 GKE 时经常出错
- javascript - 搜索html表格
- c++ - C++程序产生异常?
- swift - 使用 Xcode 11.4.1,为什么这些错误显示在动画部分?
- javascript - Node.js + Express + i18next 如何同时管理两种语言