Show Định nghĩa và sử dụng
Cấu trúc:nth-of-type(n) { property: value; } Ví dụHTML viết:<html> <head></head> <body> <p>Thành phần độc lập thứ nhất</p> <p>Thành phần độc lập thứ 2</p> <p>Thành phần độc lập thứ 3</p> <p>Thành phần độc lập thứ 4</p> <p>Thành phần độc lập thứ 5</p> <p>Thành phần độc lập thứ 6</p> <div> <p>Thành phần thứ nhất</p> <p>Thành phần thứ 2</p> <p>Thành phần thứ 3</p> <p>Thành phần thứ 4</p> <p>Thành phần thứ 5</p> <p>Thành phần thứ 6</p> </div> <div> <h4>Thành phần thứ nhất</h4> <div>Thành phần thứ 2</div> <p>Thành phần thứ 3</p> <p>Thành phần thứ 4</p> <p>Thành phần thứ 5</p> <p>Thành phần thứ 6</p> </div> <div> <h4>Thành phần thứ nhất</h4> <ul> <li>ul cùng cấp với p, nên tất cả li bên trong đều được coi là thành phần thứ 2</li> <li>ul cùng cấp với p, nên tất cả li bên trong đều được coi là thành phần thứ 2</li> </ul> <p>Thành phần thứ 3</p> <p>Thành phần thứ 4</p> <p>Thành phần thứ 5</p> <p>Thành phần thứ 6</p> </div> </body> </html> Hiển thị trình duyệt khi chưa có CSS:Thành phần độc lập thứ nhất Thành phần độc lập thứ 2 Thành phần độc lập thứ 3 Thành phần độc lập thứ 4 Thành phần độc lập thứ 5 Thành phần độc lập thứ 6 Thành phần thứ nhất Thành phần thứ 2 Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 Thành phần thứ nhấtThành phần thứ 2 Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 Thành phần thứ nhất
Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 CSS viết:p:nth-of-type(3){ color: blue; } Hiển thị trình duyệt khi đã có css:Thành phần độc lập thứ nhất Thành phần độc lập thứ 2 Thành phần độc lập thứ 3 Thành phần độc lập thứ 4 Thành phần độc lập thứ 5 Thành phần độc lập thứ 6 Thành phần thứ nhất Thành phần thứ 2 Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 Thành phần thứ nhấtThành phần thứ 2 Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 Thành phần thứ nhất
Thành phần thứ 3 Thành phần thứ 4 Thành phần thứ 5 Thành phần thứ 6 Ta thấy những thành phần <p> ở vị trí thứ 3 đều đã được chọn. Trình duyệt hỗ trợ":nth-of-type(n)" được hỗ trợ trong đa số các trình duyệt, ngoài trừ trình duyệt Internet Explorer 8 trở xuống. 1) Bộ chọn :nth-of-type(n) trong CSS- Bộ chọn :nth-of-type(n) dùng để chọn những phần tử là phần tử con (thuộc một kiểu nào đó) thứ n của cha nó. 2) Cách sử dụng bộ chọn :nth-of-type(n) trong CSS- Để sử dụng bộ chọn :nth-of-type(n), ta dùng cú pháp như sau:
3) Một số ví dụ- Chọn những phần tử <p> là phần tử có kiểu <p> thứ ba bên trong cha nó.
Xem ví dụ
Xem ví dụ
Xem ví dụ ExampleHow to use the :nth-of-type() selector: /* Selects the second element of div siblings */ /* Selects the second li element in a list */ /* Selects every third element among any group of siblings */ Try it Yourself » More "Try it Yourself" examples below. Definition and UsageThe n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.
Browser SupportThe numbers in the table specifies the first browser version that fully supports the selector.
CSS Syntax :nth-of-type(number) { More ExamplesExampleOdd and even are keywords that can be used to match child elements whose index is odd or even (the index of the first child is 1). Here, we specify two different background colors for odd and even p elements: p:nth-of-type(odd) { p:nth-of-type(even) { Try it Yourself » ExampleUsing a formula (an + b). Description: a represents a cycle size, n is a counter (starts at 0), and b is an offset value. Here, we specify a background color for all p elements whose index is a multiple of 3 (will select the third, sixth, ninth, etc): p:nth-of-type(3n+0) { Try it Yourself » ExampleHere, we specify a background color for all p elements whose index is a multiple of 3. Then we subtract 1 (will select the second, fifth, eight, etc): p:nth-of-type(3n-1) { Try it Yourself » |