我想让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/


当前回答

最简单的方法: .wrap { 显示:flex; } .full-width { 宽度:100%; } .centered { 显示:flex; justify-content:中心; } .btn { 显示:flex; justify-content:结束; } < div class = " wrap " > < div class = "宽屏" > < / div > <div class="full-width居中"> < div > < / div > div < div > < / B > < div > C < / div > < / div > <div class="full-width btn">D</div> < / div >

其他回答

最简单的方法

.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 >

使用display:grid方法,你可以简单地把所有的ul子元素放到同一个单元格中,然后设置justification -self:

.ul { 显示:网格; } .ul > * { grid-column-start: 1; grid-row-start: 1; justify-self:中心; } .ul > *:last child { justify-self:正确; } /* Make Fancy */ .li { 显示:inline-block; 保证金:1 px; 填充:5 px; 背景:# bbb; } < div class =“ul”> < span > < span类=‘李’> < / span > B <跨类=‘李’> < / span > < span类=‘李’> C > < /跨度 < / span > D <跨类=‘李’> < / span > < / div >

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

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

我扩展了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作为一个代码依赖的帮助

受到方法#5:@Michal Benjamin的解决方案的CSS网格布局的启发,因为我正在使用顺风,到目前为止,默认情况下仍然没有访问所有的网格选项。这似乎很有效:

ul { 显示:网格; Grid-template-columns: repeat(3, minmax(0, 1fr)); } 李{ align-self:中心; } 李:nth-child (1) { justify-content: flex-start;/*或margin-right:自动*/ } 李:nth-child (3) { justify-content: flex-end;/* OR margin-left:自动*/ } < ul > <李> < /李> <李> B < /李> <李> C李< / > < / ul >

PS:不确定把flex和grid混合起来是不是一个好主意!