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>
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.
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.
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".
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With