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

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?

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

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.