This article mainly introduces the relevant information of CSS selector grouping. Friends who need it can refer to it
Selector grouping
## Suppose you want both the h2 element and the paragraph to be gray. To achieve this, the easiest thing to do is to use the following declaration:h2, p {color:gray;}
body, h2, p, table, th, td, pre, strong, em {color:gray;}
/* no grouping */ h1 {color:blue;} h2 {color:blue;} h3 {color:blue;} h4 {color:blue;} h5 {color:blue;} h6 {color:blue;} /* grouping */ h1, h2, h3, h4, h5, h6 {color:blue;}
/* group 1 */ h1 {color:silver; background:white;} h2 {color:silver; background:gray;} h3 {color:white; background:gray;} h4 {color:silver; background:white;} b {color:gray; background:white;} /* group 2 */ h1, h2, h4 {color:silver;} h2, h3 {background:gray;} h1, h4, b {background:white;} h3 {color:white;} b {color:gray;} /* group 3 */ h1, h4 {color:silver; background:white;} h2 {color:silver;} h3 {color:white;} h2, h3 {background:gray;} b {color:gray; background:white;}
Wildcard Selector
CSS2 introduces a new simple selector - the universal selector, which is displayed as an asterisk (*). This selector can match any element, just like a wildcard. For example, the following rule can make every element in the document red:* {color:red;} <html> <head> <style type="text/css"> * {color:red;} </style> </head> <body> <h1>这是 heading 1</h1> <h2>这是 heading 2</h2> <h3>这是 heading 3</h3> <h4>这是 heading 4</h4> <p>这是一段<b>普通</b>的段落文本。</p> </body> </html>
Declaration Grouping
We can group both selectors and declarations. Suppose you want all h1 elements to have a red background and display blue text using the 28-pixel-high Verdana font, you can write the following style:h1 {font: 28px Verdana;} h1 {color: blue;} h1 {background: red;}
h1 {font: 28px Verdana; color: white; background: black;}
h1 { font: 28px Verdana; color: blue; background: red; }
h1 { font: 28px Verdana; color: blue background: red; }
Combining selector and declaration grouping
We can combine selector grouping and declaration grouping in one rule, and we can use few statements to define relatively complex style. The following rule specifies a complex style for all headings:h1, h2, h3, h4, h5, h6 { color:gray; background: white; padding: 10px; border: 1px solid black; font-family: Verdana; } <html> <head> <style type="text/css"> h1, h2, h3, h4, h5, h6 { color:gray; background: white; padding: 10px; border: 1px solid black; font-family: Verdana; } </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> <h4>This is heading 4</h4> <h5>This is heading 5</h5> <h6>This is heading 6</h6> </body> </html>
Combining selectors and declared grouping
We can By combining selector grouping and declaration grouping in rules, relatively complex styles can be defined with very few statements. The following rule specifies a complex style for all headings:h1, h2, h3, h4, h5, h6 { color:gray; background: white; padding: 10px; border: 1px solid black; font-family: Verdana; }
Combining selector and declaration grouping
We can combine selector grouping and declaration grouping in one rule, and we can use few statements to define relatively complex style. The following rule specifies a complex style for all headings:h1, h2, h3, h4, h5, h6 { color:gray; background: white; padding: 10px; border: 1px solid black; font-family: Verdana; }
The above is the detailed content of CSS tutorial: in-depth analysis of CSS selector grouping. For more information, please follow other related articles on the PHP Chinese website!