What methods of ‘clearfix’ can I use?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Clearfix is the group of CSS techniques used to make a parent element contain floated children instead of collapsing around them. It matters mostly in float-based legacy layouts. In modern CSS, the cleanest answer is often not another clearfix hack at all, but using a newer layout mode or flow-root.
Why clearfix exists
Floats are taken out of normal block flow. That means a parent containing only floated children may behave as though it has no height.
Without containment, the border on .gallery may collapse because the floated children do not contribute to the parent's normal height.
The classic pseudo-element clearfix
The most common historical solution is a generated pseudo-element that clears the floats:
Then apply it to the parent:
This works well because the generated box participates in normal flow and clears the floated elements above it. For broad browser compatibility in legacy code, this is still the standard utility-class answer.
The extra markup approach
An older method is to insert a clearing element in the HTML:
This works, but it mixes layout repair markup into the document structure, which is why pseudo-element clearfix became more popular. It solves the same problem with cleaner HTML.
The overflow technique
Another historical trick is to create a new block formatting context with overflow:
or:
This often contains floats successfully, but it has side effects. Scrollbars, clipped shadows, clipped dropdowns, or clipped absolutely positioned children can appear. So overflow is not a universal clearfix. It is a containment technique with tradeoffs.
The modern CSS answer is often flow-root
If you still have float-based markup but want a clean containment rule, display: flow-root is usually the best modern option:
flow-root creates a new block formatting context intentionally, without using overflow as a side effect. In many modern browsers, this is the most straightforward float-containment rule you can write.
Sometimes the best clearfix is to stop using floats
A lot of historical clearfix problems disappear when the layout is rewritten with Flexbox or Grid.
This is not technically a clearfix method. It is a layout change. But for new UI work, that is often the best answer because the need for clearfix came from using floats for layout in the first place.
So the practical rule is:
- keep clearfix or
flow-rootwhen maintaining float-based legacy code - prefer Flexbox or Grid for new layouts
Choosing the right method
Use the pseudo-element clearfix when you need a classic, compatible utility class. Use overflow only when its clipping or scrolling behavior is acceptable. Use flow-root when you want clean containment in modern CSS. Use Flexbox or Grid when you are free to change the layout model.
That choice is less about memorizing hacks and more about understanding why the parent is collapsing in the first place.
Common Pitfalls
The most common mistake is using overflow: hidden as a generic clearfix and then accidentally clipping content such as shadows, menus, or absolutely positioned children.
Another common issue is adding clearfix classes to containers that do not even contain floats. That adds noise without fixing anything.
People also treat Flexbox and Grid as just another clearfix trick, when really they are layout systems that remove the need for float-based hacks altogether.
Finally, when debugging a collapsed parent, verify that floats are actually the cause. A height or positioning bug is not always a clearfix problem.
Summary
- Clearfix exists to make a parent contain floated children.
- The classic method is a pseudo-element with
clear: both. - '
overflowcan work, but it has side effects.' - '
display: flow-rootis usually the cleanest modern float-containment rule.' - For new layouts, Flexbox or Grid is often better than any clearfix technique.
Related reading
- What q.defer really does?
- What so different about Node.js's event-driven? Can't we do that in ASP.Net's HttpAsyncHandler?
- What's the difference between an object initializer and a constructor?
- What’s the difference between Array() and [] while declaring a JavaScript array?
- What''s the difference between <b> and <strong>, <i> and <em>?
- What's the difference between event.stopPropagation and event.preventDefault?
- What's the equivalent of Java's Thread.sleep in JavaScript?
- What's the right way for handling Async functions for loading a .gif image?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.