html 列适合内容但并排时匹配高度(没有 jQuery)

标签 html css flexbox zurb-foundation

我有 3 个内容不同的 div。由于它们的背景,我希望 div 的高度匹配。我的问题是如何在没有 jQuery/Javascript 的情况下使用纯 html/css 实现这一点,如果可能的话。

小的时候,每个 div 变成 12 列,所以它们的高度适合内容。中等时,绿色和蓝色的高度相同。较大时,灰色框与蓝色和绿色 div 的高度匹配。

https://jsfiddle.net/Ljzr2krv/

.col-1 {
  background-color: blue;
}
.col-2 {
  background-color: green;
}
.col-3 {
  background-color: grey;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/css/foundation.min.css" rel="stylesheet"/>
<div class="row collapse">
  <div class="large-3 small-12 columns">
    <div class="row collapse">
      <div class="small-12 medium-6 large-12 columns col-1">
      The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
      </div>
      <div class="small-12 medium-6 large-12 columns col-2">
      The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. 
      </div>
    </div>
  </div>
  <div class="large-9 small-12 columns col-3">
  The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
  </div>
</div>

This is what I'm trying to get as an end result.

目前,为了实现这一点,我一直在使用 jQuery 获取 div 的自然高度,并根据窗口大小分配较大的高度。每次窗口调整大小时都会调用它。

我希望有一个 html/css 解决方案可以实现 div 的这种排列,同时允许 div 的高度像上图一样对齐。

最佳答案

您可以添加以下规则。它似乎有效。

.row.collapse {
    display:flex;
    flex-wrap:wrap;
}

.col-1 {
  background-color: blue;
}
.col-2 {
  background-color: green;
}
.col-3 {
  background-color: grey;
}

.row.collapse {
    display:flex;
    flex-wrap:wrap;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/css/foundation.min.css" rel="stylesheet"/>
<div class="row collapse">
  <div class="large-3 small-12 columns">
    <div class="row collapse">
      <div class="small-12 medium-6 large-12 columns col-1">
      The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
      </div>
      <div class="small-12 medium-6 large-12 columns col-2">
      The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. 
      </div>
    </div>
  </div>
  <div class="large-9 small-12 columns col-3">
  The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
  </div>
</div>

关于html 列适合内容但并排时匹配高度(没有 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614569/

相关文章:

html - 如何使用 AngularJS ng-include?

html - 在 div 悬停时更改链接颜色

jquery - 如何使用javascript将CSS文本装饰删除线设置为红色

html - IE11/Edge 中的可滚动 Flex-box Child 问题

html - img 缩放到 div 内的视口(viewport)

html - 针对特定 CSS 定义的背景图像禁用 Pagespeed 图像优化

html - 组合多个伪选择器

javascript - 在调整窗口大小时设置产品 slider 宽度以及 CSS 媒体查询

html - 3 张图片并排放置,每张图片上方有文字(每张 2 个词,以换行符分隔),整个内容居中

html - 如何在框架上水平和垂直居中多个表单元素?