简体   繁体   English

如何左右对齐flexbox列?

[英]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. 在这样做时,子弹性工具箱将与相对侧对齐,并且它们之间具有空间。

Updated Example 更新的示例

#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添加到第二个元素,以便将其与右侧对齐。

Updated Example 更新的示例

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

I came up with 4 methods to achieve the results. 我想出了4种方法来实现结果。 Here is demo 这是演示

Method 1: 方法1:

#a {
    margin-right: auto;
}

Method 2: 方法2:

#a {
    flex-grow: 1;
}

Method 3: 方法3:

#b {
    margin-left: auto;
}

Method 4: 方法4:

#container {
    justify-content: space-between;
}

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;
}

see the example 看到这个例子

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM