Псевдокласс CSS :nth-of-type()
Псевдокласс :nth-of-type() выбирает элементы одного и того же типа на основе их индекса.
:nth-of-type() можно задать числом, ключевым словом или формулой. Селектор :nth-of-type похож на :nth-child, но есть отличие: он более специфичен. :nth-of-type нацеливается на определённый тип элемента в последовательности только по отношению к однотипным соседям.
Версия
Синтаксис
Синтаксис CSS :nth-of-type()
css
:nth-of-type(number) {
css declarations;
}Пример селектора :nth-of-type():
Пример кода CSS :nth-of-type()
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:nth-of-type(3) {
background: #8ebf42;
}
</style>
</head>
<body>
<h2>:nth-of-type() selector example</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
</body>
</html>Пример :nth-of-type, заданного как "odd" и "even":
Ещё один пример кода CSS :nth-of-type()
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:nth-of-type(odd) {
background: #1c87c9;
}
p:nth-of-type(even) {
background: #8ebf42;
}
</style>
</head>
<body>
<h2>nth-of-type() selector example</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
<p>Paragraph 6</p>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 9</p>
<p>Paragraph 10</p>
</body>
</html>Практика
Что выбирает селектор CSS :nth-of-type()?