Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Layout Techniques Float Layout Creating a Horizontal Menu

Chen Wang
Chen Wang
7,371 Points

About float and collapse

The video talks about float. It says when a floated tag is inside a tag without float, it will collapse.

In the code, we set the outer tag white background color.

Firstly, I don't understand why this happen. Are there any general rules for us when we use float? For example, if we set inside tag float, we must also set outer tag float?

Second, the video says add float to the outer tag "main-nav" can solve this problem. Indeed, it works. However, according to his explanation, if I remove the float for inner tag, it should also be fine, right? Actually, it is not. Please refer to my code below. The white space "main-nav" is still empty!

/* Page Styles
================================ */

* {
    box-sizing: border-box;
}
html,
body{
    height: 100%;
}
body {
    font: normal 1.1em/1.5 sans-serif;
    color: #222;
    background-color: #edeff0;
}



/* Layout Element Colors
================================ */

.main-header       { background-color: #384047; }
.main-logo a       { background-color: #5fcf80; }
.main-nav a       { background-color: #3f8abf; }
.primary-content   { background-color: #caebf6; }
.secondary-content { background-color: #bfe3d0; }
.main-footer       { background-color: #b7c0c7; }

/* Main Layout Styles
================================ */

.main-wrapper {
    width: 90%;
    margin: auto;
}
.main-header {
    min-height: 100px;
    padding: 15px;
}

.main-logo {
    float: left;
    margin: 0 50px 0 0;
}
.main-nav {
    margin: 10px 5px;
    background: white;
    padding: 5px;
}
.main-nav li{

    margin-right: 10px;
}


.main-logo a, 
.main-nav a {
    display: block;
    color: white;
    text-decoration: none;
    text-align: center;
    padding: 5px 15px;
    border-radius: 5px;

}

.main-nav {

    display: inline
}
.main-banner {
    background: #dfe2e4;
    text-align: center;
    padding: 35px 15px;
}
.main-footer {
    text-align: center;
    padding-top: 5px;
    padding-bottom: 5px;
}

/* Column Layout
================================ */

.col {
    padding: 20px;
}

/* Media Queries
================================ */

@media (max-width: 768px) {
    .main-wrapper,
    .main-nav li, 
    .main-logo {
        width: initial;
        height: initial;
    }
    .main-logo {
        margin-right: 0;
    }
    .extra-content {
        display: none;
    }
}
<!DOCTYPE html>
<html>
<head>
    <title>Floats</title>
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="main-wrapper">
        <header class="main-header">
            <h1 class="main-logo"><a href="#">Logo</a></h1>
            <ul class="main-nav">
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
            </ul>
        </header>
<!--        <div class="main-banner">
            <h1>This is the Main Banner Heading</h1>
            <p>Andouille pork chop pancetta drumstick ground round beef ribs swine brisket ham.</p>
        </div> -->
        <div class="content-row">
            <div class="primary-content col">
                <h2>Primary Content</h2>
                <p>Bacon ipsum dolor sit amet chicken pork ground round brisket corned beef ball tip shank tail salami filet mignon ham hock pork belly venison shankle. Pig kielbasa drumstick sausage pork chop boudin. Chicken t-bone salami pork chop, beef ribs kevin ham tri-tip beef venison biltong brisket.</p>
                <p>Venison strip steak meatball chicken, brisket prosciutto sirloin. Capicola drumstick brisket tri-tip salami. Chicken beef jerky, tail turkey prosciutto cow ham sirloin boudin tenderloin. Meatloaf tri-tip turducken brisket andouille, pork belly corned beef fatback hamburger.</p>
            </div>
            <div class="extra-content col">
                <h3>Extra Content</h3>
                <p>Strip steak tenderloin kevin swine meatloaf capicola, doner beef turducken pancetta corned beef pork loin shoulder.</p>
                <hr>
                <p>Pork filet mignon leberkas, tail swine venison pancetta turkey shoulder brisket chalkers likes hamburgers.</p>
            </div>
            <div class="secondary-content col">
                <h3>Secondary Content</h3>
                <p>Strip steak tenderloin kevin swine meatloaf capicola, doner beef turducken pancetta corned beef pork loin shoulder.</p>
                <hr>
                <p>Pork filet mignon leberkas, tail swine venison pancetta turkey shoulder brisket chalkers likes hamburgers.</p>
            </div>
        </div>
        <footer class="main-footer">
            <p>&copy;2014 Example Layout</p>
        </footer>
    </div>
</body>
</html>
Yosef VanTine
Yosef VanTine
8,880 Points

this is because floated elements effectively "pop" out of the page. so the parent elements now has no height. Check out this other treehouse video on floats and a several other links with more info. One is a fix commonly called "the clearfix hack" that should help you fix the problem:

Treehouse Video https://teamtreehouse.com/library/css-foundations/the-box-model/floats-2

Clearfix hack http://nicolasgallagher.com/micro-clearfix-hack/

MND CSS float property https://developer.mozilla.org/en-US/docs/Web/CSS/float

hope that helps