简体   繁体   English

将多个弹性项对齐,就像它们向右浮动一样

[英]Align multiple flex items as if they were floated right

I am trying to achieve the equivalent of floats with Flexbox. 我试图用Flexbox实现相当于浮动。

I'm succeeding if there is only one element pushed to the right, but failing when there are multiple elements that need to be floated right, unless worked around with the general sibling selector ( ~ ). 如果只有一个元素被推到右边,我会成功,但是当有多个元素需要向右浮动时失败,除非使用通用兄弟选择器( ~ )。

Attempt #1: 尝试#1:

Failing as the red block should be on the right 因为红色区块应该在右侧失败

 .block { display: flex; margin: -5px; } .block__element { background: #ddd; padding: 5px; margin: 0 5px; } .block__element--first { order: -1; } .block__element--right { margin-left: auto; } .u-error { background: #ce0e0e; color: #fff; } 
 <div class="block"> <div class="block__element">random elem</div> <div class="block__element block__element--right u-error">right elem</div> <div class="block__element block__element--first">first elem</div> <div class="block__element block__element--right">right elem</div> </div> 

Attempt #2: 尝试#2:

Uses the general sibling selector ( ~ ) so relies on markup order, will not work if the order is modified using order . 使用通用兄弟选择器( ~ )因此依赖于标记顺序,如果使用order修改订单则不起作用。

 .block { display: flex; margin: -5px; } .block__element { background: #ddd; padding: 5px; margin: 0 5px; } .block__element--first { order: -1; } .block__element--right { margin-left: auto; } .u-error { background: #ce0e0e; color: #fff; } 
 <div class="block"> <div class="block__element">random elem</div> <div class="block__element block__element--right u-error">right elem</div> <div class="block__element block__element--first">first elem</div> <div class="block__element block__element--right">right elem</div> </div> 

I think the key is not to use margin-left: auto on the items you want positioned to the right. 我认为关键是不要在你想要的项目上使用margin-left: auto As you can see, that spaces them away from each other. 正如您所看到的那样,它们将它们彼此隔开。

Instead, use margin-right: auto on the right-most item of the left section. 相反,在左侧部分的最右侧项目上使用margin-right: auto That will push everything else to the right. 这将把一切推向正确的方向。

 .block > div:first-child { margin-right: auto; } /* The actual left-section right-most item has order:-1, so the selector above ends up targeting the proper item. */ .block { display: flex; margin: -5px; } .block__element { background: #ddd; padding: 5px; margin: 0 5px; } .block__element--first { order: -1; } .block__element--right {} .u-error { background: #ce0e0e; color: #fff; } 
 <div class="block"> <div class="block__element">random elem</div> <div class="block__element block__element--right u-error">right elem</div> <div class="block__element block__element--first">first elem</div> <div class="block__element block__element--right">right elem</div> </div> 

Another option to consider would be to use a pseudo-element that inserts an invisible flex item between the left and right sections, giving this item flex: 1 . 另一个需要考虑的选择是使用一个伪元素,在左右两个部分之间插入一个不可见的flex项,给这个项目flex: 1

 .block::after { content: "spacer element"; background: #ddd; padding: 5px; margin: 0 5px; flex: 1; visibility: hidden; /* disable this rule to see the item at work */ } .block { display: flex; margin: -5px; } .block__element { background: #ddd; padding: 5px; margin: 0 5px; } .block__element--first { order: -1; } .block__element--right { order: 2; } .u-error { background: #ce0e0e; color: #fff; } 
 <div class="block"> <div class="block__element">random elem</div> <div class="block__element block__element--right u-error">right elem</div> <div class="block__element block__element--first">first elem</div> <div class="block__element block__element--right">right elem</div> </div> 

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

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