I have the following markup and style:
[class^="container"] .target:last-of-type {
color:red;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="container-1">
<div class="item">1</div>
<div class="item">2</div>
<div class="target">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
<br>
<div class="container-2">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="target">5</div>
</div>
</body>
</html>
I want to select the .target
element, but only if it is the last within its container.
The :last-of-type
class works in this case, I just don't understand why. Shouldn't it highlight both .target
elements? They are the last-of-type (and only) elements within their parent containers. Or I am misunderstanding the :last-of-type
selector?