twitter-bootstrap - 在 .d-flex 容器 Bootstrap 中使用 .col-* 4

标签 twitter-bootstrap css flexbox bootstrap-4

是否(允许)推荐使用 .col-* 作为 .d-flex 容器中的子级?还是不是应该使用 .d-flex 而是使用 .row 的方式?


Bootstrap 4 Docs我找不到任何在 .d-flex 容器中使用 .col-* 的示例。他们只使用这样的布局

<div class="d-flex flex-row">
  <div class="p-2">Flex item 1</div>
  <div class="p-2">Flex item 2</div>
  <div class="p-2">Flex item 3</div>
</div>
<div class="d-flex flex-row-reverse">
  <div class="p-2">Flex item 1</div>
  <div class="p-2">Flex item 2</div>
  <div class="p-2">Flex item 3</div>
</div>

但是这些 .p-2 类不会使这些列响应。它们只定义了一个 padding 并且没有给出 width

例如我想要这样的东西:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

<div class="d-flex flex-wrap">
  <div class="col-sm-3 col-12">
    Responsive cols
  </div>
  <div class="col-sm-3 col-12">
    Responsive cols
  </div>
  <div class="col-sm-3 col-12">
    Responsive cols
  </div>
  <div class="col-sm-3 col-12">
    Responsive cols
  </div>
</div>

根据 Bootstrap,这是好的还是坏的做法?我找不到任何与 .d-flex.col-* 组合相关的内容。

最佳答案

如文档所述...

"Rows are wrappers for columns. Each column has horizontal padding (called a gutter) for controlling the space between them. This padding is then counteracted on the rows with negative margins. This way, all the content in your columns is visually aligned down the left side."

因此,col-* 应该是直接子级 .row。此外,.rowcol-* 被设计成一个网格。这允许 columns to wrap根据需要到下一行。由于 flex-wrap,包装在您的示例中有效。

因此,col-* 应该在 row 而不是 d-flex 中的 2 个原因:

  • 与容器内容左/右对齐
  • 根据网格宽度换行

比较 row 内的 col-*d-flex:https://www.codeply.com/go/82ecXXvpkO

关于twitter-bootstrap - 在 .d-flex 容器 Bootstrap 中使用 .col-* 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49917129/

相关文章:

javascript - 使用 Vue.js 在 Bootstrap 模式中自动完成 Google map

javascript - 使用 Bootstrap 事件添加动画

html - 用多个具有多行动态数字的跨度元素填充 Bootstrap 列

css - LESS选择器: how to apply styling only for one of the elements from inside mixin?

javascript - 如何始终使用 flexbox 填充父高度?

html - 为什么 flex 元素不缩小过去的内容大小?

html - 尽管最小高度为 0,但 Div 在空时仍具有高度

html - 当我尝试使堆栈变大时, Font Awesome 图标堆栈不起作用

html - CSS+HTML : How to draw table/cell border

css - Flexbox 容器内的图标在 Chrome 上被拉伸(stretch)但在 Firefox 上没有