Flexbox能够在这里派上用场。注意最后一个元素(带有三个<br>
)比其他两个元素更高,但它们的高度都相同:
* { box-sizing: border-box; }
.container {
display: flex;
flex-flow: row wrap;
}
.card-wrap {
flex: 0 0 33.333%;
display: flex;
padding: 10px; /* gutter width */
}
.card {
box-shadow: 0 0 4px rgba(0,0,0,0.4);
flex: 0 0 100%;
}
<div class="container">
<div class="card-wrap">
<div class="card"><br></div>
</div>
<div class="card-wrap">
<div class="card"><br></div>
</div>
<div class="card-wrap">
<div class="card"><br><br><br></div>
</div>
</div>
.container {
width: 100%;/*whatever size you want your container*/
border: 3px solid teal;
display: flex; /*Make sure to call this on your container*/
align-items: stretch; /*stretches all cards same height*/
justify-content: space-around; /*some space between cards*/
}
.card {
width: 150px; /*or whatever size card you want*/
border: 2px solid red;
}
<div class="container">
<div class="card">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</div>
<div class="card">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</div>
<div class="card">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
</div>
</div>
这是对我最有效的方法,
我在卡片容器上设置了display: flex
(而不是卡片本身)。
当你说display: flex
时,默认情况下flex-direction: row 。
然后只需在容器上添加align-items: stretch
...
这会导致我的卡片具有相等的高度。(此解决方案仅在flex-direction设置为row时有效)
希望这可以帮助到您
display:flex;flex-direction:row;
,但如果它们不在同一行,则可以使用jQuery。下面的代码片段将使具有class="card"
的每个div高度相同。$(document).ready(function (){
var maxHeight = 0;
for(i=0;i<$(".card").length;i++){
if($(".card").eq(i)){
var currentHeight = $(".card").eq(i).height();
if(currentHeight>=maxHeight){
maxHeight = currentHeight;
}
}
else{
break;
}
}
$(".card").height(maxHeight);
});
flex:row;
,但如果它们不在同一行,那就不起作用了。 - ab29007
.card
?HTML在哪里? - J. Titus