Các lớp giả CSS của CSS phù hợp với các yếu tố dựa trên vị trí của họ giữa một nhóm anh chị em, đếm từ cuối. Phân loại giả :nth-last-child[]
CSS pseudo-class matches elements based on their position among a group of siblings, counting from the end.Thử nó
Cú pháp
nth-last-child
được chỉ định với một đối số duy nhất, đại diện cho mẫu cho các phần tử phù hợp, đếm từ cuối.:nth-last-child[ [ of ]? ]
Giá trị từ khóa
odd
Đại diện cho các yếu tố có vị trí số trong một loạt anh chị em là lẻ: 1, 3, 5, v.v., đếm từ cuối.
even
Đại diện cho các yếu tố có vị trí số trong một loạt các anh chị em thậm chí là: 2, 4, 6, v.v., đếm từ cuối.
Ký hiệu chức năng
Đại diện cho các yếu tố có vị trí số trong một loạt các anh chị em phù hợp với mẫu
First line
Second line
Third line
Fourth line
Fifth line
0, cho mỗi số nguyên dương hoặc giá trị bằng không của
First line
Second line
Third line
Fourth line
Fifth line
1. Chỉ số của phần tử đầu tiên, đếm từ cuối, là
First line
Second line
Third line
Fourth line
Fifth line
2. Các giá trị
First line
Second line
Third line
Fourth line
Fifth line
3 và
First line
Second line
Third line
Fourth line
Fifth line
4 phải là
First line
Second line
Third line
Fourth line
Fifth line
5s.Ví dụ
Ví dụ bộ chọn
First line
Second line
Third line
Fourth line
Fifth line
6 hoặc
First line
Second line
Third line
Fourth line
Fifth line
7Đại diện cho các hàng lẻ của một bảng HTML: 1, 3, 5, v.v., đếm từ cuối.
First line
Second line
Third line
Fourth line
Fifth line
8 hoặc
First line
Second line
Third line
Fourth line
Fifth line
9Đại diện cho các hàng chẵn của một bảng HTML: 2, 4, 6, v.v., đếm từ cuối.
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
0Đại diện cho yếu tố thứ bảy, đếm từ cuối.
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
1Đại diện cho các yếu tố 5, 10, 15, v.v., đếm từ cuối.
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
2Đại diện cho các yếu tố 4, 7, 10, 13, v.v., đếm từ cuối.
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
3Đại diện cho ba yếu tố cuối cùng trong số một nhóm anh chị em.
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
4 hoặc table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
5Đại diện cho mọi yếu tố
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
6 giữa một nhóm anh chị em. Điều này giống như một bộ chọn table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
7 đơn giản. .A list of four items [styled]:
One
Two
Three
Four
A list of two items [unstyled]:
One
Two
1 hoặc A list of four items [styled]:
One
Two
Three
Four
A list of two items [unstyled]:
One
Two
2Đại diện cho mỗi
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
6 là yếu tố đầu tiên trong số một nhóm anh chị em, đếm từ cuối. Điều này giống như bộ chọn A list of four items [styled]:
One
Two
Three
Four
A list of two items [unstyled]:
One
Two
4.Ví dụ bảng
HTML
First line
Second line
Third line
Fourth line
Fifth line
CSS
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child[-n + 3] {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child[n + 2] {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child[2] {
font-weight: 600;
}
Kết quả
Truy vấn số lượng
Một yếu tố kiểu truy vấn số lượng tùy thuộc vào số lượng trong số chúng có. Trong ví dụ này, danh sách các mục chuyển sang màu đỏ khi có ít nhất ba trong số chúng trong một danh sách nhất định. Điều này được thực hiện bằng cách kết hợp các khả năng của lớp giả và bộ kết hợp anh chị em chung.
HTML
A list of four items [styled]:
One
Two
Three
Four
A list of two items [unstyled]:
One
Two
CSS
/* If there are at least three list items,
style them all */
li:nth-last-child[n + 3],
li:nth-last-child[3] ~ li {
color: red;
}
Kết quả
Truy vấn số lượng
Thông số kỹ thuật # nth-last-child-pseudo |
Sự chỉ rõ
Thông số kỹ thuật không xác định # thứ n-last-child-pseudo