I am trying to achieve the equivalent of floats with Flexbox.
I'm succeeding if there is only one element pushed to the right, but failing when there are multiple elements that need to be floated right, unless worked around with the general sibling selector (~
).
Attempt #1:
Failing as the red block should be on the right
.block {
display: flex;
margin: -5px;
}
.block__element {
background: #ddd;
padding: 5px;
margin: 0 5px;
}
.block__element--first {
order: -1;
}
.block__element--right {
margin-left: auto;
}
.u-error {
background: #ce0e0e;
color: #fff;
}
<div class="block">
<div class="block__element">random elem</div>
<div class="block__element block__element--right u-error">right elem</div>
<div class="block__element block__element--first">first elem</div>
<div class="block__element block__element--right">right elem</div>
</div>
Attempt #2:
Uses the general sibling selector (~
) so relies on markup order, will not work if the order is modified using order
.
.block {
display: flex;
margin: -5px;
}
.block__element {
background: #ddd;
padding: 5px;
margin: 0 5px;
}
.block__element--first {
order: -1;
}
.block__element--right {
margin-left: auto;
}
.u-error {
background: #ce0e0e;
color: #fff;
}
<div class="block">
<div class="block__element">random elem</div>
<div class="block__element block__element--right u-error">right elem</div>
<div class="block__element block__element--first">first elem</div>
<div class="block__element block__element--right">right elem</div>
</div>