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 Solution

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

clearfix and clear property

Hello! please help me: when I add the class clearfix to the copyright information, the black border expands and covers the logo, li items too even though it is not its container. Also, when I apply clear: both property to the copyright class, it no longer expands to cover the list or logo. Why?

<!DOCTYPE html>
<html>
<head>
    <title>Getting Started with CSS Layout</title>
    <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="page.css">
    <link rel="stylesheet" href="style.css">
</head>
    <body>
    <div class="container">
        <footer class="clearfix">
            <img class="logo" src="city-logo.svg" alt="logo">
            <ul class="footer-nav">
                <li><a href="#">Ice cream</a></li>
                <li><a href="#">Donuts</a></li>
                <li><a href="#">Tea</a></li>
                <li><a href="#">Coffee</a></li>
            </ul>
            <p class="copyright clearfix">&copy;2015 The Best City.</p>
        </footer>
    </div>
    </body>
</html>
.footer-nav,
.footer-nav li{
  float: left;
}
.logo {
float: right;}

.copyright {
border: 10px solid black;
clear: both;}

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

1 Answer

Steven Parker
Steven Parker
231,236 Points

Adding the "clearfix" class to the paragraph clears after the paragraph, so it extends it to include the floated items that wrap around it. But giving it a "clear" property causes the clear to occur before the paragraph, which places it below the floated items.

As a fix, I suggest wrapping the floated items in their own div element and adding the "clearfix" class to the new div instead of the footer.

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

Could you explain what :after and :before mean?

Steven Parker
Steven Parker
231,236 Points

The "::after" pseudo-element (with two colons) means "treat these properties as if they were on a new element that is the last child of the selected one". And "::before" does the same, but if it was the first child.

For more information, see the MDN pages for ::after and ::before.

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

Oh, now I understand what ::after and ::before mean, but the clear: both in .clearfix just clears the left and right sides of the pseudo element right? So why does the box expand?

Steven Parker
Steven Parker
231,236 Points

The pseudo-element is inside the element with the "clearfix" class, so that element must expand to include the floated items so that the pseudo-element is below them.

G H Mahimaanvita
G H Mahimaanvita
16,234 Points

I think I get it now! Thanks for helping me!