【问题标题】:bootstrap flex do not center the contentbootstrap flex 不将内容居中
【发布时间】:2019-12-22 09:39:55
【问题描述】:

bootstrap flex 不将内容居中

<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,不知何故 class text-center justify-content-center 不起作用,有人可以帮忙吗?

【问题讨论】:

    标签: html css twitter-bootstrap bootstrap-4 flexbox


    【解决方案1】:

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

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

    <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

    <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>

    【讨论】:

    • 还有justify-content-around——这真的取决于你想要的外观,但很可能确实是justify-content-between :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2017-02-16
    • 2010-12-19
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多