CSS tutorial: in-depth analysis of CSS selector grouping

无忌哥哥
Release: 2018-07-12 14:49:17
Original
1393 people have browsed it

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;}

Place the h2 and p selectors in the rule left, then separated by commas, defines a rule. The style to the right (color:gray;) will be applied to the elements referenced by these two selectors. The 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 make many elements 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, resulting in a more concise style sheet.

The following two sets of rules will achieve the same result, but it's clear 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;}
Copy after login

The grouping provides some interesting choices. For example, all rule groupings in the following example are equivalent, each group just shows a different way of grouping 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;}
Copy after login

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>
Copy after login

This declaration is equivalent to a grouping selector that lists all elements in the document. With a wildcard selector, a single keystroke (just an asterisk) allows all elements in the document to have a color attribute value of red.

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;}
Copy after login

But the efficiency of the above approach Not high. This is especially troublesome when we create such a list for an element with multiple styles. Instead, we can group declarations together:

h1 {font: 28px Verdana; color: white; background: black;}

This is consistent with the previous 3 lines Style sheets have exactly the same effect.

Note that when grouping statements, it is important to use a semicolon at the end of each statement. Browsers ignore whitespace in style sheets. As long as you add a semicolon, you can create a style in the following format without any scruples: How about

h1 {
  font: 28px Verdana;
  color: blue;
  background: red;
  }
Copy after login

, is the above way of writing more readable?

However, if the second semicolon is omitted, the user agent will interpret the style sheet as follows:

h1 {
  font: 28px Verdana;
  color: blue background: red;
  }
Copy after login

Because background is not a legal value for color, and because it can only Specify a keyword for color, so the user agent will completely ignore the color declaration (including the background: black part). This way the h1 heading will only appear blue without a red background, but it's more likely that you won't get a blue h1 at all. Instead, these headers simply appear in the default color (usually black) and have no background color at all. font: 28px The Verdana declaration still works because it does correctly end with a semicolon.

Like selector grouping, declaration grouping is a convenient way to shorten your stylesheet, making it clearer and easier to maintain.

Tip: It is a good practice to also add a semicolon after the last statement of the rule. When adding another declaration to a rule, you don't have to worry about forgetting to insert another semicolon.

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>
Copy after login

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;
  }
Copy after login
Copy after login

The above rule defines the style for all headings as gray text with a white background and its padding is 10 pixels with a 1 pixel solid border and the text font is 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;
  }
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!