我正在使用Bootstrap 4 alpha 2并利用卡片。 具体来说,我正在使用来自官方文档的this example。正如标题所说,我怎样才能让所有卡片都高度相同?
编辑:我现在想的就是设置以下CSS规则:
<div class="card-columns">
<div class="card">
<img class="card-img-top" data-src="..." alt="Card image cap">
<div class="card-block">
<h4 class="card-title">Card title that wraps to a new line</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
<div class="card card-block">
<blockquote class="card-blockquote">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
<footer>
<small class="text-muted">
Someone famous in <cite title="Source Title">Source Title</cite>
</small>
</footer>
</blockquote>
</div>
<div class="card">
<img class="card-img-top" data-src="..." alt="Card image cap">
<div class="card-block">
<h4 class="card-title">Card title</h4>
<p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<div class="card card-block card-inverse card-primary text-xs-center">
<blockquote class="card-blockquote">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat.</p>
<footer>
<small>
Someone famous in <cite title="Source Title">Source Title</cite>
</small>
</footer>
</blockquote>
</div>
<div class="card card-block text-xs-center">
<h4 class="card-title">Card title</h4>
<p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
<div class="card">
<img class="card-img" data-src="..." alt="Card image">
</div>
<div class="card card-block text-xs-right">
<blockquote class="card-blockquote">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
<footer>
<small class="text-muted">
Someone famous in <cite title="Source Title">Source Title</cite>
</small>
</footer>
</blockquote>
</div>
<div class="card card-block">
<h4 class="card-title">Card title</h4>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
但这只是一个硬编码的解决方案,在一般情况下不会起作用。 我视图中的代码与文档中的代码相同,即:
{{1}}
答案 0 :(得分:94)
您可以将这些课程放在&#34;行&#34;或者&#34;栏&#34;? 如果你在行上使用它,就不会在卡片(边框)上看到它们。 https://v4-alpha.getbootstrap.com/utilities/flexbox/#align-items
<div class="container">
<div class="row">
<div class="col-lg-4 d-flex align-items-stretch">
这里的其他一些答案看起来很古怪&#39;为什么要使用最小高度甚至更糟的固定高度?
我认为这个问题(相等的高度)是离开浮动障碍div的原因的一部分?
答案 1 :(得分:79)
我正在使用Bootstrap 4(Beta 2)。与此同时,情况似乎发生了变化。我有同样的问题,并找到了一个简单的解决方案。这是我的代码:
<div class="container-fluid content-row">
<div class="row">
<div class="col-sm-12 col-lg-6">
<div class="card h-100">
… content card …
</div>
</div>
… all the other cards …
</div>
</div>
使用“col-sm-12 col-lg-6”我已经让卡片响应了。使用“card h-100”,我将所有卡设置为其父列的高度。在我的系统上这是有效的,但我不是专业人士。所以,希望我能帮助别人。
答案 2 :(得分:39)
最佳解决方案,Bootstrap 4可满足您的所有需求:使用.d-flex
和.flex-fill
课程。不要使用card-decks
,因为他们没有回应。
我使用了col-sm
,您可以使用所需的.col
课程。
这是野兽,尝试将浏览器窗口缩小为XS以查看它的实际效果:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet">
<div class="container">
<div class="row my-4">
<div class="col">
<div class="jumbotron">
<h1>Bootstrap 4 Cards all same height demo</h1>
<p class="lead">by djibe.</p>
<span class="text-muted">(thx to BS4)</span>
<p>Dependencies : standard BS4</p>
<p>
Enjoy the magic of flexboxes and leave the useless card-decks.
</p>
<div class="container-fluid">
<div class="row">
<div class="col-sm d-flex">
<div class="card card-body flex-fill">
A small card content.
</div>
</div>
<div class="col-sm d-flex">
<div class="card card-body flex-fill">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
</div>
</div>
<div class="col-sm d-flex">
<div class="card card-body flex-fill">
Another small card content.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
&#13;
答案 3 :(得分:8)
更新:在 Bootstrap 4 中,flexbox现在是默认行,每行card-deck
行将包含3张卡片。卡片将填满整个高度。
http://www.codeply.com/go/x91w5Cl6ip
Bootstrap 4 alpha card-columns
使用的CSS3列实际上并不支持相同的高度(除了仅在Firefox中支持的列填充)。
如果你改为启用Bootstrap 4 flexbox模式,你可以使用card-deck
和一点CSS来均衡高度并包装每3列。
@media (min-width:34em) {
.card-deck > .card
{
width: 29%;
flex-wrap: wrap;
flex: initial;
}
}
答案 4 :(得分:7)
我是这样做的:
CSS:
.my-flex-card > div > div.card {
height: calc(100% - 15px);
margin-bottom: 15px;
}
HTML:
<div class="row my-flex-card">
<div class="col-lg-3 col-sm-6">
<div class="card">
<div class="card-block">
aaaa
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6">
<div class="card">
<div class="card-block">
bbbb
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6">
<div class="card">
<div class="card-block">
cccc
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6">
<div class="card">
<div class="card-block">
dddd
</div>
</div>
</div>
</div>
答案 5 :(得分:6)
您可以使用卡座,它将对齐所有卡...这来自bootstrap 4官方页面。
<div class="card-deck">
<div class="card">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<div class="card">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<div class="card">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
答案 6 :(得分:3)
您可以应用h-100
类,它代表高度100%。
答案 7 :(得分:3)
另一种有用的方法是Card Grids:
<div class="row row-cols-1 row-cols-md-2">
<div class="col mb-4">
<div class="card">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
<div class="col mb-4">
<div class="card">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
<div class="col mb-4">
<div class="card">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content.</p>
</div>
</div>
</div>
<div class="col mb-4">
<div class="card">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
</div>
答案 8 :(得分:3)
将卡片包裹在
中 <div class="card-group"></div>
或
<div class="card-deck"></div>
答案 9 :(得分:2)
我采取了略微不同的方法。使用Card Deck wrapper
我添加了一个限制卡块高度的样式规则:
.card .card-block {max-height:300px;overflow:auto;}
答案 10 :(得分:2)
这可能对你有帮助
只需按照此代码
<div class="row">
<div class="col-md-4 h-100">contents....</div>
<div class="col-md-4 h-100">contents....</div>
<div class="col-md-4 h-100">contents....</div>
</div>
使用引导类“h-100”
答案 11 :(得分:1)
我是新手,因此,如果我一起缺少任何东西,请原谅。
我尝试了许多上述方法。如果将高度设置为100%,则卡将始终扩展到最长卡的最大长度。容器将约束所有相同的长度。
我想看一下容器,所以我用背景色来帮助我看看发生了什么。
<div class="col-lg-3" style="background-color: rosybrown;">
<div class="card w-100" Style="height: 100%">
<div class="card-body">
<img src="images/beHappy.png" alt="" class="img-fluid rounded-circle w-25 mb-3">
<h4>CBe Happy</h2>
答案 12 :(得分:1)
您可以使用 d-flex and flex-column
将 card-body
添加到 div。
Hetre 是一个工作示例:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap-grid.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-4 d-flex align-self-stretch">
<div class="card shadow-sm mb-4">
<img src="https://placehold.it/500x300" class="card-img-top" alt="">
<div class="card-body d-flex flex-column">
<h5 class="card-title text-uppercase">Longer title here that wraps two lines</h5>
<p class="text-muted">Lorem ipsum dolor </p>
<div class="mt-auto border border-danger">
<p class="text-uppercase mb-0">Hello World!</p>
<p class="text-uppercase">consectetur adipiscing elit</p>
<a href="#" class="btn btn-info btn-block">CTA</a>
</div>
</div>
</div>
</div>
<div class="col-4 d-flex align-self-stretch">
<div class="card shadow-sm mb-4">
<img src="https://placehold.it/500x300" class="card-img-top" alt="">
<div class="card-body d-flex flex-column">
<h5 class="card-title text-uppercase">Smaller title here that wraps 1 line</h5>
<p class="text-muted">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="mt-auto border border-danger">
<p class="text-uppercase mb-0">Hello World!</p>
<p class="text-uppercase">adipiscing </p>
<a href="#" class="btn btn-info btn-block">CTA</a>
</div>
</div>
</div>
</div>
<div class="col-4 d-flex align-self-stretch">
<div class="card shadow-sm mb-4">
<img src="https://placehold.it/500x300" class="card-img-top" alt="">
<div class="card-body d-flex flex-column">
<h5 class="card-title text-uppercase">Longer title here that wraps 3 lines, Lorem ipsum dolor sit amet, consectetur adipiscing elit</h5>
<p class="text-muted">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="mt-auto border border-danger">
<p class="text-uppercase mb-0">Hello World!</p>
<a href="#" class="btn btn-info btn-block">CTA</a>
</div>
</div>
</div>
</div>
</div>
</div>
答案 13 :(得分:1)
对于 Bootstrap 5 和 4
HEIGHT-将所有卡片放入 div 中,class="card-deck"
WIDTH - 给你所有的卡片样式 {min-width: 50ch}
答案 14 :(得分:1)
根据问题我有一个答案,它可能对您有所帮助,您可以使用它。您可以使用 height
属性并将其设置为 height: 100vh;
我已经使用了 70vh
并且卡片将是您提供的固定高度。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap 4.5 CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- Bootstrap JS Requirements -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="index.css">
<title>Document</title>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
<style>
/*Use height property to set equil card height*/
.card {
background-color: #56568a;
margin: 10px !important;
height: 70vh;
width: auto;
}
</style>
<body>
<div class="container">
<div class="col-sm-6 col-md-6 col-lg-3">
<div class="card">
<img class="card-img-top img-fluid" src="img5.PNG" alt="Card image cap">
<div class="card-block">
<small class="purple"> Product Launch</small>
<h4 class="card-title">Leveraging Social Proof for Growth</h4>
<p class="card-text">Duolingo removes language barriers by connecting people that need
websites translated with students
that learning a
language...</p>
<p class="card-text "><i class="far fa-user"></i><small class="purple">Praveen</small>
<i class="far fa-clock "></i><small class="purple">Today</small>
</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-3">
<div class="card">
<img class="card-img-top img-fluid" src="img6.PNG" alt="Card image cap">
<div class="card-block">
<small class="purple"> ddc</small>
<h4 class="card-title">Leveraging Social Proof for Growth</h4>
<p class="card-text">Duolingo removes language barriers by connecting people that need
websites translated with students
that learning a
language...</p>
<p class="card-text"><i class="far fa-user"></i><small class="purple">Praveen</small>
<i class="far fa-clock "></i><small class="purple">2 days ago</small>
</p>
</div>
</div>
</div>
</div>
</body>
</html>
答案 15 :(得分:0)
实现此(我知道)的最简单方法:
.text-monospace
应用于卡片中的所有文本。在卡的标题和/或其他文本中添加.text-truncate
。这将文本强制为一行。使卡片具有相同的高度。
答案 16 :(得分:0)
我遇到了这个问题,大多数人使用jQuery ...这是我的解决方案。 “别介意,我只是这个领域的初学者...”
if currentColor === "#fafafa"
.cards-collection {
.card-item {
width: 100%;
margin: 20px 20px;
padding:0 ;
.card {
border-radius: 10px;
button {
border: inherit;
}
}
}
}
如果任何卡项目高度为400px(基于其内容),因为flex-align的默认设置为Stretch,则.card-item(row或card-collection类的子级)将被拉伸。使得卡的高度为父代的100%将会占据此全高。
我希望我是正确的。是吗?
答案 17 :(得分:-1)
如果有人有兴趣,有一个名为jquery.matchHeight.js的jquery插件
https://github.com/liabru/jquery-match-height
matchHeight使所有选定元素的高度完全相等。 它处理许多边缘情况,导致类似的插件失败。
对于一排牌,我使用:
<div class="row match-height">
然后在网站范围内启用:
$('.row.match-height').each(function() {
$(this).find('.card').not('.card .card').matchHeight(); // Not .card .card prevents collapsible cards from taking height
});
答案 18 :(得分:-3)
<强> jsfiddle 强>
只需使用CSS添加所需的高度,例如:
.card{
height: 350px;
}
您必须添加自己的CSS。
如果你查看文档,这是针对 Masonry 样式的 - 那就是它们的高度不同。
答案 19 :(得分:-3)
这对我来说很好:
<div class="card card-body " style="height:80% !important">
在引导程序常规CSS上强制使用我们的CSS。