【问题标题】:The .justify-content-center class isn't working when applied to .row (Bootstrap 4.4.1).justify-content-center 类在应用于 .row 时不起作用(Bootstrap 4.4.1)
【发布时间】:2020-06-28 21:52:35
【问题描述】:

我有以下代码:

<div class="container pt-5">
    <div class="row">
        <div class="col-6 col-sm-3">
          <ul>
              <li>Example List Item</li>
              <li>Example List Item</li>
              <li>Example List Item</li>
          </ul>
        </div>
        <div class="col-6 col-sm-3">
            <ul>
              <li>Example List Item</li>
              <li>Example List Item</li>
              <li>Example List Item</li>
            </ul>
        </div>
        <div class="col-6 col-sm-3">
            <ul>
              <li>Example List Item</li>
              <li>Example List Item</li>
              <li>Example List Item</li>
            </ul>
        </div>
        <div class="col-6 col-sm-3">
            <ul>
              <li>Example List Item</li>
              <li>Example List Item</li>
              <li>Example List Item</li>
            </ul>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 text-center">
            <p class="copyright pt-5 text-muted">Blah blah blah.</p>
        </div>
    </div>
</div>

我正在使用最新的 Boostrap 4 (4.4.1) 并希望将列在顶部 .row 中居中。我这样做有困难。当我将 .justify-content-center 添加到该行时,绝对没有任何效果(列集在我的容器开始处保持向左刷新)。我尝试了其他变体,例如 .justify-content-end 以查看是否有任何效果。我还尝试使用 justify-content: center; 创建自己的 css 类,并使用它代替 Bootstrap 类。没有运气。第二个 .row 居中很好。

【问题讨论】:

  • 因为你使用的是 4 col-sm-3,所以它需要所有的行空间,只需尝试更改为 col-sm-2
  • 您可以使用 mx-auto 在引导程序中以列为中心。如果您的元素是内联的,您可以使用列上的 text-center 类。
  • 感谢@JasarOrion 和@Steve。我将间距减小到 col-sm-2 和 justify-content-around 中心,并为我很好地分隔了列。

标签: css twitter-bootstrap bootstrap-4


【解决方案1】:

试试看:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row justify-content-md-center">
    <div class="col col-2">
      1 of 3
    </div>
    <div class="col-md-auto">
      Variable width content
    </div>
    <div class="col col-2">
      3 of 3
    </div>
  </div>
</div>

【讨论】:

  • 所以主要问题是如果列跨越行的整个宽度(跨越 12 列网格),将无法看到它们居中。如果减少 12 个网格占用的空间量,您将能够看到居中效果。然后,您还可以根据需要为列添加边距和填充。
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 2020-09-01
  • 2021-01-17
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多