[英]CSS Flexbox: How to arrange three Items in a Row, but two of them below each other?
Assumed we have this simple CSS flexbox layout:假设我们有这个简单的 CSS flexbox 布局:
.flex-container { display: flex; justify-content: space-between; align-items: center; height: 100vh; } .flex-item:nth-child(1) { order: 0; } .flex-item:nth-child(2) { order: 1; align-self: flex-start; } .flex-item:nth-child(3) { order: 1; align-self: flex-end; } .flex-item { width: 50px; height: 50px; background:red; }
<div class="flex-container"> <div class="flex-item"></div> <div class="flex-item"></div> <div class="flex-item"></div> </div>
Description of the issue:问题说明:
Unfortunately flex-item
2 and 3 are not located at the right edge below each other, although they are both set to order: 1
.不幸的是flex-item
2 和 3 并不位于彼此下方的右边缘,尽管它们都设置为order: 1
。
Question:问题:
How can I arrange flex-item 2
to be at the upper right edge, while flex-item 3
is located at the lower right edge (item 1 should remain vertically centered at left edge)?如何将flex-item 2
安排在右上边缘,而flex-item 3
位于右下边缘(项目 1 应保持在左边缘垂直居中)?
I did not yet figure out on how to arrange all three items in a row, but the latter two items below each other within this row.我还没有弄清楚如何将所有三个项目排成一行,但后两个项目在这一行中彼此相邻。
If you want a block to be at up right, one at the center and other at the bottom right, then you can use this snippet.如果您希望一个块在右上角,一个在中心,另一个在右下角,那么您可以使用此代码段。 The flex item 1
should be at flex-start
, the flex item 3
should be at the flex end
and the flex item 2
should be where it is. flex item 1
应该在flex-start
, flex item 3
应该在flex end
并且flex item 2
应该在它所在的位置。
* { padding: 0; margin: 0; } .flex-container { display: flex; justify-content: space-between; align-items: center; height: 100vh; } .flex-item:nth-child(1) { order: 0; align-self: flex-start; } .flex-item:nth-child(2) { order: 1; } .flex-item:nth-child(3) { order: 1; align-self: flex-end; } .flex-item { width: 50px; height: 50px; background:red; }
<div class="flex-container"> <div class="flex-item"></div> <div class="flex-item"></div> <div class="flex-item"></div> </div>
I guess you are trying to do that.我猜你正在尝试这样做。
* { padding: 0; margin: 0; } .flex-container { display: flex; justify-content: space-between; align-items: center; height: 100vh; } .flex-container-col { display: flex; flex-direction:column; justify-content: space-between; align-items: center; height: 100vh; } .flex-item:nth-child(1) { order: 0; align-self: flex-center; } .box { width: 50px; height: 50px; background:red; }
<div class="flex-container"> <div class="flex-item box"></div> <div class="flex-item flex-container-col"> <div class="box"></div> <div class="box"></div> </div> </div>
If you want to both item 2 and 3 top of the right edge together, you can delete justify-content in flex-container-col so they will unite at the top.如果您想将右边缘的第 2 项和第 3 项顶部放在一起,您可以删除 flex-container-col 中的 justify-content,以便它们在顶部合并。 Hope works.希望有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.