Making a flex item float right
To float an item to the right in flex container, use margin-left: auto on the item. This uses the flexbox's distribution of space to shove the item against the right edge:
Apply the .item-to-float-right class to the item:
Reshuffling items
If you wish to maintain the right alignment of an item, even while changing the order of other items in the flex container, we could use the order property:
The order value can be any digit greater than the highest order existing in the flex container.
Going responsive
For responsive layouts, sometimes, the right floated item may need to wrap to the new line while keeping itself to the {extreme} right. Combine flex-wrap: wrap at the container level with width: 100% on the item, to retain predictability:
Other routes to Rome
Spread 'em out
Set justify-content: space-between for equal spacing between items:
The push to right
Align all items to the right with justify-content: flex-end:
Things to avoid
- The
floatproperty doesn't affect flex items. Attemptingfloat: right;on a flex item inside a flex container won't have the desired effect. - Using
positionproperties on flex items could create conflicts with how flexbox operates. The layout implications of thepositionproperty may override the intended effects of using flexbox. - Using
marginon the left-side items to shove the last item to the right could backfire if the content of the left-side items changes unpredictably.
Was this article helpful?