×

css · level 2 · unit 6 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

:first-child, :last-child and count siblings.

A is an element that shares a parent with another, like the <li> items in one list. Some pseudo-classes pick siblings by position: li:first-child is the first, and li:last-child is the last.

:nth-child() takes a number or a pattern. li:nth-child(3) is the third. odd and even pick every other one, perfect for striped rows, and 3n picks every third.

They count every sibling, whatever its tag, and they start counting from 1, not 0.

<ul>
  <li>Pickles: 120 points</li>
  <li>Rex: 95 points</li>
  <li>Luna: 90 points</li>
  <li>Mango: 70 points</li>
  <li>Biscuit: 45 points</li>
</ul>
li:nth-child(odd) {
  background-color: whitesmoke;
}
li:first-child {
  font-weight: bold;
}

Press show my page to see it here.

Try li:nth-child(3n) in the first rule and only the third item gets the background.