7

Why is last-child not targeting the last article tag in this fiddle?

http://jsfiddle.net/gunP9/

<div class="parent">
<article class="example">111</article>
<article class="example">111</article>
<article class="example">111</article>
<article class="example">111</article>
<section>content</section>
</div>

CSS

.parent .example{ background-color: red;}
.parent .example:last-child{background-color: yellow;}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
byronyasgur
  • 4,627
  • 13
  • 52
  • 96

2 Answers2

14

last-child targets, well, the last child of the parent. In this case, the last child is a section which doesn't have the class example, and consequently nothing matches the selector.

What you're looking for is last-of-type, which matches elements and not classes. Take a look.

jamesplease
  • 12,547
  • 6
  • 47
  • 73
0

Because the selector says "last child of each of articles"

Moseleyi
  • 2,585
  • 1
  • 24
  • 46