Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Layout Basics Page Layout with the Float Property The Float Challenge

Dilip Agheda
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dilip Agheda
Full Stack JavaScript Techdegree Graduate 28,581 Points

How does the clearFix actually works?

i have experimented with this and i know that it works. i don't understand how it works though!

  • what is meant by "clearing the floats". what is ::after, content:"" ? my understanding is that browser will insert content="" as block element after the element on which we put .clearfix.
  • but then how exactly, it becomes tall enough to accomodate the floats? how does it know the height of the floated element?

.clearfix::after { content: ""; clear: both; display: table; }

1 Answer

Alexey Kulikov
Alexey Kulikov
11,124 Points

Hi Dilip!

.clearfix::after means that we are adding pseudo element as the last child (NOT after) of container (the one with clearfix class).

content: "" means that this pseudo element has no content (like emty <div></div> for example). Try it with some content (i.e. content: "TEST CONTENT") and you will see this element on your page. You can also see it in Chrome developer tools.

display: table; simply sets the Display property for this pseudo element. For the purpuse of clearfix you can also set it to block. The point is to make this new element take the whole new line (display: inline or inline-block won't work for clearfix).

From MDN - "The clear CSS property specifies whether an element can be next to floating elements that precede it or must be moved down (cleared) below them". So setting clear: both for this pseudo element moves it below the floated elements of it's container. And since this pseudo element is the last child of container moving it below floated elements restores container's height.

Hope it helps!