Home Web Front-end CSS Tutorial CSS tutorial: in-depth analysis of CSS selector grouping

CSS tutorial: in-depth analysis of CSS selector grouping

Jul 12, 2018 pm 02:49 PM
css Selector

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles