【问题标题】:How to get equal height columns childs in Bootstrap 4?如何在 Bootstrap 4 中获得相等高度的子项?
【发布时间】:2017-11-28 02:36:39
【问题描述】:

我想要达到的目标:

  1. 等高的列指向子级,因此列之间的间隙可见(Codepen 中的第一个解决方案,但在 Safari (Mac) 上失败)。

    或(如果可能的话)

  2. 等高的 Bootstrap 列之间有可见的间隙(如果我将背景颜色设置为 col 类 div - 由于填充,间隙不可见 - 您可以在 Codepen 中看到)。

    李>

我尝试过的:

  1. h-100 类 (height: 100%) 设置为直接子列,但在 Safari (see StackOverflow question) 上失败。
  2. col div 上使用水平margins 而不是padding,但这会破坏Bootstrap 的网格系统。

CODEPEN

HTML:

<div class="container">
  <h3>This is how it should work - fails on Safari (Mac)</h3>
  <div class="row">
    <div class="col-4">
      <div class="inner h-100">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
    </div>
    <div class="col-4">
      <div class="inner h-100">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner h-100">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner h-100">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner h-100">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
    </div>
  </div>
</div>

<div class="container">
  <h3>Backgrounds applied to col divs (gaps are not visible because of cols padding)</h3>
  <div class="row no-gutters">
    <div class="col-4 bg">
      <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
    </div>
    <div class="col-4 bg">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4 bg">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4 bg">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4 bg">
      <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
    </div>
  </div>
</div>

<div class="container">
  <h3>Basic Bootstrap HTML</h3>
  <div class="row">
    <div class="col-4">
      <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
    </div>
    <div class="col-4">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner">Inner</div>
    </div>
    <div class="col-4">
      <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
    </div>
  </div>
</div>

CSS:

.inner,
.bg {
  background: #ddd;
}
.h-100 {
  height: 100%;
}

【问题讨论】:

  • 您使用的是哪个版本的 Safari?你的 codepen 在我的看起来不错(版本 11.0 (11604.1.38.1.7))
  • 嗯,我现在无法访问我的 Mac,明天我会检查它。
  • 我也很好。你可以试试min-height:100%
  • 它可能在 11 中修复,我有 Safari 10.1,但它不起作用。我找到了另一个解决方案,但我会等几个小时才能得到答案;)

标签: html twitter-bootstrap css flexbox


【解决方案1】:

使 i.a. Safari 一直使用 Flexbox,而不是 height

d-flex 添加到col-4 元素,将col 添加到inner

d-flex 添加display: flex 并使inner,基于align-items 默认为stretch,填充其父级,col 添加flex: 1 并使其填充宽度。

堆栈sn-p

h3 {
  margin-top: 30px;
}
.col-4 {
  margin-bottom: 30px;
}
.inner {
  background: #ddd;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet"/>

<div class="container">
  <h3>This is how it should work - <strike>fails on Safari (Mac)</strike></h3>
  <div class="row">
    <div class="col-4 d-flex">
      <div class="inner col">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
    </div>
    <div class="col-4 d-flex">
      <div class="inner col">Inner</div>
    </div>
    <div class="col-4 d-flex">
      <div class="inner col">Inner</div>
    </div>
    <div class="col-4 d-flex">
      <div class="inner col">Inner</div>
    </div>
    <div class="col-4 d-flex">
      <div class="inner col">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
    </div>
  </div>
</div>

【讨论】:

  • 太棒了。我想通了,但现在我确信这是最好的解决方案。谢谢。
【解决方案2】:

将此类 align-items-stretchcol-4 一起用于您想要的所有列行内高度相等。

【讨论】:

  • 奇怪,我添加了这个类,对你的代码笔进行了一些更改,并且可以看到更新后的相等高度(我有类似的问题,我已经解决了,所以我很确定这应该可以解决这个问题)。
  • 我将该类添加到所有 col-4 div 中,但没有看到任何变化。
  • 我正在尝试嵌入 codepen,但无论如何无法添加它,这就是我所做的全部更改:1. 从 col-4 更改为 col(因为您有 6 列,只是为了使它们宽度相等) 2. 添加了此类“align-items-stretch” 3. 添加了一些颜色(使用您的内部类),以便我可以看到它们的宽度相等 4. 为列之间的边距添加了 mr-1。跨度>
  • 你可以只 fork 我的 Codepen,进行更改,保存并粘贴链接 :)
  • 检查此链接,我已更改基本引导 HTML 下的部分:codepen.io/pritesha/pen/pdOVYy
猜你喜欢
  • 2020-10-03
  • 2023-03-03
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2015-02-21
  • 2018-10-13
  • 2019-09-09
相关资源
最近更新 更多