InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS :nth-of-type

    on Apr 9, 2016

    CSS :nth-of-type(an+b) pseudo class represent all sibling elements of same type whose sibling position (starting from 1) is obtained by putting n=0,1,2,3… and so on.

    :nth-of-type values

    valueDescription
    :nth-of-type(2n+1)Every 1st, 3rd, 5th, ... sibling element of a type
    :nth-of-type(odd)Every odd sibling element of a type (same as 2n+1)
    :nth-of-type(2n+2)Every 2nd, 4th, 6th, ... sibling element of a type
    :nth-of-type(even)Every even sibling element of a type (same as 2n+2)
    :nth-of-type(5)Fifth sibling element of a type
    :nth-of-type(-n+6)represents the 6 first sibling elements of a type

    Example of :nth-of-type

    Change background of every alternate (2,4,6…) p tag

    <style type="text/css" media="screen">
    p:nth-of-type(2n+2) {
      background-color:lightgreen;
    }
    </style>
    <div class="box">
      <div>div1</div>
      <p>p1</p>
      <p>p2</p>
      <p>p3</p>
      <p>p4</p>
      <div>div2</div>
      <p>p5</p>
      <p>p6</p>
    </div>
    refresh done
    try it online

    Related

    • Css :nth-child
    • Css :nth-child vs :nth-of-type
    • CSS :nth-last-child
    • Css :first-child selector - first child element
    • Css :first-of-type selector - first child element of type
    • CSS :nth-last-of-type
    • Css :last-child selector - last child element
    • Css :last-of-type selector - last child element of type
    • CSS :only-child - define style if element if only child
    • CSS :only-of-type - define style of only child a type

    Specification

    • W3C CSS 3 :nth-of-type pseudo class specification

    Suggested posts:

    1. Css :last-child selector – last child element
    2. CSS border radius and round corners examples
    3. CSS :not – negation pseudo-class
    4. Mysql how to dump schema of all databases
    5. CSS :nth-last-child
    6. CSS :only-child – define style if element if only child
    7. CSS pseudo class :visited – style for visited links
    8. CSS :root
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS Pseudo Classes, CSS selectors, CSS3, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress