html - flexbox 可以处理不同大小的列但一致的行高吗?

标签 html css flexbox

我刚刚学习 flexbox 的工作原理,想构建一个非常粗略的原型(prototype),看看 flexbox 是否可以处理这种布局。

我基本上想看看 flexbox 是否可以支持具有不同宽度和高度但行高一致的 4 列表。

我不确定我是否很好地解释了布局,所以这里是我所追求的快速草图:

enter image description here

所以我关心的是:如果我使用 HTML 表格制作它,无论特定列的高度有多少,每一行都将保持一致。

最佳答案

I basically want to see if flexbox can support a 4-column table with varying width and height, but with consistent row height.

简单地说,Flexbox 与 HTML Table 共享的一个好特性是同一行上的 flex 元素可以具有相同的高度,这是由 align-items property 控制的。 .它的默认值为 stretch,这将使一行中的元素等高。

.flex-container {
  display: flex;
  flex-wrap: wrap;                    /*  allow items to wrap  */
  justify-content: space-between;     /*  vertical  */
  align-items: stretch;               /*  horizontal, is default
                                          and can be omitted  */
}

.flex-item {
  flex-basis: 23%;                    /*  give each item a width  */
  background-color: #ccc;
  padding: 10px;
  box-sizing: border-box;
}

.flex-item:nth-child(n+5) {
  margin-top: 10px;                   /*  from 5th item, add top margin  */
}
<div class="flex-container">
  <div class="flex-item">Left<br>high</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right<br>higher<br>higher</div>
  <div class="flex-item">Right</div>
</div>


可能值得一提的是,Flexbox 与 HTML Table 不共享的地方。

  • 动态等宽列宽。

如果一个元素的内容比同一中的其他元素宽,则其他元素不会将其宽度调整为等于最宽。

.flex-container {
  display: flex;
  flex-wrap: wrap;                    /*  allow items to wrap  */
  justify-content: space-between;     /*  vertical  */
  align-items: stretch;               /*  horizontal, is default
                                          and can be omitted  */
}

.flex-item {
  flex-basis: 23%;                    /*  give each item a width  */
  background-color: #ccc;
  padding: 10px;
  box-sizing: border-box;
}

.flex-item:nth-child(n+5) {
  margin-top: 10px;                   /*  from 5th item, add top margin  */
}
<div class="flex-container">
  <div class="flex-item">Left<br>high</div>
<div class="flex-item">Middle/Left&nbsp;-&nbsp;&nbsp;is&nbsp;wider</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right<br>higher<br>higher</div>
  <div class="flex-item">Right</div>
</div>

通过防止 flex 元素收缩,并使它们小于其内容,可以强制等宽。

请注意,overflow: hidden(或 visible 以外的任何值)与 min-width: 0 具有相同的效果有,所以在下面的示例中 min-width 可以省略。

.flex-container {
  display: flex;
  flex-wrap: wrap;                    /*  allow items to wrap  */
  justify-content: space-between;     /*  vertical  */
  align-items: stretch;               /*  horizontal, is default
                                          and can be omitted  */
}

.flex-item {
  flex-basis: 23%;                    /*  give each item a width  */
  background-color: #ccc;
  padding: 10px;
  box-sizing: border-box;
  
  flex-shrink: 0;                     /*  prevent from "shrink to fit" */
  min-width: 0;                       /*  allow to shrink past content width  */
  overflow: hidden;                   /*  hide overflowed content  */
}

.flex-item:nth-child(n+5) {
  margin-top: 10px;                   /*  from 5th item, add top margin  */
}
<div class="flex-container">
  <div class="flex-item">Left<br>high</div>
<div class="flex-item">Middle/Left&nbsp;-&nbsp;&nbsp;is&nbsp;wider</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right</div>
  <div class="flex-item">Right</div>
  
  <div class="flex-item">Left</div>
  <div class="flex-item">Middle/Left</div>
  <div class="flex-item">Middle/Right<br>higher<br>higher</div>
  <div class="flex-item">Right</div>
</div>


更合适的比较是 CSS Grid,它几乎可以做 HTML Table 可以做的所有事情,然后是一些 :)


这里有两个很好的 Flexbox 和 CSS Grid 指南:

关于html - flexbox 可以处理不同大小的列但一致的行高吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493500/

相关文章:

javascript - html 表单按钮 vs 输入类型 ="submit"vs this.submit()?

javascript - 在 html 和 js 中设置变量和值

javascript - 单击时激活菜单按钮

css - 将桌面的 Bootstrap 网格和容器宽度从 1170px 更改为 1000px

css - 将 flex 元素对齐到 flex-end

javascript - 用于样式和事件的 CSS 类的命名约定是什么?

html - 边框折叠不适用于嵌套表

html - table 垂直空间比水平空间大

css - 使用 flexbox 网格的 Bootstrap 4 砌体布局

html - Flexbox 元素中不希望出现的换行行为