我想让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/
我扩展了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混合起来是不是一个好主意!