The :nth-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent. The 'n' can be a number, a keyword, or a formula. In this example, the selector specifies a background color for every <p> element that is the second p element of its parent:
HTML file: | Displayed by browser: |
---|---|
<style>
<body> <h1>The first paragraph.</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </body> |
This is a headingThe first paragraph. The second paragraph. The third paragraph. The fourth paragraph. |