gpt4 book ai didi

css - 防止 Bootstrap 按钮填充 flexbox

转载 作者:行者123 更新时间:2023-12-04 14:21:26 24 4
gpt4 key购买 nike

我添加了 d-flexflex-column Bootstrap 类 card-body类,以便将包含的按钮与卡片底部对齐,如 this question's 中所建议的那样最佳答案。我的问题是这样做会导致按钮变宽以填充卡片。我怎样才能让按钮返回收缩以适应按钮的文本,就像在添加 flex 类之前所做的那样?

...
<div class="row pt-4" id="cards">
<div class="card-deck">

// THIS IS THE CARD WITH THE WIDENED BUTTON
<div class="card">
<img class="card-img-top img-fluid" src="{{url_for('static', filename='volunteer.png')}}" alt="exercise_image" >
<div class="card-body d-flex flex-column">
<h5 class="card-title">Giving Back</h5>
<p class="card-text">Help others and feel useful</p>
<a href="#" class="btn btn-primary mt-auto">Set a Goal</a>
</div>
</div>

// THIS CARD'S BUTTON SHRINKS TO FIT ITS TEXT
<div class="card">
<img class="card-img-top img-fluid" src="{{url_for('static', filename='spirituality.png')}}" alt="exercise_image" >
<div class="card-body">
<h5 class="card-title">Spirituality</h5>
<p class="card-text">Connect to something larger than yourself</p>
<a href="#" class="btn btn-primary">Set a Goal</a>
</div>
</div>

...

</div>
</div>
...

以下是按钮的外观:
Comparison of the two buttons

最佳答案

只需将您的 anchor 包装在另一个 div 中

<div>
<a href="#" class="btn btn-primary mt-auto">Set a Goal</a>
</div>

<!DOCTYPE html>
<html lang="en">

<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
<div class="row pt-4" id="cards">
<div class="card-deck">
<div class="card">
<img class="card-img-top img-fluid" src="{{url_for('static', filename='volunteer.png')}}" alt="exercise_image">
<div class="card-body d-flex flex-column">
<h5 class="card-title">Giving Back</h5>
<p class="card-text">Help others and feel useful</p>
<div>
<a href="#" class="btn btn-primary mt-auto">Set a Goal</a>
</div>
</div>
</div>
</div>
</div>
</div>
</body>

</html>



只需添加 align-items-center flex div 上的类

<!DOCTYPE html>
<html lang="en">

<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
<div class="row pt-4" id="cards">
<div class="card-deck">
<div class="card">
<img class="card-img-top img-fluid" src="{{url_for('static', filename='volunteer.png')}}" alt="exercise_image">
<div class="card-body d-flex flex-column align-items-center">
<h5 class="card-title">Giving Back</h5>
<p class="card-text">Help others and feel useful</p>
<a href="#" class="btn btn-primary mt-auto">Set a Goal</a>
</div>
</div>
</div>
</div>
</div>
</body>

</html>

关于css - 防止 Bootstrap 按钮填充 flexbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50885070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com