我有三个 div:
- 分区 01
- Div 02 - 固定宽度 300px
- 分区 03
Div 01 和 Div 03 的宽度应该相同。
例子:
- 如果视口为1000px,Div 01 width=350px,Div 03 width=350px,
- 如果视口为 800px,则 Div 01 宽度 = 250px,Div 03 宽度 = 250px。
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
background: red;
flex: 1 auto;
height: 400px;
}
.middle {
background: blue;
width: 300px;
}
<div class="flex-container">
<div class="flex-item">This is a sample text. This is a sample text. This is a sample text. This is a sample text. This is a sample text. This is a sample text. This is a sample text.</div>
<div class="middle">sd</div>
<div class="flex-item">sd</div>
</div>
这是我想要的工作。但我需要将 overflow: scroll
添加到 flex-item 类。
添加后,它不起作用。如何解决?
原文由 nimal_sadu 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您希望 Div 01 和 Div 03 的宽度相同,那么
flex: 1 auto
不是一个可靠的工具。flex-grow: 1
组件将根据容器中的可用空间调整弹性项目的大小,这可能会有所不同。您需要为flex-item
类定义一个宽度。For the flex items to scroll vertically, you need to specify a
height
orflex-basis
(whenflex-direction
iscolumn
).For the flex items to scroll horizontally, you need to specify
width
orflex-basis
(whenflex-direction
isrow
).您还需要添加white-space: nowrap
。