I am puzzled by the following HTML markup and CSS 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 am trying to target the .target
element, only if it is the last one within its container.
The use of the :last-of-type
class seems to work in this scenario, but I find it confusing. Shouldn't it apply to both .target
elements? After all, they are the only and last elements within their respective parent containers. Am I misunderstanding the purpose of the :last-of-type
selector?