简体   繁体   English

如何仅将 flex-direction 应用于 flex 容器内的某些元素?

[英]How to apply flex-direction only to certain elements inside a flex container?

I have a flex container which has 3 elements inside of it (X, Y, Z).我有一个 flex 容器,里面有 3 个元素(X、Y、Z)。

Currently, X, Y and Z are stacked like a row.目前,X、Y 和 Z 像一行一样堆叠。 However, I want to make Y and Z be stacked like a column without affecting X.但是,我想让 Y 和 Z 像柱子一样堆叠而不影响 X。

Here is the Codepen which also explains better what I want to achieve:这是 Codepen,它也更好地解释了我想要实现的目标:

https://codepen.io/kibezin/pen/qBOQypy https://codepen.io/kibezin/pen/qBOQypy

I have tried applying flex-direction: column : to Y and Z (user's awards and user's sketches) however it doesn't affect them (I guess it can only be applied through the parent node).我曾尝试将flex-direction: column : 应用于 Y 和 Z(用户的奖项和用户的草图),但它不会影响它们(我想它只能通过父节点应用)。 Is there any way I can achieve this?有什么办法可以做到这一点?

Or even better, perhaps using Flexbox is overkill and there's a better alternative?甚至更好,也许使用 Flexbox 是矫枉过正,还有更好的选择?

Thank you.谢谢你。

You need to have a wrapper and style that accordingly.你需要有一个包装器和相应的样式。 As you noted - flex only applies to DIRECT children of the felx container - there fore - adding a wrapper will break that cycle and apply normal layout.正如您所指出的 - flex 仅适用于 felx 容器的 DIRECT 子级 - 因此 - 添加包装器将打破该循环并应用正常布局。

 #profile-container { display: flex; } #profile-container>div:not(#profile-info) { flex-wrap: 1; } #profile-info { transform: translateY(25px); padding-top: 10px; text-align: center; margin-left: 10px; margin-right: 20px; flex-grow: 1; min-width: 250px; max-width: 250px; height: 500px; border-radius: 10px; background-color: rgba(0, 0, 0, 0.1); } #profile-sketch { background-color: yellow; display: block; width: 150px; height: 150px; border: 1px solid black; }
 <div id="profile-container"> <aside id="profile-info"> <div id="profile-sketch"></div> <br> <hr> <span id="profile-username">username</span> <span id="profile-karma">350</span> <br> <span id="profile-bio">About:</span> </aside> <div class="profile-awards-and-"> <div id="profile-awards-wrapper"> <h2>user's awards</h2> <ul> <li> award 1</li> <li> award 2</li> <li> award 3</li> </ul> </div> <div id="profile-sketches-wrapper" > <h2>user's sketches</h2> <div id="profile-sketches-placeholder"> SKETCH 1, SKETCH 2, SKETCH 3 </div> </div> </div> </div>

This can be done in flexbox without nesting, The only drawback is you have to define a height.这可以在没有嵌套的 flexbox 中完成,唯一的缺点是你必须定义一个高度。

 body * { padding: 10px; border: 1px solid; } [flex] { display: flex; flex-wrap: wrap; flex-direction: column; height: 200px; } [a] { flex: 1 0 100%; box-sizing: border-box; } [b], [c] { flex: 1 0 0%; }
 <div flex> <div a>A</div> <div b>B</div> <div c>C</div> </div>


The more flexible way to achieve this is to use CSS Grid实现这一点的更灵活的方法是使用 CSS Grid

 body * { padding: 10px; border: 1px solid; } [grid] { display: grid; grid-template-columns: 1fr 1fr; } [a] { grid-row: span 2; }
 <div grid> <div a>A</div> <div b>B</div> <div c>C</div> </div>

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

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