What is a clearfix?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
A clearfix is a method used in CSS (Cascading Style Sheets) to handle the issue of container collapse when the children elements inside the container use floating styles (float: left; or float: right;). This method ensures that the container element retains its height and does not collapse, thus containing floated elements properly.
Understanding Float Property and Parent Collapse
In CSS, when you apply a float style to an element, it is taken out of the normal flow of the document and placed along the left or right side of its container. Elements after the floated element will flow around it. However, a parent container will not automatically expand to contain the floated elements, leading to what is referred to as "parent collapse."
Here's a typical scenario:
Implementing the Clearfix
The clearfix hack involves adding a virtual element inside the floating container with a clear: both; property. This can be done traditionally by adding a new element with clearing styles, or using the pseudo-element :after.
Traditional Method
Using Pseudo-elements
A more elegant approach is using the :after pseudo-element:
The Role of display: table and Other Alternatives
In some cases, setting the container to display: table; or utilizing properties like overflow: hidden; might also clear floats, but they come with different implications:
display: table;: This makes the container behave like a table, which can disrupt nested layouts, especially in complex designs.overflow: hidden;: This clips any content overflowing the container bounds, which might not be desirable.
Table of Key Points
Here's a summary table outlining clearfix and its alternatives:
| Method | Code Example | Pros | Cons |
| Clearfix | content: ""; clear: both; | Reliable, no hidden elements | Extra CSS |
display: table; | display: table; | Simple | Might disrupt layouts |
overflow: hidden; | overflow: hidden; | Clutters content | Clips overflowed content |
Conclusion
Using a clearfix is an essential technique in web design for ensuring that float-based layouts perform as expected. This CSS trick maintains the integrity and appearance of a website, making visual design feasible and functional even with complex, floating elements. As new CSS features like Flexbox and Grid gain wider support, reliance on float might decrease, but understanding clearfix remains a valuable skill for today’s developers.
Related reading
- What is a good approach to develop a synchronous/blocking and an asynchrounous/non-blocking library-api in parallel? JavaScript
- What is a SIMPLE implementation of async.series?
- What is 'Currying'?
- What is event bubbling and capturing?
- What is export default in JavaScript?
- What is HTML5 File.slice method actually doing?
- What is JavaScript's highest integer value that a number can go to without losing precision?
- What is lexical scope?
.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.