【问题标题】:Equal height of columns in Bootstrap 4Bootstrap 4中列的等高
【发布时间】:2017-10-06 17:33:05
【问题描述】:

我在这个设计中有一点非常规的 DIV,如下图所示。我可以使用高度并做到这一点,但我希望它动态改变: 例如,如果 DIV 有更多内容并且右侧块之一的高度发生变化,则左侧 DIV 也会自动调整其高度。我想知道 flex 是否有帮助。以下是它应该如何更改为:

到目前为止,我有这个 HTML:

<div class="container">
  <div class="row row-eq-height">
      <div class="col-sm-8 col-8">
        <div class="black">
          <p>Bar Graph or Line Graph</p>
        </div>
      </div>
      <div class="col-sm-4 col-4">
        <div class="red">
          <p>numbers</p>
        </div>
        <div class="purple">
          <p>numbers</p>
        </div>
        <div class="green">
          <p>numbers</p>
        </div>
        <div class="blue">
          <p>numbers</p>
        </div>
      </div>
  </div>
</div>

和 CSS:

p { color: #fff; }
.black { background-color: black; }
.green { background-color: green; }
.red { background-color: red; }
.blue { background-color: blue; }
.purple { background-color: purple; }

JSFiddle Demo

【问题讨论】:

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


【解决方案1】:

您使用的是 Bootstrap 4,对吗? Bootstrap 4 默认实现了 flexbox,你可以使用 Bootstrap 4 提供的纯类轻松解决这个问题:

<div class="container">
  <div class="row">
      <div class="col-sm-8 col-8 black">
          <p>Bar Graph or Line Graph</p>
      </div>
      <div class="col-sm-4 col-4 d-flex align-content-around flex-wrap">
        <div class="red">
          <p>numbers and more and so on and on and on</p>
        </div>
        <div class="purple">
          <p>numbers</p>
        </div>
        <div class="green">
          <p>numbers</p>
        </div>
        <div class="blue">
          <p class="mb-0">numbers</p>
        </div>
      </div>
  </div>
</div>

JS Fiddle 链接:https://jsfiddle.net/ydjds2ko/

详情:

  • 你不需要 row-eq-height 类(它不在 Bootstrap4 中 无论如何)因为相等的高度是默认的。

  • 第一个具有col-sm-8 类的 div 具有正确的高度,它只是在黑色背景下不可见,因为内部 div 有它自己的高度。我刚刚删除了内部 div 并将 black 类添加到 col.如果您需要内部 div,请给它 (Bootstrap4) 类h-100,它将高度调整为父元素。

  • 类 col-sm-4 的 div 是类 d-flex align-content-around flex-wrap。他们正在对齐内容 div。引导独库:https://getbootstrap.com/docs/4.0/utilities/flex/#align-content

    • 使用 w-100 将此容器内 div 的宽度设置为父级的宽度
  • 因为&lt;p&gt; 在底部添加了一个边距,所以您的蓝色 div 在 结束时不会与黑色 div 齐平。我添加了课程 “mb-0”,它将边距底部设置为“0”,以便您可以看到它的工作原理。

无论右侧或左侧 div 是具有较大高度属性的那个,这都应该有效。

编辑:为内容对齐添加了类。

【讨论】:

  • 就是这么简单!!但是,在这种情况下它不起作用:jsfiddle.net/huoaynb0/1
  • 你是对的!正确 col 中的 div 项目无法自行正确对齐。幸运的是,Bootstrap 也涵盖了这种情况。你可以让它像这样工作:jsfiddle.net/ydjds2ko
【解决方案2】:

您可以为此使用flexbox

更新

还有一种方式:

https://jsfiddle.net/persianturtle/ar0m0p3a/5/

.row-eq-height > [class^=col] {
  display: flex;
  flex-direction: column;
}

.row-eq-height > [class^=col]:last-of-type div {
  margin: 10px;
}

.row-eq-height > [class^=col]:last-of-type div:first-of-type {
  margin-top: 0;
}

.row-eq-height > [class^=col]:last-of-type div:last-of-type {
  margin-bottom: 0;
}

.row-eq-height > [class^=col]:first-of-type .black {
  flex-grow: 1;
}

更新

更具体的类的更好方法:

https://jsfiddle.net/persianturtle/ar0m0p3a/3/

.row-eq-height > [class^=col]:first-of-type {
  display: flex;
}

.row-eq-height > [class^=col]:first-of-type .black {
  flex-grow: 1;
}

.blue p {
  margin: 0;
}

原路:

https://jsfiddle.net/persianturtle/ar0m0p3a/1/

[class^=col] {
  display: flex;
  flex-direction: column;
}

[class^=col] div {
  flex-grow: 1
}

【讨论】:

  • 太棒了。我们可以使用特定的类名来代替coldiv 吗?由于页面中有其他 col 和 div,因此可能会产生问题
  • @ElaineByene 绝对。使用对您有意义的选择器。我首先建议学习 flexbox,因为一开始有点棘手。如果您遇到困难,请随时询问 flexbox 特定的问题。乐于助人。
  • @ElaineByene,我已经更新了另一个使用更具体/语义选择器的实现。
  • 完美!原始代码与其他 DIV 存在问题。非常感谢:)
  • @ElaineByene,我还添加了第三种方式,因为第二种方式仅确保左列 (.black) 与包含四种颜色的第一列的高度匹配。第三种方法确保行的高度真正相等。
【解决方案3】:

这是一个非常简单的仅引导方法。您不需要任何额外的 CSS。

https://www.codeply.com/go/J3BHCFT7lF

<div class="container">
  <div class="row">
      <div class="col-8">
        <div class="black h-100">
          <p>Bar Graph or Line Graph</p>
        </div>
      </div>
      <div class="col-4 d-flex flex-column">
        <div class="red mb-2">
          <p>numbers</p>
        </div>
        <div class="purple mb-2">
          <p>numbers with more lines of content that wrap to the next line</p>
        </div>
        <div class="green mb-2">
          <p>numbers</p>
        </div>
        <div class="blue">
          <p>numbers</p>
        </div>
      </div>
  </div>
</div>

这使用Bootstrap 4 utility classes 使右列填充高度。


相关:
bootstrap 4 row height

【讨论】:

    猜你喜欢
    • 2019-01-20
    • 2017-04-02
    • 2018-03-13
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多