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 floating issues

Hi, Im building my first website on my own. I have gone through the beginner CSS and "How to build a website" courses but i'm stuck on ta particular issue regarding floats.

Im floating two elements one to the left and one to the right. However the the right floated element cant seem to clear the left and move up on the webpage. When i use chrome's dev tools to investigate, it appears the left element has a phantom margin on the right thats preventing the other element form moving up. I have adjusted the margins and container width etc. to no avail. I am brand new at this so I apologize for my ignorance

Here's the HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Lowcities</title>
    <link rel="stylesheet" href="css/style.css">
    <body>
    <header class="main-header">
    <h1 class="title">Lowcities</h1>
</header>

    <div class="group">
        <div class="navigation">
            <ul>
                <li>about</li>
                <li>music</li>
                <li>photos</li>
            </ul>
        </div> <!-- End .navigation -->


        <div class="blog">
                <h1>state of indifference</h1>
                <p>Theres not much happening right now im just trying to learn how to buld and design websites. you know...</p> 
        </div> <!-- End .blog -->
    </div>

</body>
<footer></footer>

HERE IS THE CSS:

img {
    max-width: 100%;
}


.main-header {
    margin: -20px;
    padding: 100px;
    height: 600px;
    max-width: 100%;
    background: #000 
                url('../img/100_0469.jpg') no-repeat center;
    background-size: 70%;



}
.title {
    color: red;
    padding: 100px 0 0 50px;


}

.primary {
    width: 30%;

}
.navigation {
    line-height: 10;
    letter-spacing: 1em;
    background-color: red;
    display: block;
    padding-top: 200px;
    width:30%;



}

.blog {
    background-color: green;
    text-align: center;
    width: 40%;

}

/*Floated columns*/////////////////////





.navigation {

    float: left;

}

.blog {

    float:right;
}

3 Answers

Matthew Underhill
Matthew Underhill
20,361 Points

Add the following to your .navigation class

          .navigation {
                    display: inline-block;
          }

That should achieve what you are trying to do. Also you have missed off the closing </head> tag in your HTML.

Do not use absolute positioning to fix this. The position attribute is used for positioning elements out of the normal document flow, which is not what you're trying to do here.

that did the trick! thanks!

Tracy Excell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tracy Excell
Front End Web Development Techdegree Graduate 15,333 Points

I also have trouble with floats, but found using absolute / relative positioning worked for me in a similar problem recently. This is the css code I used.

header-info {

position: relative;

}

.profile-image { position: absolute; right: 5em; height: 200px; width: 200px; margin-top: 1em;

}

first-paragraph {

position: absolute;
text-align: justify;
width: 400px;
height: 300px;
left: 10%;

}

Hi David. Here's a suggestion. Try positioning the .blog to the right of your .navigation using the code below. Hope this helps and all the best with your learning ;)

.navigation {
    float: left;
    position: relative;
}

.blog {
    position: absolute;
    top:  0;
    right: 0;
}