Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS :nth-of-type() Selector

Posted in CSS Tutorial
Updated on Oct 27, 2024
By Mari Selvan
๐Ÿ‘๏ธ 17 - Views
โณ 4 mins
๐Ÿ’ฌ 1 Comment
CSS :nth-of-type() Selector

Photo Credit to CodeToFun

๐Ÿ™‹ Introduction

The :nth-of-type() selector in CSS is a powerful pseudo-class that allows you to select elements based on their position among siblings of the same type.

This selector is useful when you need to target elements in a specific order without adding additional classes or IDs. The argument provided inside the nth-of-type() function determines which elements are selected.

๐Ÿ’ก Syntax

The signature of the :nth-of-type() Selector is as follows:

Syntax
Copied
Copy To Clipboard
element:nth-of-type(n) {
    /* CSS properties */
}
  • element: The type of element (e.g., <div>, <p>, <li>) to be selected.
  • n: A number or a formula (e.g., 2, odd, even, 3n+1) that defines the pattern of element selection.

๐Ÿงช Formula Explanation

  • n: Represents an integer starting from 0.
  • odd: Targets elements at odd positions (1st, 3rd, 5th...).
  • even: Targets elements at even positions (2nd, 4th, 6th...).
  • an+b: A more flexible formula. For example, 3n+1 selects every third element, starting from the first.

๐Ÿ“ Example

Here is an example that demonstrates the usage of the :nth-of-type() selector in CSS:

โ˜ ๏ธ HTML

HTML
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS :nth-of-type() Selector Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <p>First paragraph</p>
        <p>Second paragraph</p>
        <p>Third paragraph</p>
        <p>Fourth paragraph</p>
        <p>Fifth paragraph</p>
    </div>
</body>
</html>

๐ŸŽจ CSS

CSS
Copied
Copy To Clipboard
/* Select every 2nd paragraph (even-numbered) */
p:nth-of-type(even) {
    color: blue;
    font-weight: bold;
}

/* Select the 3rd paragraph */
p:nth-of-type(3) {
    color: green;
    font-size: 18px;
}

In this example:

  • The p:nth-of-type(even) selector applies styles to the second and fourth paragraphs, making their text blue and bold.
  • The p:nth-of-type(3) selector applies styles specifically to the third paragraph, making it green and increasing its font size.

๐Ÿ’ฌ Usage Tips

  • Use for cleaner markup: Instead of adding extra classes for styling specific elements, use :nth-of-type() to minimize the need for additional HTML attributes.
  • Combine with other pseudo-classes: You can use :nth-of-type() alongside other pseudo-classes like :hover or :last-of-type for more advanced effects.
  • Apply to any type of element: It works with any element type, such as paragraphs, list items, divs, and more.

โš ๏ธ Common Pitfalls

  • Siblings of the same type: The :nth-of-type() selector only targets elements of the same type. If your target elements are mixed with other types (e.g., a <div> between <p> elements), the counting will only apply to the elements of the specified type.
  • Zero-based counting: Be mindful of the fact that formulas using n (like 3n+1) start counting from the first element (n=0). Always test your formula to ensure it selects the right elements.

๐ŸŽ‰ Conclusion

The CSS :nth-of-type() selector provides a flexible and efficient way to target elements based on their position in a group of siblings.

Whether you're selecting specific items in a list or styling alternate rows in a table, this pseudo-class allows you to achieve precise, dynamic styling without cluttering your HTML with extra classes or IDs.

๐Ÿ‘จโ€๐Ÿ’ป Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
๐Ÿ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy