首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bootstrap flex不将内容居中

bootstrap flex不将内容居中
EN

Stack Overflow用户
提问于 2019-08-16 10:30:09
回答 1查看 421关注 0票数 1

bootstrap flex不将内容居中

代码语言:javascript
复制
<div class="d-flex flex-row bd-highlight mb-3">
  <div class="p-2 bd-highlight text-left"><i class="fa fa-chevron-left" aria-hidden="true"></i></div>
  <div class="p-2 bd-highlight text-muted text-center justify-content-center" style="text-transform: uppercase;">comments</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>

我使用的是bootstrap 4,不知何故text-center justify-content-center类不能工作,有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2019-08-16 10:41:58

Bootstrap4 justify classes需要应用于行(使用d-flex类),而不是列。

justify-content-center移到父级,使元素按预期居中。

代码语言:javascript
复制
<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="d-flex flex-row bd-highlight mb-3 justify-content-center">
  <div class="p-2 bd-highlight text-left"><i class="fa fa-chevron-left" aria-hidden="true"></i></div>
  <div class="p-2 bd-highlight text-muted" style="text-transform: uppercase;">comments</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>

请注意,这将使所有元素居中。如果你正在寻找一个“左-中-右”的布局(这样只有中间的元素居中),那么你想要justify-content-between

代码语言:javascript
复制
<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="d-flex flex-row bd-highlight mb-3 justify-content-between">
  <div class="p-2 bd-highlight text-left"><i class="fa fa-chevron-left" aria-hidden="true"></i></div>
  <div class="p-2 bd-highlight text-muted" style="text-transform: uppercase;">comments</div>
  <div class="p-2 bd-highlight">Flex item 3</div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57518272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档