在卡片中垂直对齐内容的最佳方法是什么,同时仍然允许它们在悬停时制作动画,以显示不同的屏幕?
例如,如果我有卡片,每张卡片都有一个显示的正面和一个在悬停时淡入的背面:
<div class="items">
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
</div>
然后使用这个 css:
.item {
align-items: center;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
height: 200px;
background-color: tomato;
margin: 0 auto 1rem auto;
width: 200px;
position: relative;
}
.item .material-icons {
display: block;
}
.back,
.front {
opacity: 0;
position: absolute;
text-align: center;
width: 100%;
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
.front {
opacity: 1;
}
.item:hover .front {
opacity: 0;
}
.item:hover .back {
opacity: 1;
}
.material-icons {
font-size: 5em !important;
}
但是,在使用绝对位置后,我的垂直居中不起作用,我很确定这可能不是最好的方法!
您可以在此处看到半工作版本:http://jsfiddle.net/kmturley/8o29y7pd/26/
谢谢!
您可以使用
transform
进行居中。它适用于绝对位置元素。你根本不需要弹性框。
position: absolute;
left: 50%; top: 50%;
transform: translate(-50%, -50%);
text-align: center;
http://jsfiddle.net/1354hzqb/
只需在.back, .front
类中添加50%
和transform: translate(0, -50%)
的top
位置即可。
@import url('https://fonts.googleapis.com/css?family=Material+Icons');
.item {
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 200px;
background-color: tomato;
margin: 0 auto 1rem auto;
width: 200px;
position: relative;
}
.item .material-icons {
display: block;
}
.back,
.front {
opacity: 0;
position: absolute;
text-align: center;
width: 100%;
top: 50%;
transform: translate(0, -50%);
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
.front {
opacity: 1;
}
.item:hover .front {
opacity: 0;
}
.item:hover .back {
opacity: 1;
}
.material-icons {
font-size: 5em !important;
}
<div class="items">
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
</div>