是否有一种比使用绝对位置更灵活的方式来将“Contact”向右对齐?

.main { display: flex; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { flex: 1; text-align: center; } .c { position: absolute; right: 0; } <h2>With title</h2> <div class="main"> <div class="a"><a href="#">Home</a></div> <div class="b"><a href="#">Some title centered</a></div> <div class="c"><a href="#">Contact</a></div> </div> <h2>Without title</h2> <div class="main"> <div class="a"><a href="#">Home</a></div> <!--<div class="b"><a href="#">Some title centered</a></div>--> <div class="c"><a href="#">Contact</a></div> </div>

http://jsfiddle.net/vqDK9/


当前回答

给你。在flex容器上设置justify-content: space-between。

.main { display: flex; justify-content: space-between; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { text-align: center; } <h2>With title</h2> <div class="main"> <div class="a"><a href="#">Home</a></div> <div class="b"><a href="#">Some title centered</a></div> <div class="c"><a href="#">Contact</a></div> </div> <h2>Without title</h2> <div class="main"> <div class="a"><a href="#">Home</a></div> <!-- <div class="b"><a href="#">Some title centered</a></div> --> <div class="c"><a href="#">Contact</a></div> </div>

其他回答

如果你想使用flexbox,你应该能够,通过这样做(在容器上display: flex,在项目上flex: 1,在.c上text-align: right):

.main { display: flex; }
.a, .b, .c {
    background: #efefef;
    border: 1px solid #999;
    flex: 1;
}
.b { text-align: center; }
.c { text-align: right; }

...或者(甚至更简单),如果项目不需要满足,你可以在容器上使用justify-content: space-between,并完全删除文本对齐规则:

.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }

下面是Codepen上的一个演示,允许您快速尝试上述操作。

很容易

.main {
    display: flex;
    flex-direction:row-reverse;
}

左边:自动工作良好。但干净的柔性盒解决方案将在主类之间的空间。如果有两个或两个以上的元素,那么两者之间的间隔就很好。我也为单个元素添加了一个解决方案。

.main { display: flex; justify-content: space-between; } .a, .b, .c { background: #efefef; border: 1px solid #999; padding: 0.25rem; margin: 0.25rem;} .b { flex: 1; text-align: center; } .c-wrapper { display: flex; flex: 1; justify-content: flex-end; } .c-wrapper2 { display: flex; flex: 1; flex-flow: row-reverse; } <div class="main"> <div class="a"><a href="#">Home</a></div> <div class="b"><a href="#">Some title centered</a></div> <div class="c"><a href="#">Contact</a></div> </div> <div class="main"> <div class="a"><a href="#">Home</a></div> <div class="c"><a href="#">Contact</a></div> </div> <div class="main"> <div class="c-wrapper"> <a class="c" href="#">Contact</a> <a class="c" href="#">Contact2</a> </div> </div> <div class="main"> <div class="c-wrapper2"> <span class="c">Contact</span> <span class="c">Contact2</span> </div> </div>

基于TetraDev回答的示例代码

右图: * { 轮廓:0.4 px红色虚线; } .main { 显示:flex; flex-direction:行; 对齐项目:中心; } h1 { flex-basis: 100%; } img { 边距:0 5px; 高度:30 px; } < div class = "主" > <标题>安全支付h1 > < / < img src = " https://i.stack.imgur.com/i65gn.png " > < img src = " https://i.stack.imgur.com/i65gn.png " > < / div >

左图:

* { 轮廓:0.4 px红色虚线; } .main { 显示:flex; flex-direction:行; 对齐项目:中心; } h1 { flex-basis: 100%; text-align:正确; } img { 边距:0 5px; 高度:30 px; } < div class = "主" > < img src = " https://i.stack.imgur.com/i65gn.png " > < img src = " https://i.stack.imgur.com/i65gn.png " > <标题>安全支付h1 > < / < / div >

您也可以使用填充物来填充剩余的空间。

<div class="main">
    <div class="a"><a href="#">Home</a></div>
    <div class="b"><a href="#">Some title centered</a></div>
    <div class="filler"></div>
    <div class="c"><a href="#">Contact</a></div>
</div>

.filler{
    flex-grow: 1;
}

我已经更新了3个不同版本的解决方案。这是因为讨论了使用额外填充元素的有效性。如果您运行剪切的代码,您会看到所有解决方案都做不同的事情。例如,在项目b上设置填充类将使该项填充剩余的空间。这样做的好处是没有不可点击的“死”空间。

<div class="mainfiller"> <div class="a"><a href="#">Home</a></div> <div class="b"><a href="#">Some title centered</a></div> <div class="filler"></div> <div class="c"><a href="#">Contact</a></div> </div> <div class="mainfiller"> <div class="a"><a href="#">Home</a></div> <div class="filler b"><a href="#">Some title centered</a></div> <div class="c"><a href="#">Contact</a></div> </div> <div class="main"> <div class="a"><a href="#">Home</a></div> <div class="b"><a href="#">Some title centered</a></div> <div class="c"><a href="#">Contact</a></div> </div> <style> .main { display: flex; justify-content: space-between; } .mainfiller{display: flex;} .filler{flex-grow:1; text-align:center} .a, .b, .c { background: yellow; border: 1px solid #999; } </style>