alignment - CSS align one item right with flexbox -
https://jsfiddle.net/vhem8scs/
is possible have 2 items align left , 1 item align right flexbox? link shows more clearly. last example want achieve.
in flexbox have 1 block of code. float have 4 blocks of code. 1 reason why prefer flexbox.
html
<div class="wrap"> <div>one</div> <div>two</div> <div>three</div> </div> <!-- desired result --> <div class="result"> <div>one</div> <div>two</div> <div>three</div> </div>
css
.wrap { display: flex; background: #ccc; width: 100%; justify-content: space-between; } .result { background: #ccc; margin-top: 20px; } .result:after { content: ''; display: table; clear: both; } .result div { float: left; } .result div:last-child { float: right; }
to align 1 flex child right set withmargin-left: auto;
from flex spec:
one use of auto margins in main axis separate flex items distinct "groups". following example shows how use reproduce common ui pattern - single bar of actions aligned on left , others aligned on right.
.wrap div:last-child { margin-left: auto; }
updated fiddle
.wrap { display: flex; background: #ccc; width: 100%; justify-content: space-between; } .wrap div:last-child { margin-left: auto; } .result { background: #ccc; margin-top: 20px; } .result:after { content: ''; display: table; clear: both; } .result div { float: left; } .result div:last-child { float: right; }
<div class="wrap"> <div>one</div> <div>two</div> <div>three</div> </div> <!-- desired result --> <div class="result"> <div>one</div> <div>two</div> <div>three</div> </div>
note:
you achieve similar effect setting flex-grow:1 on middle flex item (or shorthand flex:1
) push last item way right. (demo)
the obvious difference middle item becomes bigger may need be. add border flex items see difference.
demo
.wrap { display: flex; background: #ccc; width: 100%; justify-content: space-between; } .wrap div { border: 3px solid tomato; } .margin div:last-child { margin-left: auto; } .grow div:nth-child(2) { flex: 1; } .result { background: #ccc; margin-top: 20px; } .result:after { content: ''; display: table; clear: both; } .result div { float: left; } .result div:last-child { float: right; }
<div class="wrap margin"> <div>one</div> <div>two</div> <div>three</div> </div> <div class="wrap grow"> <div>one</div> <div>two</div> <div>three</div> </div> <!-- desired result --> <div class="result"> <div>one</div> <div>two</div> <div>three</div> </div>
Comments
Post a Comment