简体   繁体   English

如果行中没有足够的空间,请将弹性方向切换到列

[英]Make flex-direction switch to column if there's not enough space in a row

In the example below, what I want is to have .inner-flexbox have flex-direction: row; 在下面的示例中,我想要的是让.inner-flexbox具有flex-direction: row; when its child elements can fit on a single row, but switch to flex-direction: column; 当其子元素可以放在一行中,但切换到flex-direction: column; when they cannot. 当他们不能。 I don't think media queries are what I want here because I would be basing them on whether the viewport width is greater than or less than the width of all the <div> s in .inner-flexbox in a row plus the width of the <aside> . 我认为媒体查询不是我想要的,因为我将基于视口宽度是否大于或小于连续.inner-flexbox中所有<div>的宽度加上的宽度。 <aside> Basically, I don't want the items inside .inner-flexbox wrapping—I want them either all in one row, or all in one column. 基本上,我不需要.inner-flexbox包装内的项目, .inner-flexbox希望它们全部排成一列,或者全部排成一列。

(distilled example): I have a flexbox that contains two items. (摘录示例):我有一个包含两个项目的flexbox。 One is a "normal" element, the other is another flexbox. 一个是“普通”元素,另一个是另一个flexbox。

 .outer-wrapper { display:flex; width: 100%; height: auto; background-color: gray; flex-wrap: nowrap; } .inner-flexbox { display: flex; flex-wrap: wrap; flex-direction: row; } .inner-flexbox > div { width: 10rem; height: 10rem; background-color: salmon; margin: 10px; } aside { background-color: blue; width: 15rem; height: 15rem; } 
 <div class="outer-wrapper"> <div class="inner-flexbox"> <div>Block 1</div> <div>Block 2</div> <div>Block 3</div> </div> <aside><!-- Other content here --></aside> </div> 

Try This Code 试试这个代码

 .outer-wrapper { display: flex; width: 100%; height: auto; background-color: gray; flex-wrap: nowrap; } .inner-flexbox { width: 75%; display: flex; flex-wrap: wrap; flex-direction: row; flex-flow: row wrap; justify-content: space-between; } .inner-flexbox>div { width: 10rem; height: 10rem; background-color: salmon; margin: 10px; } aside { width: 25%; background-color: blue; width: 15rem; height: 15rem; float: right; display: block; } 
 <div class="outer-wrapper"> <div class="inner-flexbox"> <div>Block 1</div> <div>Block 2</div> <div>Block 3</div> </div> <aside> <!-- Other content here --> </aside> </div> 

In the end I resorted to scripting, as @LGSon suggested. 最后,正如@LGSon建议的那样,我求助于脚本。 I used jQuery to compare the total width of the inner flexbox to the width of the container minus the width of the item next to it. 我使用jQuery将内部flexbox的总宽度与容器的宽度减去其旁边项目的宽度进行比较。

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

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