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

HTML How to Make a Website Responsive Web Design and Testing Build a Three Column Layout

Anna Gardener
Anna Gardener
1,521 Points

3 column fixed with nth-child, now 2 columns are buggy

After following the instructions to use the nth-child pseudoclass, the 3 column-gallery lines up perfectly! However, now, if I resize the window and get to the smallest screen, the 2 column gallery is messed up. There are three rows and the first item in the second row is missing (=it pushed everything to the right by one.) I wonder why this is happening and how could I fix it? I have looked in the previous answers but could not find a definitive answer. Thank you in advance!

Anna Gardener
Anna Gardener
1,521 Points

My code is:

@media screen and (min-width: 480px) { /**** Two COLUMN LAYOUT ****/

#primary {
    width: 50%;
    float: left;
}
#secondary {
    width: 40%;
    float: right;
}

/**** THREE COLUMN LAYOUT- PAGE ****/

#gallery li {
    width: 28.3333%;
}

#gallery li:nth-child(4) {
    clear:both;
}
Aurelian Spodarec
Aurelian Spodarec
10,801 Points

There isn't sufficent code for this to possibly work. You need more media queries, and it woudl be helpful to see your html and some more css, if you have any.

5 Answers

nico dev
nico dev
20,364 Points

Anna Gardener ,

Thanks for your persistence (which is so needed in this field, where you often crash walls and get stuck!), and don't worry, I am also a beginner. Indeed, I started here this month, in September, hoping I could understand at least the basics, and the truth is I've found it extremely helpful and engaging. I'm already (in little time) learning more than I expected :)

Now, down to our business, regarding your code, I'd like to share a remark and a question.

The remark is: when you want to post code in the forum, it's easier for everyone to read it and for someone to help you if you write it like this:

  1. Write 3 "```" (without the quotation marks) in a blank line. You should find that symbol in your keyboard under "Esc" and on top of "Tab" (provided that you have the English keyboard installed in your Operative System).
  2. Next to it, write "html", "css" (always without quotation marks) or whatever language it may correspond, in our case you'll need either of those for your code.
  3. Press Enter, thus going to the next line, and once there, start typing or pasting your code.
  4. Once finished, press Enter again, to go to the next (blank) line. Again, type 3 "```".
  5. Before posting, if you want you can press the eye button at the bottom right of the box where you're writing your comment, it will allow you to preview how your message will look like once sent, and then you can press the same button (now looks like a pencil) to come back to edition mode.

The question: I noticed that in the fourth image of your <ul>, that is, the one that should be the first image of the second row (thus the one that's giving you trouble), you made an anchor to "img/capricco,jpg", but the image you called subsequently is "img/01.jpg" instead. Like this:

<a href="img/capricco.jpg"> <img src="img/01.jpg" alt="websites"> 

Was that intentional, for some reason like that you wanted one image to link to another one? Or maybe you changed images names and one was left unchanged?

Just one more thing, that I remembered now, as I type: what I was saying about 3 +1 and the Teacher's Notes is this. In your responsive.css, you have:

#gallery li:nth-child(4n) {
    clear:both;
}

However, what Nick suggested (and I learned from him) is that instead of 4n, which is the fourth element and then the eighth element (thus the second in the third row, not the first or last one on any row), etc., we may use "3n + 1" (that is: you have three columns, so the next one after the third will always be cleared from the left).

So, in your case, this is how that applies:

#gallery li:nth-child(3n + 1) {
    clear: left;                              /* believe me, in this case, making it left will be better, you don't want to clear:right :) */
}

Last but not least. The Teacher's Notes sometimes have very important notes or relevant links, you do well in reviewing those of videos you already watched. You might have missed something important! I was going to attach a screenshot, but my connection is not really good here and it was impossible to load it. However, when you're watching a video, you'll find the Teacher's Notes under the video, at the very left. Click on it (if it's not already selected) and you can read it.

NOTE: Not every video has Teacher's Notes.

Let me know your thoughts!

Anna Gardener
Anna Gardener
1,521 Points

Thank you Nico for the extensive reply! I'll make sure I' use the correct formatting next time, apologies for that one more time :) I am not sure why I wrote that for the image... Must be a mistake. For the nth-child and clear functions: I have tried those and that didn't help. I'll give it another try, with the corrected image and see how it goes. Thank you again! :)

Hanxiao Jiang
Hanxiao Jiang
8,526 Points

Thank you, Learned a lot from your post

Graham Tonelli
Graham Tonelli
11,968 Points

What James is trying to say is that you need a second media query defining a breakpoint for your 3 column layout. For example

css
@media and screen (min-width:740px) {
 #gallery li {
width: 28.333%;
}
#gallery li nth-child(4) {
clear: both;
}
}
Aurelian Spodarec
Aurelian Spodarec
10,801 Points

My Name is Bond, James Bond.

But that's correct.

nico dev
nico dev
20,364 Points

Hi Anna Gardener ,

First of all, I guess if you have no problem with the three column layout, and your problem is with the smallest screen (if it's possible to resize it, I assume it's a computer screen), then your media query specified for screen 480px or larger should be fine for now. Good to go there.

I was going to comment to you about the :nth-child(4) that it would be advisable to follow the suggestion in Teacher's Notes and make it :nth'child(3n + 1), plus I was going to suggest that you only clear:left instead of both, in case this might disrupt the format at the right of your 3n + 1 image.

However, I tried your code and it worked for me, plus I thought if this media queries are for 480px or larger and your problem is when it's smaller, the problem may not really lie in those media queries, but either in the index.html or the main.css (or the name you may have used for your main css file).

So my first question is: how many images ( <li> ) do you have there? Five like in the example?

And my second question is, may it be possible to see your index.html and your main.css (or corresponding file name)?

Thank you!

Hi Anna,

A student asked a similar question a few days ago.

https://teamtreehouse.com/community/responsive-design-of-image-gallery-and-the-use-of-nthchild

See if that helps you out. The class name is different but it's overall the same problem.

As Nico mentioned, you do want to change your nth-child expression to 3n + 1 for the 3 column layout.

Anna Gardener
Anna Gardener
1,521 Points

Hi everyone, thank you for trying to help me!

I am not sure how to attach my code, so I guess I'll just copy it here. I have 5 images and followed the video withe very step.

I used clear: both as an attempt to fix the problem, previously it was "left".

I am quite new here, didn't know there was such a thing as "teacher's notes :) I tried the 3+1 though, based on other's comments.

So, with all that, I'l just comment my code below.

Anna Gardener
Anna Gardener
1,521 Points

@media screen and (min-width: 480px) { /**** Two COLUMN LAYOUT ****/

#primary {
    width: 50%;
    float: left;
}
#secondary {
    width: 40%;
    float: right;
}

/**** THREE COLUMN LAYOUT- PAGE PORTFOLIO ****/

#gallery li {
    width: 28.3333%;
}

#gallery li:nth-child(4n) {
    clear:both;
}

/**** ABOUT PAGE ****/ .profile-photo { float:left; margin: 0 5% 80px 0;

}

/******** 3* 5 =15 % for margin 100-15 =85% for images / 3 =28.333

*********/ }

@media screen and (min-width: 660px) {

/**** HEADER ****/ nav { background: none; float: right; font-size: 1.125em; margin-right: 5%; width: 45%; }

#logo {
    float: left;
    margin-left: 5%;
    text-align: left;
    width: 45%;

}

h1 {
    font-size: 2.5em;
}

h2 {
    font-size: 0.9em;
    margin-bottom: 20px;
}

header {
    border-bottom: 5px solid #599a68;
    margin-bottom: 60px;
}

}

Anna Gardener
Anna Gardener
1,521 Points

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Anna|Designer</title> <link rel="stylesheet" href="css/normalize.css"> <link href="https://fonts.googleapis.com/css?family=Merriweather|Raleway" rel="stylesheet"> <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/responsive.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head> <body>

<header>

<a href="index.html" id="logo"> <h1>Anna </h1> <h2>Designer Wizard</h2> </a>

<nav> <ul> <li><a href="index.html" class="selected">Portfolio</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header>

<div id="wrapper" >
<section> <ul id="gallery"> <li> <a href="img/momo.jpg"> <img src="img/momo.jpg" alt="momo cafe"> <p>Branding and Corporate Identity</p> </a> </li> <li> <a href="img/app.jpg"> <img src="img/app.jpg" alt="UIX design"> <p>UIX Design</p> </a> </li> <li> <a href="img/bangkok.jpg"> <img src="img/bangkok.jpg" alt="website and app"> <p>Web&Mobile App design</p> </a> </li> <li> <a href="img/capricco.jpg"> <img src="img/01.jpg" alt="websites"> <p>Web Design</p> </a> </li> <li> <a href="img/logo.jpg"> <img src="img/logo.jpg" alt="logo design"> <p>Logo Design</p> </a> </li> </ul> </section>

<footer> <a href="https://www.pinterest.com/ladyneara/"><img src="img/pinterest.png" alt="pinterest logo" class="social-icon"></a>

<a href="https://www.facebook.com/r.a.kertesz"><img src="img/facebook.png" alt="facebook logo" class="social-icon" ></a>

<a href="#"><img src="img/behance.png" alt="behance logo" class="social-icon"></a> <p>© 2016 Anna.</p>

</footer> </div> </body> </html>

Anna Gardener
Anna Gardener
1,521 Points

My main css file:

/********************************** GENERAL ***********************************/

body { font-family: 'Open Sans', sans-serif; }

wrapper {

max-width: 940px; margin: 0 auto; padding: 0 5%; }

a { text-decoration: none; }

img { max-width: 100%; }

h3 { margin: 0 0 1em 0; }

/********************************** HEADING ***********************************/ header { float:left; margin: 0 0 30px 0; padding: 5px 0 0 0; width: 100%; }

logo {

text-align: center; margin: 0; }

h1 { font-family: 'Changa One', sans-serif; margin: 15px 0; font-size: 1.75em; font-weight: normal; line-height: 0.8em; }

h2 { font-size: 0.75em; margin: -5px 0 0; font-weight: normal; }

/********************************** NAVIGATION ***********************************/

nav { text-align: center; padding: 10px 0; margin: 20px 0 0; }

nav ul { list-style: none; margin: 0 10px; padding: 0; }

nav li { display: inline-block; }

nav a { font-weight: 800; padding: 15px 10px; }

/********************************** FOOTER ***********************************/

footer { font-size: 0.75em; text-align: center; clear: both; padding-top: 50px; color: #ccc; }

.social-icon { width: 40px; height: 40px; margin: 0 5px; }

/********************************** PAGE: PORTFOLIO ***********************************/

gallery {

margin: 0; padding: 0; list-style: none; }

gallery li {

float: left; width: 45%; margin: 2.5%; background-color: #f5f5f5; color: #bdc3c7; }

gallery li a p {

margin: 0;
padding: 5%;
font-size: 0.75em;
color: #bdc3c7;

}

/********************************** PAGE:ABOUT ***********************************/

.profile-photo { display: block; max-width: 150px; margin: 0 auto 30px; border-radius: 100%; }

/********************************** PAGE:CONTACT ***********************************/ .contact-info { list-style: none; margin: 0; padding: 0; font-size: 0.9em;

}

.contact-info a { display: block; min-height: 20px; background-repeat: no-repeat; background-size: 20px 20px; padding: 0 0 0 30px; margin: 0 0 10px; }

.contact-info li.phone a { background-image: url('../img/phone.png'); } .contact-info li.mail a { background-image: url('../img/mail.png'); } .contact-info li.behance a { background-image: url('../img/twitter.png'); }

/********************************** COLORS ***********************************/

/* site body */ body { background-color: #fff; color: #999; }

/* green header */ header { background: #6ab47b; border-color: #599a68; }

/* nav background on mobile */ nav { background: #599a68; }

/* logo text */ h1, h2 { color: #fff; }

/* links */ a { color: #6ab47b; }

/* nav link */ nav a, nav a:visited { color: #fff; }

/* selected nav link */ nav a.selected, nav a:hover { color: #32673f; }