CSS
Web Development
Clearfix
HTML
Coding Techniques

What is a clearfix?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

html
1<div class="container">
2    <div class="floated-left">This div is floated to the left.</div>
3    <div class="floated-right">This div is floated to the right.</div>
4</div>
css
1.floated-left {
2    float: left;
3    width: 50%;
4}
5
6.floated-right {
7    float: right;
8    width: 50%;
9}
10
11.container {
12    border: 1px solid blue; /* This border will not wrap the floated elements without clearfix */
13}

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

html
1<div class="container">
2    <div class="floated-left"></div>
3    <div class="floated-right"></div>
4    <div style="clear: both;"></div>
5</div>

Using Pseudo-elements

A more elegant approach is using the :after pseudo-element:

css
1.container::after {
2    content: "";
3    display: block;
4    clear: both;
5}

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:

MethodCode ExampleProsCons
Clearfixcontent: ""; clear: both;Reliable, no hidden elementsExtra CSS
display: table;display: table;SimpleMight disrupt layouts
overflow: hidden;overflow: hidden;Clutters contentClips 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.