简体   繁体   English

将 flex 项目向右对齐

[英]Aligning flex items to the right

I have a container and inside there are 3 boxes: yellow, green and red.我有一个容器,里面有 3 个盒子:黄色、绿色和红色。

I gave display:flex to the container and gave justify-content:flex-start for the items to start at beginning.我给了display:flex容器并给了justify-content:flex-start让项目从头开始。

I wanted to move the red box to the end so I gave margin-right: auto to the yellow box so that the red box could move to end (not sure if this is the exact way to move the red box to end, if not i want help in that too).我想将红框移到最后,所以我给了margin-right: auto给黄框,这样红框就可以移到最后(不确定这是否是将红框移到最后的确切方法,如果不是我也需要这方面的帮助)。

So the question is: I want the green box in the vertical middle of the container and I want to move it to the extreme right like the red box (but should be in the middle of container)所以问题是:我想要容器垂直中间的绿色框,我想像红色框一样将它移动到最右边(但应该在容器中间)

How do I do it with flex box?我如何使用 flex box 来做到这一点?

 .container { height: 500px; width: 500px; background-color: royalblue; display: flex; flex-direction: row; justify-content: flex-start; } .yellowbox { color: black; height: 100px; width: 100px; background-color: yellow; margin-right: auto; } .greenbox { color: black; height: 100px; width: 100px; background-color: green; align-self: center; margin-left: auto; } .redbox { color: black; height: 100px; width: 100px; background-color: red; }
 <div class="container"> <div class="yellowbox"> <p>the white text</p> </div> <div class="greenbox"> <p>the black text</p> </div> <div class="redbox"> <p>the red text</p> </div> </div>

THIS IS MY CODEPEN LINK: http://codepen.io/ShamZ/pen/pRLELP这是我的 CODEPEN 链接: http ://codepen.io/ShamZ/pen/pRLELP

you may increase some of the margin s but you must allow the flex children to wrap .您可以增加一些margin但您必须允许flex子项wrap and use order to put the green box in last position并使用order将绿色框放在最后一个位置

 .container { height: 500px; width: 500px; background-image:linear-gradient(to left,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%),linear-gradient(to top,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%); background-color: royalblue; display: flex; flex-direction: row; flex-wrap:wrap; justify-content:space-between; } .yellowbox { color: black; height: 100px; width: 100px; background-color: yellow; margin-right: 50%; } .greenbox { order:1; color: black; height: 100px; width: 100px; background-color: green; margin: -100px 0 auto auto; } .redbox { margin:0 0 0 auto; color: black; height: 100px; width: 100px; background-color: red; }
 <div class="container"> <div class="yellowbox"> <p>the white text</p> </div> <div class="greenbox"> <p>the black text</p> </div> <div class="redbox"> <p>the red text</p> </div> </div>

http://codepen.io/gc-nomade/pen/Qdmpbb http://codepen.io/gc-nomade/pen/Qdmpbb

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

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