How to align flexbox columns left and right?

前端 未结 4 1098
天涯浪人
天涯浪人 2020-11-28 03:06

With typical CSS I could float one of two columns left and another right with some gutter space in-between. How would I do that with flexbox?

http://jsfiddle.net/1sp

相关标签:
4条回答
  • 2020-11-28 03:36

    I came up with 4 methods to achieve the results. Here is demo

    Method 1:

    #a {
        margin-right: auto;
    }
    

    Method 2:

    #a {
        flex-grow: 1;
    }
    

    Method 3:

    #b {
        margin-left: auto;
    }
    

    Method 4:

    #container {
        justify-content: space-between;
    }
    
    0 讨论(0)
  • 2020-11-28 03:38

    You could add justify-content: space-between to the parent element. 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.

    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>

    0 讨论(0)
  • 2020-11-28 03:39

    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

    0 讨论(0)
  • 2020-11-28 03:46

    Another option is to add another tag with flex: auto style in between your tags that you want to fill in the remaining space.

    https://jsfiddle.net/tsey5qu4/

    The HTML:

    <div class="parent">
      <div class="left">Left</div>
      <div class="fill-remaining-space"></div>
      <div class="right">Right</div>
    </div>
    

    The CSS:

    .fill-remaining-space {
      flex: auto;
    }
    

    This is equivalent to flex: 1 1 auto, which absorbs any extra space along the main axis.

    0 讨论(0)
提交回复
热议问题