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

Bethany Maxfield
Bethany Maxfield
2,944 Points

One small issue with three column gallery responsiveness

For practice, I added some extra images to my gallery, I currently have 8 images in my gallery, I made them thumbnails (meaning the img that appears as the link in the gallery is a cropped version that links to the full size, so that all of my img links are the same size. When I have my browser shrunk down to simulate mobile version, everything stacks nicely into two columns. Yay! When expand to the larger breakpoints, my gallery goes into three columns, except for the last (8th image) which stacks undearneath the seventh. So what I end up with is two rows of three images, then a third row with one and a fourth row with 1. When I expand further, this doesn't resolve. I added the nth-child pseudo class selectors so that the 4th image clears and the 7th image clears. That definitely fixed the 4th image getting wacky and the 7th image getting wacky when scaling from mobile to larger. Here is my responsive CSS code currently:

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

#gallery li { width: 28.3333%; }

#gallery li:nth-child(4n) { /* nth-child pseudo-class selector applied to li elements in the #gallery. the (4n) grabs the 4th <li> in the <ul id="gallery"> */ clear: left; }

#gallery li:nth-child(7n) { /* nth-child pseudo-class selector applied to li elements in the #gallery. the (4n) grabs the 4th <li> in the <ul id="gallery"> */ clear: left; }

}

I'm really confused as to why that last image isn't sitting in line with the 7th.... any ideas?

Thank you so much!

Here is the snapshot of my project: https://w.trhou.se/ybkh9el8wx

4 Answers

Raúl Barrera C.
Raúl Barrera C.
18,943 Points

Hi, instead of :nth-child(4n) and :nth-child(7n), that selects 4 (4x1), 7 (7x1) and 8 (4x2), try using:

#gallery li:nth-child(3n+1) {}

This way select every 3rd+1 element, in this case 4 (3x1 + 1) and 7 (3x2 + 1). Greetings!

Hi Rau,

Your expression is correct but you left off the fact that the 1st child will be selected as well. n starts at zero.

I would suggest not thinking about it as "every 3rd+1 element" because it leads you to missing that the first element is selected too. Similar language is used in the Teacher's notes I linked to as well which can be misleading.

I prefer to think of the expression from right to left, "start with the 1st and then every 3rd after that"

I have also seen others think of it from left to right, "every 3rd element, starting with the first" which works well too.

I think either of these give you an intuitive way to think about it and not miss out on any elements.

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

hi Bethany Maxfield you used the nth-child(4n) this tells the selector to actually select EVERY 4th element and clear it left, thats why the next 4th element is in the posiition 7. you just dont need to use (4n). use this :

#gallery li:nth-child(4)
Bethany Maxfield
Bethany Maxfield
2,944 Points

Oh cool!!! Thank you so much! Both of these worked. I'm sure I'll get into this later in the courses, but could you explain the difference for me if you have some time? I think I understand why both your suggestions worked, Rau Barrera C. Konstantinos Pedarakis

I did some fiddling, and the (3n+1) was the best solution. The (4) gave me clearing problems when scaling the browser page, that first image in the third line wasn't clearing properly. But the (3n+1) seemed to do the trick, just not totally sure why.

Thank you both so much for your help on this! I was wracking my brain trying to figure it out. It makes sense that the values used in this video were specific for the amount of images in the gallery that Nick makes, but doesn't necessarily apply to galleries with more images.

Raúl Barrera C.
Raúl Barrera C.
18,943 Points

Hi Bethany Maxfield, the (3n+1) works as a mathematical formula. "n" is a counter that increments every time, this way you can increase the number of elements and get the same result of three columns ;)

3x1 + 1 = 4

3x2 + 1 = 7

3x3 + 1 = 10

...

Hi Bethany,

I think you might be on the following video? https://teamtreehouse.com/library/how-to-make-a-website/responsive-web-design-and-testing/build-a-three-column-layout

If so, then you'll probably want to take a look at the teacher's notes below the video. I think it will clear up some of the confusion at least.

I'm not sure if you still need further explanation on this but it might help to walk through the thought process of how you might come up with the correct expression on your own given that the design calls for 3 columns.

Regardless of how many columns you want, the important thing is that you want the first item in each row to clear the previous floats.

The first step then is determining which items are the first in each row.

If we look at this numbered 3 column grid

 1  2  3
 4  5  6
 7  8  9
10 11 12

we can see that the first item in each row is going to be 1, 4, 7, 10, and so on

Now it's a matter of recognizing the pattern and figuring out the correct expression to generate those numbers.

Hopefully, you'll be able to recognize that you're always adding 3 to get to the next number. You start with 1 and then you keep adding 3.

nth-child expressions take the form of an + b where you choose integers for a and b and you leave n alone. b is what you start on and a is what you keep adding or subtracting to get to the next number.

So we know that we want to start on 1 and keep adding 3. That gives us the correct expression 3n + 1 that was posted by Rau.

This may seem like a long process to come up with 3n + 1 but once you have some practice then you'll likely be able to do this all in your head within a few seconds.

Hopefully that didn't add to your confusion.

Bethany Maxfield
Bethany Maxfield
2,944 Points

@Jason Anello Thank you! That actually really clarifies this thought process for me! So in theory, if I wanted to 4 column layout, and wanted the 5th and 9th element to clear, I would use 4n+1 to get that to work out?

The teacher's notes, I have to say, weren't as helpful to me in terms of understanding WHY to use that value, but it does answer the question, I didn't even see this! I'll be sure to check this area for each lesson from now on. Jason, your explanation is far easier for me to understand.

Thank you all so much for your help!