Xuất bản vào ngày 15 tháng 3 năm 2015by Rene SpronkBy Rene Spronk Bạn có thể đã nhìn thấy tất cả các ký tự này trong các tệp CSS của mình, vậy chính xác họ làm gì? Trước khi chúng tôi trả lời rằng, các nhân vật mà chúng ta sẽ xem xét ngày hôm nay là:> [lớn hơn], + [dấu cộng] và ~ [tilde]. Trong CSS, chúng được gọi là bộ chọn. Bộ chọn là các mẫu được sử dụng để chọn phần tử bạn muốn tạo kiểu, mặc dù có nhiều bộ chọn khác nhau [một trang chuyên dụng của tất cả các bộ chọn CSS sẽ sớm tăng lên], chúng tôi sẽ chỉ xem xét ba trang chúng tôi đã đề cập. Điều này được gọi là một người chọn trẻ em. Bộ chọn trẻ em CSS áp dụng cho các yếu tố là con của một yếu tố khác. Một yếu tố con là một yếu tố là hậu duệ trực tiếp hoặc trực tiếp của một yếu tố khác. Ví dụ: một bộ chọn của biểu mẫu E> Fiêu phù hợp khi phần tử F là hậu duệ trực tiếp của phần tử E. Hãy xem mã sau: This span is a direct child of the div. Trong ví dụ trên, chỉ có hậu duệ trực tiếp của sẽ thay đổi màu nền của chúng thành màu xanh lá cây. Kết quả sẽ trông như thế này: Khoảng thời gian này là một đứa trẻ trực tiếp của div. Khoảng thời gian này không phải là một đứa trẻ trực tiếp của div. Span này là một đứa trẻ trực tiếp của div. Điều này được gọi là một bộ chọn liền kề. Bộ chọn liền kề CSS sẽ chỉ chọn phần tử ngay trước phần tử trước. Hãy xem mã sau: Paragraph number one should now have a green background. Paragraph number two should have no affect. Paragraph number three should have no affect. Với ví dụ trên, đoạn đầu tiên sẽ thay đổi nền thành màu xanh lá cây, vì đây là phần tử đầu tiên đi trước phần tử. Kết quả sẽ trông như thế này: 2. + [cộng với dấu hiệu] bộ chọn Điều này được gọi là một bộ chọn liền kề. Bộ chọn liền kề CSS sẽ chỉ chọn phần tử ngay trước phần tử trước. Hãy xem mã sau: Với ví dụ trên, đoạn đầu tiên sẽ thay đổi nền thành màu xanh lá cây, vì đây là phần tử đầu tiên đi trước phần tử. Đoạn số một bây giờ nên có một nền màu xanh lá cây. Paragraph number one should now have a green background. Paragraph number two will also have a green background. Paragraph number three will also have a green background. Đoạn hai nên không có ảnh hưởng. Kết quả sẽ trông như thế này: 2. + [cộng với dấu hiệu] bộ chọn Điều này được gọi là một bộ chọn liền kề. Bộ chọn liền kề CSS sẽ chỉ chọn phần tử ngay trước phần tử trước. Hãy xem mã sau: Với ví dụ trên, đoạn đầu tiên sẽ thay đổi nền thành màu xanh lá cây, vì đây là phần tử đầu tiên đi trước phần tử. Ví dụ chọn lọc liền kề
1.> [lớn hơn] bộ chọn
Lets look at the following code:span { background-color: #fff; }
div > span {
background-color: green;
}
This span is not a direct child of the div.
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.2. + [cộng với dấu hiệu] bộ chọn
Lets
look at the following code:h4 + p {
background-color: green;
}
Adjacent Selector Example
Khoảng thời gian này là một đứa trẻ trực tiếp của div. Khoảng thời gian này không phải là một đứa trẻ trực tiếp của div. Span này là một đứa trẻ trực tiếp của div.
Ví dụ chọn lọc liền kề
h4 ~ p {
background-color: green;
}
General Selector Example
Khoảng thời gian này là một đứa trẻ trực tiếp của div. Khoảng thời gian này không phải là một đứa trẻ trực tiếp của div. Span này là một đứa trẻ trực tiếp của div.
Nếu bạn thích sử dụng CSSportal, vui lòng xem xét chia sẻ trang này với người dùng khác, chỉ cần nhấp vào liên kết truyền thông xã hội ưa thích của bạn hoặc sao chép trang web từ liên kết bên dưới.
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
ĐọcCSS universal selectors. It can be used to select any and all types of elements in an HTML page. The asterisk can also be followed by a selector while using to select a
child object. This selector is useful when we want to select all the elements on the page.
For example:
* { property : value; }
Bàn luận
parent * { property : value; }
Dấu hoa thị [*] được gọi là bộ chọn phổ quát CSS. Nó có thể được sử dụng để chọn bất kỳ và tất cả các loại phần tử trong một trang HTML. Dấu hoa thị cũng có thể được theo sau bởi một bộ chọn trong khi sử dụng để chọn một đối tượng con. Bộ chọn này rất hữu ích khi chúng tôi muốn chọn tất cả các yếu tố trên trang. & Nbsp; ví dụ: & nbsp; & nbsp;
grand_parent * grand_child{ property : value; }
Examples:
Input: * { color : green; } Output: The text in all the elements become green Input: * p { color : green; } Output: The text inside those which are direct children of any elements of HTML the page will become green.
Trong khi chọn các phần tử, nếu chúng ta chỉ sử dụng dấu hoa thị [*], thì tất cả các phần tử của trang HTML sẽ được chọn không phân biệt mối quan hệ cha mẹ con. Nếu chúng ta sử dụng dấu hoa thị [*] trong khi chọn con cái của một phụ huynh cụ thể, vì vậy chúng ta có thể chọn tất cả con cái của cha mẹ đó bằng: & nbsp; & nbsp;
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
0This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
2This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
5This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
7This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
9This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
2h4 + p {
background-color: green;
}
3h4 + p {
background-color: green;
}
4h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
6h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
8h4 + p {
background-color: green;
}
3Adjacent Selector Example
Paragraph number one should now have a green background.
Paragraph number two should have no affect.
Paragraph number three should have no affect.
0h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
6h4 + p {
background-color: green;
}
1Adjacent Selector Example
Paragraph number one should now have a green background.
Paragraph number two should have no affect.
Paragraph number three should have no affect.
4h4 + p {
background-color: green;
}
3Adjacent Selector Example
Paragraph number one should now have a green background.
Paragraph number two should have no affect.
Paragraph number three should have no affect.
6h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
6h4 + p {
background-color: green;
}
1h4 ~ p {
background-color: green;
}
0Chúng ta có thể sử dụng dấu hoa thị [*] cho cấp độ trung gian trong khi chọn một phần tử bởi: & nbsp; & nbsp;
h4 + p {
background-color: green;
}
1h4 + p {
background-color: green;
}
6This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
7h4 ~ p {
background-color: green;
}
6This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
9This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 ~ p {
background-color: green;
}
6This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
5This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1General Selector Example
Paragraph number one should now have a green background.
Paragraph number two will also have a green background.
Paragraph number three will also have a green background.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
7This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1General Selector Example
Paragraph number one should now have a green background.
Paragraph number two will also have a green background.
Paragraph number three will also have a green background.
7This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3HTML
* { property : value; }6
* { property : value; }7
* { property : value; }8
h4 ~ p {
background-color: green;
}
6* { property : value; }1
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 + p {
background-color: green;
}
1This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1parent * { property : value; }4
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1parent * { property : value; }7
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3parent * { property : value; }9
grand_parent * grand_child{ property : value; }0
parent * { property : value; }9
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1grand_parent * grand_child{ property : value; }3
grand_parent * grand_child{ property : value; }4
grand_parent * grand_child{ property : value; }3
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 + p {
background-color: green;
}
3h4 ~ p {
background-color: green;
}
6parent * { property : value; }7
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 + p {
background-color: green;
}
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1grand_parent * grand_child{ property : value; }3
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
7This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1Input: * { color : green; } Output: The text in all the elements become green Input: * p { color : green; } Output: The text inside those which are direct children of any elements of HTML the page will become green.7
Input: * { color : green; } Output: The text in all the elements become green Input: * p { color : green; } Output: The text inside those which are direct children of any elements of HTML the page will become green.8
Input: * { color : green; } Output: The text in all the elements become green Input: * p { color : green; } Output: The text inside those which are direct children of any elements of HTML the page will become green.7
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 ~ p {
background-color: green;
}
6grand_parent * grand_child{ property : value; }3
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 + p {
background-color: green;
}
1h4 ~ p {
background-color: green;
}
6parent * { property : value; }4
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
1parent * { property : value; }7
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
10parent * { property : value; }7
This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
7h4 ~ p {
background-color: green;
}
6General Selector Example
Paragraph number one should now have a green background.
Paragraph number two will also have a green background.
Paragraph number three will also have a green background.
7This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 ~ p {
background-color: green;
}
6General Selector Example
Paragraph number one should now have a green background.
Paragraph number two will also have a green background.
Paragraph number three will also have a green background.
3This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3h4 ~ p {
background-color: green;
}
6This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
2This span is a direct child of the div.
This span is not a direct child of the div.
This span is a direct child of the div.
3Output:
Trình duyệt được hỗ trợ:
- Google Chrome 4.0
- Internet Explorer 7.0
- Firefox 2.0
- Opera 9.6
- Safari 3.1