[英]How to align flexbox columns left and right?
With typical CSS I could float one of two columns left and another right with some gutter space in-between. 使用典型的CSS,我可以将两个列中的一个向左浮动,另一个向右浮动,其间有一些沟槽空间。 How would I do that with flexbox?
我如何使用flexbox做到这一点?
http://jsfiddle.net/1sp9jd32/ http://jsfiddle.net/1sp9jd32/
#container { width: 500px; border: solid 1px #000; display: -webkit-flex; display: -ms-flexbox; display: flex; } #a { width: 20%; border: solid 1px #000; } #b { width: 20%; border: solid 1px #000; height: 200px; }
<div id="container"> <div id="a"> a </div> <div id="b"> b </div> </div>
You could add justify-content: space-between
to the parent element. 您可以将
justify-content: space-between
到父元素。 In doing so, the children flexbox items will be aligned to opposite sides with space between them. 在这样做时,子弹性工具箱将与相对侧对齐,并且它们之间具有空间。
#container {
width: 500px;
border: solid 1px #000;
display: flex;
justify-content: space-between;
}
#container { width: 500px; border: solid 1px #000; display: flex; justify-content: space-between; } #a { width: 20%; border: solid 1px #000; } #b { width: 20%; border: solid 1px #000; height: 200px; }
<div id="container"> <div id="a"> a </div> <div id="b"> b </div> </div>
You could also add margin-left: auto
to the second element in order to align it to the right. 您还可以将
margin-left: auto
添加到第二个元素,以便将其与右侧对齐。
#b {
width: 20%;
border: solid 1px #000;
height: 200px;
margin-left: auto;
}
#container { width: 500px; border: solid 1px #000; display: flex; } #a { width: 20%; border: solid 1px #000; margin-right: auto; } #b { width: 20%; border: solid 1px #000; height: 200px; margin-left: auto; }
<div id="container"> <div id="a"> a </div> <div id="b"> b </div> </div>
There are different ways but simplest would be to use the space-between see the example at the end 有不同的方法,但最简单的方法是使用空格 - 看到最后的例子
#container {
border: solid 1px #000;
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 10px;
height: 50px;
}
.item {
width: 20%;
border: solid 1px #000;
text-align: center;
}
Another option is to add another tag with flex: auto
style in between your tags that you want to fill in the remaining space. 另一个选项是在要填充剩余空间的标记之间添加另一个带有
flex: auto
样式的标记。
https://jsfiddle.net/tsey5qu4/ https://jsfiddle.net/tsey5qu4/
The HTML: HTML:
<div class="parent">
<div class="left">Left</div>
<div class="fill-remaining-space"></div>
<div class="right">Right</div>
</div>
The CSS: CSS:
.fill-remaining-space {
flex: auto;
}
This is equivalent to flex: 1 1 auto, which absorbs any extra space along the main axis. 这相当于flex:1 1 auto,它沿主轴吸收任何额外的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.