This article mainly introduces the CSS selector grouping related data, the need for friends can refer to the following
Selector grouping
Suppose you want H2 elements and paragraphs to be gray. The easiest way to achieve this is to use the following statement:
h2, p {color:gray;}
A rule is defined by placing the H2 and P selectors on the left side of the rule and separating them with commas. Its right-hand style (color:gray;) is applied to the elements referenced by these two selectors. A comma tells the browser that the rule contains two different selectors. Without this comma, the meaning of the rule would be completely different. See descendant selector.
You can group any number of selectors together without any restrictions.
For example, if you want to display many elements as gray, you can use a rule similar to the following:
body, h2, p, table, th, td, pre, strong, em {color:gray;}
Tip: By grouping, authors can "compress" certain types of styles together, so that a more concise style sheet can be obtained.
The following two sets of rules can achieve the same result, but it is clear to see which one is easier to write:
/* 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;}
Grouping offers some interesting options. For example, all rule groupings in the following example are equivalent, and each group simply shows the different ways to group selectors and declarations:
/* 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-a wildcard selector (Universal selector), which is displayed as an asterisk (*). The selector can match any element, as if it were a wildcard character.
For example, the following rules can make each element in a document red:
* {color:red;}
This declaration is equivalent to a group selector that lists all the elements in the document. With the wildcard selector, you can specify the Color property value of all elements in the document as red by tapping the key once (only one asterisk).
Claim grouping
We can group selectors as well as groups of claims.
Suppose you want all H1 elements to have a red background, and use a 28-pixel high Verdana font to display as blue text, you can write the following style:
h1 {font:28px Verdana;} h1 {Color:blue;} h1 {background:red;}
But the above approach is inefficient. This is especially problematic when we create such a list for an element that has more than one style. Instead, we can group the declarations together:
h1 {font: 28px Verdana; color: white; background: black;}
This works exactly like the previous 3-row style sheet.
Note that it is important to group the declarations by using semicolons at the end of each declaration. The browser ignores whitespace characters in the style sheet. As long as you add a semicolon, you can create a style with no scruples in the following format:
H1 { font:28px Verdana; Color:blue; background:red; }
How about the readability of the above-mentioned wording is not stronger.
However, if you omit the second semicolon, the user agent interprets the style sheet as follows:
H1 { font:28px Verdana; Color:blue background:red; }
Because background is not a valid value for color, and because only one keyword can be specified for color, the user agent completely ignores the color declaration (including the Background:black section). This way the H1 title will only appear blue, without a red background, but it is more likely that there is no blue H1 at all. Instead, the headings appear as default colors (usually black) and have no background color at all. The font:28px Verdana statement still works as it does, because it does correctly end with a semicolon.
As with selector groupings, declaring grouping is also a convenient way to shorten stylesheets, make them clearer, and be easier to maintain.
Tip: It's a good practice to add a semicolon after the last declaration of a rule. When you add another statement to a rule, you don't have to worry about forgetting to insert a semicolon.
Grouping with selectors and declarations
We can combine selectors grouping and declaring groupings in one rule, and you can use very few statements to define relatively complex styles.
The following rules specify 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; }
Grouping with selectors and declarations
We can combine selectors grouping and declaring groupings in one rule, and you can use very few statements to define relatively complex styles.
The following rules specify 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 rule defines the style of all headings as gray text with a white background, with a padding of 10 pixels and a solid border of 1 pixels, and the text font is Verdana.
Grouping with selectors and declarations
We can combine selectors grouping and declaring groupings in one rule, and you can use very few statements to define relatively complex styles.
The following rules specify 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; }