我刚刚学习 flexbox 的工作原理,想构建一个非常粗略的原型(prototype),看看 flexbox 是否可以处理这种布局。
我基本上想看看 flexbox 是否可以支持具有不同宽度和高度但行高一致的 4 列表。
我不确定我是否很好地解释了布局,所以这里是我所追求的快速草图:
所以我关心的是:如果我使用 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 - is 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 - is 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/