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

Mahima Gangavarapu
Mahima Gangavarapu
15,490 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
168,137 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.

Mahima Gangavarapu
Mahima Gangavarapu
15,490 Points

Could you explain what :after and :before mean?

Steven Parker
Steven Parker
168,137 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.

Mahima Gangavarapu
Mahima Gangavarapu
15,490 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
168,137 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.

Mahima Gangavarapu
Mahima Gangavarapu
15,490 Points

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