:nth-child()
and :nth-of-type()
pseudo-classes in CSS are used to select elements based on their position in a parent container. Here’s a detailed look at how to use each:
<p>Using :nth-child()
<p>The :nth-child()
pseudo-class selects elements based on their position among all siblings within the same parent, regardless of their type. The syntax is::nth-child(an b)
a
and b
are integers, and n
is a counter starting from 0. Common patterns include::nth-child(2)
: Selects the second child element.<li>:nth-child(odd)
: Selects all odd-numbered child elements.<li>:nth-child(even)
: Selects all even-numbered child elements.<li>:nth-child(3n)
: Selects every third child element (3, 6, 9, etc.).<li>:nth-child(3n 1)
: Selects every third child element starting from the first (1, 4, 7, etc.).<ul> <li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li> </ul>
li:nth-child(2) { background-color: yellow; }
<li>
element.<p>Using :nth-of-type()<p>The :nth-of-type()
pseudo-class is similar, but it only selects elements based on their position among siblings of the same type. The syntax is identical to :nth-child()
::nth-of-type(an b)
<ul> <li>First item</li> <p>Some text</p> <li>Second item</li> <p>More text</p> </ul>
li:nth-of-type(2) { background-color: yellow; }
<li>
element, but would not affect the <p>
elements.:nth-child()
and :nth-of-type()
are::nth-child()
counts all child elements regardless of their type. It considers the element's position among all siblings.<li>:nth-of-type()
counts only siblings of the same element type. It is more specific in its selection.:nth-child()
is less precise if you are targeting a specific type of element because it counts all elements.<li>:nth-of-type()
allows you to target elements more precisely by type, which can be more efficient in certain scenarios.:nth-child()
when you need to select elements based on their position among all siblings, regardless of type.<li>Use :nth-of-type()
when you want to select elements based on their position among siblings of the same type.<div class="grid"> <div>Item 1</div> <p>Item 2</p> <span>Item 3</span> <div>Item 4</div> <p>Item 5</p> <span>Item 6</span> </div>
.grid :nth-child(3n) { background-color: lightblue; }
:nth-child(3n)
will style every third item (Item 3 and Item 6) regardless of their element type.<p>Using :nth-of-type() Example:<p>If you want to style every second <div>
element in the same grid layout:<div class="grid"> <div>Item 1</div> <p>Item 2</p> <span>Item 3</span> <div>Item 4</div> <p>Item 5</p> <span>Item 6</span> <div>Item 7</div> </div>
.grid div:nth-of-type(2n) { background-color: lightgreen; }
:nth-of-type(2n)
will style every second <div>
element (Item 4 and Item 7).:nth-child()
and :nth-of-type()
for better performance, consider the following strategies::nth-of-type()
instead of :nth-child()
when you specifically need to target elements of a particular type. This can be more efficient as the browser doesn't need to process unrelated elements.<li>Keep your selectors as specific as necessary but as general as possible to reduce the overhead of matching.:nth-child()
or :nth-of-type()
with class selectors can help reduce the DOM traversal required. For instance:.list-item:nth-child(2) { /* Style properties */ }
.list-item
, which can be more efficient than searching through all children.:nth-child()
or :nth-of-type()
can help avoid the need for overly specific selectors. For example:ul li:nth-child(2) { /* Instead of */ /* ul > li > ul > li:nth-child(2) */
:nth-child(2)
is likely to perform better than a complex combination of pseudo-classes and attribute selectors.:nth-child()
and :nth-of-type()
is optimized for performance.The above is the detailed content of How do you use the :nth-child() and :nth-of-type() pseudo-classes?. For more information, please follow other related articles on the PHP Chinese website!