Masatoshi Nishiguchi
Masatoshi Nishiguchi's Blog

Masatoshi Nishiguchi's Blog

Float right using flex-grow spacer

Masatoshi Nishiguchi's photo
Masatoshi Nishiguchi
·May 7, 2020·

2 min read

Every now and again, there is a situation where I want to make an HTML element float to the right. I could use CSS float property; however its resulting behavior is sometimes counterintuitive and it requires something called "Clearfix hack".

As an alternative, I found it much more intuitive to use CSS Flexbox and a spacer element. All I need to do is:

  • wrap all the items in a wrapper element that has CSS display: flex
  • insert to the list a spacer element that has CSS flex-grow: 1

then that spacer will stretch out and push the subsequent elements to the right.

That's it.

 
Share this