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 Refactor the Layout

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

Refractor the layout module, task 2 of 2

I really have no idea what's being asked of me here. I' not an English speaker and I have no recollection of an nth child think being mentioned. Can someone explain this quetion to me? Thanks!

css/main.css
a {
  text-decoration: none;
}

#wrapper {
  max-width: 940px;
  margin: 0 auto;
}

#logo {
  text-align: center;
  margin: 0;
}

h1, h2 {
  color: #fff;
}

nav a {
  color: #fff;
}

nav a:hover {
  color: #32673f;
}

h1 {
  font-family: Changa One, sans-serif;
  font-size: 1.75em;
  font-weight: normal;
}

img {
  max-width: 100%;
}

#gallery {
  margin: 0;
  padding: 0;
  list-style: none;
}

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

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

nav li {
  display: inline-block;
}

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

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

.contact-info {
  list-style: none;
  padding: 0;
  margin: 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;
}

@media screen and (min-width: 480px) {
  #gallery li {
    width: 28.333%
  }

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

4 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

You're close! But, the way you wrote you're nth-child property, you're targeting only the fourth element. You want to target every fourth element, which is done using "4n".

"n" is basically a multiplier, starting at 0. So your code would target 4x0 (nothing), 4x1 (fourth element), 4x2 (eighth element), 4x3 (twelfth element), and so on.

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

Ok, so 4x1 means every 4th element,? I don't know how to express that inside the parenthesis. I tried 41, 4x1, 4*1... I even wrote "every fourth" hehe.

Jeff Lemay
Jeff Lemay
14,268 Points

No no, sorry for the confusion. You want :nth-child(4n).

The stuff with 4x1, etc. was just to illustrate what is happening when you use 4n. n is the key to nth-child.

  #gallery li:nth-child(4n) {
    clear: left;
  }
Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

ok, cool. So, if I wanted the 8th element it would have been 8n, the 12th 12n and so on? I don't have to do much math, right?

Jeff Lemay
Jeff Lemay
14,268 Points

No, that's not how it works. "n" represents an infinite amount of numbers, from 0 thru infinity. If you have 500 items on your page and you use :nth-child(4n), every element that is a multiple of 4 will be touched (4, 8, 12, 16, 20, 24, 400, 444, 496, 500, and all multiples of 4 in between).

So if you used :nth-child(3n), you would affect the elements that are multiple of 3 (3, 6, 9, 12, 15, and so on).

If you only want to affect the eighth element, you would simple use :nth-child(8).

Here is some material if this is still not making since: https://css-tricks.com/how-nth-child-works/