卡未在引导程序 4 中居中 [重复]
Posted
技术标签:
【中文标题】卡未在引导程序 4 中居中 [重复]【英文标题】:Card not being centred in bootstrap 4 [duplicate] 【发布时间】:2021-07-22 13:04:00 【问题描述】:所以我试图让一组卡片居中,但我无法做到。这些卡片在一个容器中,即使使用 justify 内容中心,我也无法将它们居中。有什么解决办法吗?我的代码示例如下:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container justify-content-center text-center">
<div class="justify-content-center text-center">
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-3 col-xl-3 mx-lg-3 mt-4">
<div class="card text-center mx-auto" style="width: 18rem;">
<div class="embed-responsive embed-responsive-16by9">
<img class="card-img-top embed-responsive-item" src="https://source.unsplash.com/random/800x600" />
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Add to cart</a>
</div>
</div>
</div>
</div>
</div>
</div>
这是一张显示它没有居中的图像。
【问题讨论】:
将justify-content-center
类放在row
元素上,而不是其父元素上。现在,您将行“居中”,该行始终是 full 宽度。
【参考方案1】:
您必须将“justify-content-center
”类添加到包含“row
”类的元素中。
例如。
<div class="row justify-content-center">...</div>
sn-p
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container justify-content-center text-center">
<div class="justify-content-center text-center">
<div class="row justify-content-center">
<div class="col-sm-6 col-md-6 col-lg-3 col-xl-3 mx-lg-3 mt-4">
<div class="card text-center mx-auto" style="width: 18rem;">
<div class="embed-responsive embed-responsive-16by9">
<img class="card-img-top embed-responsive-item" src="https://source.unsplash.com/random/800x600" />
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Add to cart</a>
</div>
</div>
</div>
</div>
</div>
</div>
【讨论】:
以上是关于卡未在引导程序 4 中居中 [重复]的主要内容,如果未能解决你的问题,请参考以下文章