In my opinion, it's a terrible idea, for the following reasons:
- Clearfix is a hack. It was introduced as a workaround for
another hack — using floats as a main block layout tool, which they
were never meant to be (as well as tables!). When float are used for
what they were meant to (incut illustrations, tables etc. in long
texts) the fact they cross the boundaries of blocks (in both
directions, from outside to inside and vice versa) is a feature, not
bug. Clearing everything inselectively would lead to ugly holes in
the document, text would not nicely flow around floats anymore.
- Clearfixes add pseudo elements to the blocks. These pseudo
elements have zero size and are invisible if the hack is used for
what it was invented (to make the block contain nested floats and,
optionally, margins of its children). But it is not definitely the
case in other situations. For example, if you turn such block into
Flexbox container and set it
justify-content:space-between
, you
would get unwanted space at the end of the container.
It's because this pseudo element would turn into flex element and
it would follow Flexbox placement rules, affecting the
position of other elements. Same for Grid layout.
- Clearfix hacks based on the
clear
property are not ideal
even for for containing floats! The 'clearfixed' blocks stay in the same block formatting context (BFC) as their neighbourhood, so they may be affected by other floats in the document (e.g. a floated previous column). The other approach for containing floats, based on creating the new BFC (mostly used in
form of overflow:hidden
, but there are other alternatives, each
with its own strengths and downsides) is in general much more
reliable. Please check out this Codepen demo that compares the
effect of different solutions to containing floats problem:
https://codepen.io/SelenIT/details/qrORXm/.
Personally, I'd recommend not using floats for layout at all, so no 'clearfixing' would be needed for anything. Flexbox has much more layout possibilities than floats ever had, and browser support for it became extremely good now. Use clearfixes as a last resort, if no other way to solve the specific layout problem can help you.