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 portfolio image positioning messes up between mobile and bigger.

I've finished the "How to Build a Website" class and have used my own images and description for the portfolio. Since the beginning, building the mobile version, my 5th image was bumped down by the description of the 3rd. I learned about the:

gallery li:nth-child(5n) {

clear: left; 

}

I've tried it in various ways within my responsive and main css files, however I can't get it to work for both my mobile and expanded versions of the site. Currently, I created an @media screen set to 0 and within there I cleared it left, then my media screen set to 480px I floated it back left, but it doesn't work in both.

Help?

5 Answers

dylan goddard
dylan goddard
12,752 Points

Have you tried

gallery li:nth-child(4n) {

 clear:left;

}

I'm pretty sure you need to clear the first image in the second row. And remember to ad the hash (#) before you #gallery id selector.

Zhihao Wang
Zhihao Wang
9,687 Points

I think your problem is that you do not have the hash symbol to tell the stylesheet that you are targeting an id.

You also need to apply this style to the 4th image, instead of the 5th.

Here's the new version:

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

Sorry guys, Though you're both correct that I forgot to add the # in my post, it was in my actual code and that wasn't the problem I was experiencing. Turns out, depending on the size, I needed to kick every other image (when small) and every 3rd image (when big.) My teacher told me not to use (5n), but to solve it with more complex math:

/*3, 5, 7, 9 */

gallery li:nth-child(2n+1){

clear left; }

/4, 7, 10, 13/

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

clear left; }

However, when I did that, it was still messing up. Figured out, my 'children' weren't overwriting each other, but combining. So it was kicking 3,4,5,7,9,10. What I had to do was set a max screen size for the small, so it would reset when I extend beyond that size. Here's my working responsive.css

@media screen and (min-width: 0px) and (max-width: 479px){ #gallery li:nth-child(2n+1) { clear: left; } }

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

#gallery li { float: left; width: 28.3333% }

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

}

dylan goddard
dylan goddard
12,752 Points

Cool! Thanks for sharing your solution with us, glad to see you got things working!

awesome Alexander Kenemer, I spent 2 weeks trying to figure out what was wrong with my code (after changing the pic size). You solved it for me here thanks a lot !

Glad it's helpful, I'm sorry the code didn't copy properly.