search
HomeWeb Front-endCSS TutorialCSS tutorial: in-depth analysis of CSS selector grouping

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

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

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 id="这是-nbsp-heading-nbsp">这是 heading 1</h1>
<h2 id="这是-nbsp-heading-nbsp">这是 heading 2</h2>
<h3 id="这是-nbsp-heading-nbsp">这是 heading 3</h3>
<h4 id="这是-nbsp-heading-nbsp">这是 heading 4</h4>
<p>这是一段<b>普通</b>的段落文本。</p>
</body>
</html>

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

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

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

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 id="This-nbsp-is-nbsp-heading-nbsp">This is heading 1</h1>
<h2 id="This-nbsp-is-nbsp-heading-nbsp">This is heading 2</h2>
<h3 id="This-nbsp-is-nbsp-heading-nbsp">This is heading 3</h3>
<h4 id="This-nbsp-is-nbsp-heading-nbsp">This is heading 4</h4>
<h5 id="This-nbsp-is-nbsp-heading-nbsp">This is heading 5</h5>
<h6 id="This-nbsp-is-nbsp-heading-nbsp">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;
  }

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

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
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
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.