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
Cheryl Oliver
21,676 PointsWhere do I put my clearfix?
Im having a problem with a collapsing margin and I dont know where to put the clearfix that it will actually work. Please can someone help?
<div class="calculator">
<img src="images/portfolio-4.png">
<h3 class="titles">Calculator</h3>
<p class="content">Someone can enter in the numbers they want, and press the big blue button and get the result.</p>
</div>
<div class="contact" id="contact">
<h3 class="alt-title">CONTACT</h3>
<p class="contact-info">If you're interested in chatting or want more information about what I've been working on, I'd love to hear from you.</p>
<p class="telephone">Phone<a href="tel:+1(111)555-1234">+1 (111) 555-1234</a></p>
<p class="email">Email<a href="mailto:email@yoursite.com">email@yoursite.com</a></p>
</div>
</div>
<div class="footer">
<p class="footer-name">Cheryl Casteling</p>
<a href="#top" class="top">BACK TO TOP</a>
</div>
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
/* ///// MEDIA QUERIES ////// */
@media (min-width: 768px) {
.main-nav li a {
display: inline-block;
text-align: center;
vertical-align: top;
margin: 0 35px;
padding-right: 0 20px;
}
.nav li {
display: inline-block;
text-align: center;
}
.profile-image,
.intro {
float: right;
width: 45%;
padding-right: 30px;
}
.intro {
margin-left: -30px;
padding-top: 80px;
}
.marketing,
.search {
float: left;
width: 50%;
}
.travel,
.map {
float: left;
width: 50%;
}
.photo,
.calculator {
float: left;
width: 50%;
}
}
Cheryl Oliver
21,676 Points'''HTML
<!doctype html>
<html lang="en">
<head>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<title>Responsive Layout Project</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" type="text/css" src="css/normalize.css">
</head>
<body>
<div class="main-nav">
<h2 class="name" id="top">Cheryl Casteling</h2>
<ul class="nav">
<li><a href="index.html">HOME</a></li>
<li><a href="#portfolio">PORTFOLIO</a></li>
<li><a href="#contact">CONTACT</a></li>
</ul>
</div>
<header class="clearfix">
<img src="images/responsive-layout-profile.png" alt="Drawing of Cheryl Casteling" class="profile-image">
<p class="intro">Hi! I'm a front-end developer who loves responsive design and css. I recently finished a degree in front-end web development at Treehouse and am excited to put all my skills to use!</p>
</header>
<div class="main-content">
<h3 class="alt-title" id="portfolio">PORTFOLIO</h3>
<div class="marketing">
<img src="images/portfolio-1.png">
<h3 class="titles">Marketing Page</h3>
<p class="content">The project shows the front page of a marketing website meant for a specific business I'm interested in.</p>
</div>
<div class="search">
<img src="images/portfolio-2.png">
<h3 class="titles">Search Page</h3>
<p class="content">The project searches through a specific database to find information that the user is trying to look up.</p>
</div>
<div class="travel">
<img src="images/portfolio-3.png">
<h3 class="titles">Travel App</h3>
<p class="content">This project compares travel times based on different transportation methods and tells you the best one.</p>
</div>
<div class="map">
<img src="images/portfolio-6.png">
<h3 class="titles">Map of Favorite Spots</h3>
<p class="content">This project uses mapping apis to plot points for my favorite spots in the city for a do-it-yourself walking tour.</p>
</div>
<div class="photo">
<img src="images/portfolio-5.png">
<h3 class="titles">Photo Gallery</h3>
<p class="content">This project shows pictures from a recent trip to the viewer and allows them to easily navigate through the photos.</p>
</div>
<div class="calculator">
<img src="images/portfolio-4.png">
<h3 class="titles">Calculator</h3>
<p class="content">Someone can enter in the numbers they want, and press the big blue button and get the result.</p>
</div>
<div class="contact" id="contact">
<h3 class="alt-title">CONTACT</h3>
<p class="contact-info">If you're interested in chatting or want more information about what I've been working on, I'd love to hear from you.</p>
<p class="telephone">Phone<a href="tel:+1(111)555-1234">+1 (111) 555-1234</a></p>
<p class="email">Email<a href="mailto:email@yoursite.com">email@yoursite.com</a></p>
</div>
</div>
<div class="footer">
<p class="footer-name">Cheryl Casteling</p>
<a href="#top" class="top">BACK TO TOP</a>
</div>
</body>
'''CSS
/* /////// GLOBAL STYLES ////// */
- { box-sizing: border-box; }
/* ///// STYLING ////// */
body { font-family: sans-serif; font-weight: 200; }
.titles { font-family: sans-serif; font-weight: 400; }
.nav { list-style: none; margin-left: 0; padding: 0; }
.main-nav li a { color: black; background-color: #fafafa; padding: 5px 5px; display: block; text-decoration: none; margin: 2px 0 0 0; }
.main-nav, .main-content, .contact, .profile-image, .intro { text-align: center; }
.profile-image { display: block; margin: 0 auto; padding: 20px 0; width: 60%; }
.alt-title, .nav { font-family: "Oswald", impact, sans-serif; font-weight: 600; font-size: 1em; }
img { width: 95%; margin: 10px; }
.footer { width: 100%; border-top: solid 1px grey; list-style: none; display: inline-block; margin-top: 5px }
.top { float: right; font-weight: 600; font-size: 0.75em; margin-top: 12px; }
.footer-name { float: left; font-weight: 600; margin-top: 10px; }
a { text-decoration: none; color: black; font-weight: 600; margin-left: 15px; }
.alt-title { margin-top: 100px; font-family: }
.content, .intro { margin: 10px; padding: 10px 10px;
}
.email { padding-bottom: 40px; }
.clearfix:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; }
/* ///// MEDIA QUERIES ////// */
@media (min-width: 768px) { .main-nav li a { display: inline-block; text-align: center; vertical-align: top; margin: 0 35px; padding-right: 0 20px;
}
.nav li {
display: inline-block;
text-align: center;
}
.profile-image,
.intro {
float: right;
width: 45%;
padding-right: 30px;
}
.intro {
margin-left: -30px;
}
.marketing,
.search,
.travel,
.map,
.photo,
.calculator {
float: left;
width: 50%;
}
}
@media (min-width: 1024px) { .marketing, .search, .travel, .map, .photo, .calculator { float: left; width: 33%; } }
Caleb Kleveter
Treehouse Moderator 37,862 PointsI edited the post to make the code more readable.
4 Answers
Adam Hill
7,492 PointsNot 100% sure exactly where the problem is that you're trying to fix.
After reconstructing your page from the html css above I can see that the #contact div isn't clearing the rest of the content in the .main-content div.
Quickest fix for that would be to force that div to clear by adding
.contact {
display: block;
width: 100%;
clear: both;
}
Alternatively you could add another wrapper around all of the content except the #contact div in .main-content and add a clearfix to that, or move the #contact div outside of the .main-content div.
Rich Turnbull
9,212 PointsIt's a bit hard to get the full picture from the code snippets above but the clearfix is a class you assign to the parent container of floating items. If i have a div with the class "gallery" and i have 4 floating images in there, i will put the clearfix on the gallery. The clear fix basically dynamically inserts an additional element after the last child element and applies the css you specify. consider this:
Using a clearfix to automatically clear floats
<div class="gallery clearfix">
<img class="float" src="#" />
<img class="float" src="#" />
<img class="float" src="#" />
<img class="float" src="#" />
</div>
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
Vs manually clearing floats in html
<div class="gallery">
<img class="float" src="#" />
<img class="float" src="#" />
<img class="float" src="#" />
<img class="float" src="#" />
<div style="clear:both;"></div>
</div>
Bob McCarty
Courses Plus Student 16,618 PointsHi Cheryl,
The parent container needs to clear. Use the following css in the media query.
.main-content:after {
content:" ";
display: table;
clear: both;
}
For more info on clearfix, just do a web search for clearfix .
Bob
Cheryl Oliver
21,676 PointsNo that didnt work unfortunately.
Bob McCarty
Courses Plus Student 16,618 PointsCheryl,
Who did respond to with this answer? Did you try the following?
@media (min-width: 1024px) {
.marketing, .search, .travel, .map, .photo, .calculator {
float: left; width: 33%;
}
.main-content:after {
content: " ";
display: table;
clear: both;
}
}
Bob
Mark DeArmond
15,233 PointsMark DeArmond
15,233 PointsWould you mind posting all of your html and css? It looks like I'm only seeing part of it.