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
Ben Os
20,008 Pointsclear: both - Only for the previous element?
I've learned here in Treehouse and read on several other places that when if we float an element, we might need to clear float from the element before it.
I am not sure if "before it" always relates to the parent div (if there is one), or just to the one which is one stage up in the DOM.
One programmer advised me to also clear floats from those who come after the div... I.E --- One stage down in the DOM... Is this also necessary? Why would this action ever be necessary? None of the videos I have seen talked about this.
3 Answers
Bryan Summers
11,864 PointsI agree with Steven. It's a perfect strategy for not having to worry about clearing floats.
"before it" does indeed refer to the parent element. The clear property will only affect float elements "contained" within the parent element, not on subsequent elements in the DOM.
As for your second question, you should disregard that advice. Just apply the clearfix class to the parent container element and all floats within that element will be cleared.
Hope this helps!
Bryan Summers
11,864 PointsHi Ben,
Yes you are correct :)
Steven Parker
243,318 PointsTypically, you'll float things within a container. A common practice is to create a "clearfix" using the ::after pseudo-element. So, if your outer container had the class clearfix, you might have this in the CSS:
.clearfix::after {
content: "";
display: block;
clear: both;
}
If you do this consistently, you'll never have to worry about uncleared floats coming before something that is not also inside the same container.
Ben Os
20,008 PointsBen Os
20,008 Pointsif I understand correctly we apply the cleafix only to the parent-container of all the child-elements, and this will give us peace \ quiet of the mind about all the its child elements... Am I accurate?
Much thanks !!!