Monday, 4 May 2020

CSS nth-child versus nth-of-type

Consider this HTML structure:
<body>
  <div>foobar</div>
  <span>foo</span>
  <span>bar</span>
</body>

To select the last (the second) span using nth-child:
document.querySelector("body >span:nth-child(3)");

To select the last (the second) span using nth-of-type:
document.querySelector("body >span:nth-of-type(2)");

No comments:

Post a Comment