[英]overflow:scroll not working on flex items
I have three div's: 我有三个div:
Div 01 and Div 03 should be same width. Div 01和Div 03的宽度应相同。
Example: 例:
.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>
This is work as I want. 这是我想要的工作。 But I need to add
overflow: scroll
to flex-item class. 但是我需要添加
overflow: scroll
到flex-item类。
After adding this, it does not work. 添加后,它不起作用。 How to solve it?
怎么解决呢?
If you want Div 01 and Div 03 to be the same width, then flex: 1 auto
is not a reliable tool. 如果要使Div 01和Div 03具有相同的宽度,则
flex: 1 auto
是不可靠的工具。 The flex-grow: 1
component will size the flex item based on the available space in the container, which could vary. flex-grow: 1
组件将根据容器中的可用空间来调整flex项目的大小,该空间可能会有所不同。 You need to define a width for the flex-item
class. 您需要为
flex-item
类定义宽度。
For the flex items to scroll vertically, you need to specify a height
or flex-basis
(when flex-direction
is column
). 为了使flex项目垂直滚动,您需要指定
height
或flex-basis
(当flex-direction
为column
)。
For the flex items to scroll horizontally, you need to specify width
or flex-basis
(when flex-direction
is row
). 为了使flex项目能够水平滚动,您需要指定
width
或flex-basis
(当flex-direction
为row
)。 You also need to add white-space: nowrap
. 您还需要添加
white-space: nowrap
。
.flex-container { display: flex; width: 1000px; } .flex-item { /* flex: 1 auto; */ flex: 0 0 350px; overflow-x: scroll; white-space: nowrap; background: red; height: 400px; } .middle { /* width: 300px; */ flex: 0 0 300px; background: aqua; height: 400px; }
<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>
When you use flexbox all your div need to be in the flex flow. 使用flexbox时,所有div都必须位于flex流中。
So you better use flex: 1 1 300px
instead of width: 300px
因此,最好使用
flex: 1 1 300px
而不是width: 300px
.flex-container { display: flex; flex-flow: row wrap; justify-content: space-around; } .flex-item { background:red; flex: 1 1 0px; height: 200px; overflow: scroll; } .middle { background:blue; flex: 1 1 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. This is a sample text. </div> <div class="middle">sd</div> <div class="flex-item">sd</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.