我想让A、B和C在中间对齐。

怎样才能让D完全向右移动呢?

之前:

后:

ul { 填充:0; 保证金:0; 显示:flex; flex-direction:行; justify-content:中心; 对齐项目:中心; } 李{ 显示:flex; 保证金:1 px; 填充:5 px; 背景:# aaa级; } 李:胎{ 背景:# ddd; /*魔术扔到右边*/ } < ul > <李> < /李> <李> B < /李> <李> C < /李> <李> D < /李> < / ul >

https://jsfiddle.net/z44p7bsx/


当前回答

如果您想使它对齐,您可以简单地附加一个空span并将三个子span分割为它们。

其他回答

我扩展了Michael_B的答案

.center-flex__2-of-3 > :nth-child(1), .center-flex__2-of-3 > :nth-child(3) { flex: 1; } .center-flex__2-of-3 > :nth-child(1) { justify-content: flex-start; } .center-flex__2-of-3 > :nth-child(3) { justify-content: flex-end; } .center-flex__1-of-2 > :nth-child(1) { margin: auto; } .center-flex__1-of-2 > :nth-child(2) { flex: 1; justify-content: flex-end; } .center-flex__2-of-2 > :nth-child(1) { flex: 1; justify-content: flex-start; } .center-flex__2-of-2 > :nth-child(2) { margin: auto; } .center-flex__1-of-2:before, .center-flex__1-of-1:before { content: ''; flex: 1; } .center-flex__1-of-1:after, .center-flex__2-of-2:after { content: ''; flex: 1; } [class*=center-flex] { display: flex; list-style: none; margin: 0; padding: 0; border: 10px solid rgba(0, 0, 0, 0.1); } [class*=center-flex] > * { display: flex; } li { padding: 3px 5px; } 2 of 3 <ul class="center-flex__2-of-3"> <span> <li>Accusamus</li> <li>Porro</li> </span> <span> <li>Center</li> <li>this</li> </span> <span> <li>Accusamus</li> <li>Porro</li> <li>Culpa</li> <li>Sit</li> </span> </ul> <br><br> 1 of 2 <ul class="akex center-flex__1-of-2"> <span> <li>Center</li> <li>this</li> </span> <span> <li>Accusamus</li> <li>Porro</li> <li>Culpa</li> <li>Sit</li> </span> </ul> <br><br> 2 of 2 <ul class="akex center-flex__2-of-2"> <span> <li>Accusamus</li> <li>Porro</li> <li>Culpa</li> <li>Sit</li> </span> <span> <li>Center</li> <li>this</li> </span> </ul> <br><br> 1 of 1 <ul class="center-flex__1-of-1"> <span> <li>Center</li> <li>this</li> </span> </ul>

这里有SASS作为一个代码依赖的帮助

接受的答案可以改变一点,因为你可以使用网格模板区域,没有假元素

grid-template-areas '. b c'
grid-template-columns: 1fr 1fr 1fr

非常清楚的问题。经过几个小时的挖掘,我忍不住把答案贴了出来。我们可以用表格,表格单元格,绝对位置,转换来解决这个问题,但我们必须用flexbox来做:)

.parent {
  display: flex;
  justify-content: flex-end;
}

.center {
  margin: auto;
}

http://codepen.io/rgfx/pen/BLorgd

最简单的方法

.box { 显示:flex; justify-content:中心; } .item1 { flex: 1; 显示:flex; justify-content:中心; 变换:translateX(10px);/*D元素宽度[如果需要]*/ } < div class = "盒子" > < div class = " item1”> < div > < / div > div < div > < / B > < div > C < / div > < / div > < div class = "第二条“> D < / div > < / div >

如果您想使它对齐,您可以简单地附加一个空span并将三个子span分割为它们。