Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align flexbox columns left and right?

Tags:

html

css

flexbox

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/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>
like image 313
archytect Avatar asked Mar 08 '15 01:03

archytect


People also ask

How do you align a flex item to the right?

In simple words, flex-items now expand from right to left as shown in the given figure. When justify-content is set to “flex-end”, it instantly shifts all the flex-items to the end of the flex-container along the main-axis, i.e flex items get right aligned.

How do you center a div vertically and horizontally with Flexbox?

To center a div vertically and horizontally using flexbox, you need to wrap the div or div's inside a container with properties ' display: flex; flex-direction: column; justify-content: center;align-items: center; ', then just make the div ' text-align: center; ' if it has text.

How do I align my child to the right flex?

To align one flex child to the right set it with margin-left: auto; From the flex spec: One use of auto margins in the main axis is to separate flex items into distinct "groups".


1 Answers

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>
like image 158
Josh Crozier Avatar answered Sep 23 '22 00:09

Josh Crozier