简体   繁体   English

溢出:滚动不适用于弹性项目

[英]overflow:scroll not working on flex items

I have three div's: 我有三个div:

  • Div 01 第01组
  • Div 02 - fixed width 300px Div 02-固定宽度300px
  • Div 03 第03分部

Div 01 and Div 03 should be same width. Div 01和Div 03的宽度应相同。

Example: 例:

  • If viewport is 1000px, Div 01 width=350px and Div 03 width=350px, 如果视口为1000px,Div 01宽度= 350px和Div 03宽度= 350px,
  • If viewport is 800px, Div 01 width=250px and Div 03 width=250px. 如果视口为800像素,则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> 

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 01Div 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项目垂直滚动,您需要指定heightflex-basis (当flex-directioncolumn )。

For the flex items to scroll horizontally, you need to specify width or flex-basis (when flex-direction is row ). 为了使flex项目能够水平滚动,您需要指定widthflex-basis (当flex-directionrow )。 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> 

This fiddle can help you! 这个小提琴可以帮助您! To make overflow:scroll work use the below attributes: 要使overflow:scroll正常工作,请使用以下属性:

flex-grow: 1;
flex-basis:0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM