简体   繁体   English

flex-direction:单个孩子的“列”与“行”

[英]flex-direction: “column” vs “row” for single child

I have the following working layout.我有以下工作布局。 I want a vertical scrollbar only for .gallery-items element.我想要一个仅用于.gallery-items元素的垂直滚动条。 The layout should not overflow .split-panel container.布局不应溢出.split-panel容器。

Accidentally, I found that adding flex-direction: column to .pane-2 breaks the layout.意外地,我发现将flex-direction: column添加到.pane-2会破坏布局。

So, the question is why does this happen?那么,问题是为什么会发生这种情况?

As I understand, there should be no difference between flex-direction: row and flex-direction: column if there is only one child element.据我了解,如果只有一个子元素,则flex-direction: rowflex-direction: column之间应该没有区别。

 body { margin: 0; }.split-panel { height: 100vh; display: flex; flex-direction: column; }.pane-2 { overflow: hidden; display: flex; /* flex-direction: column; */ }.gallery { display: flex; flex-direction: column; }.gallery-items { display: flex; flex-wrap: wrap; overflow-y: auto; }.item { background: teal; width: 200px; height: 200px; margin: 10px; display: flex; }
 <div class="split-panel"> <div class="pane-1">Content</div> <hr /> <div class="pane-2"> <div class="gallery"> <div class="toolbar"> <h1>Title</h1> </div> <div class="gallery-items"> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> </div> </div> </div> </div>

Because the column direction is pushing the gallery element height to fit its content.因为列方向正在推动画廊元素高度以适应其内容。 If you set it to be explicitly 100% it works.如果您将其设置为明确 100%,则它可以工作。

 body { margin: 0; }.split-panel { height: 100vh; display: flex; flex-direction: column; }.pane-2 { overflow: hidden; display: flex; flex-direction: column; }.gallery { display: flex; flex-direction: column; height: 100%; // <-------------------- }.gallery-items { display: flex; flex-wrap: wrap; overflow-y: auto; }.item { background: teal; width: 200px; height: 200px; margin: 10px; display: flex; }
 <div class="split-panel"> <div class="pane-1">Content</div> <hr /> <div class="pane-2"> <div class="gallery"> <div class="toolbar"> <h1>Title</h1> </div> <div class="gallery-items"> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> <div class="item">Item</div> </div> </div> </div> </div>

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

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