Pseudo-comments in CSS (Or, How Browsers Parse Styles)
Although the CSS specification is not mentioned, there are some ways you can simulate C-style and/or Unix-style line comments in CSS files (with some limitations). Others have written about this before (particularly the CSS annotations are covered in SitePoint's Web Foundations article). This article will discuss this in more detail.
Key Points
- CSS officially only supports C-style multi-line comments, but pseudo comments use parsing errors to inadvertently comment out the code.
- Pseudo comments can be created by malforming the CSS declaration, such as omitting semicolons or using unrecognized attribute names, resulting in subsequent code being ignored.
- Inline and next line placement of pseudo comments will affect whether subsequent CSS rules are applied, and inline pseudo comments may invalidate subsequent declarations on the same line.
- Pseudo-annotations can also be applied to @ rules, and the observed behavior will vary depending on whether the @ rules contain a body block or end with a semicolon.
- Although pseudo comments can be used for debugging, they are poorly readable and should not replace standard CSS comments in production code.
CSS Comments
According to the specification, the CSS parser formally supports an annotation style, i.e., multi-line comments from C-style language, which uses the start mark /*
and the end mark */
as shown below:
<code>/* 起始和结束标记之间(包括起始和结束标记)的字符将被解析器忽略, */</code>
Therefore, the rule declaration in the comment will be ignored:
<code>body { background: red; /* background: white; */ }</code>
The block declaration in the comment will be ignored:
<code>/* body { background: red; } */</code>
In these examples, we all intentionally use comment syntax to instruct the parser to ignore content.
However, we may also do this unexpectedly, such as using a malformed statement:
<code>body { background: red /* 缺少分号 */ background: blue; }</code>
In this example, neither background declarations are applied due to the lack of a semicolon. The parser scans the next semicolon to determine that the entire two-line statement is incorrect, so the content of the entire lexical analysis is ignored. The same thing will happen if we omit the attribute value altogether:
<code>body { background: background: blue; /* 此声明未应用 */ }</code>
This indicates that we can use a malformed statement as...
Pseudo Comment
We call these "pseudo comments" because strictly speaking, these are not comments that terminate at the end of the line character. Instead, they work by malforming subsequent inputs (even on subsequent lines). This is due to the error handling process of rule sets, declaration blocks and selectors:
"If there is an error anywhere in the selector, the entire statement should be ignored, even if the rest of the selector looks reasonable in CSS 2.1."In the following example (excerpt from the specification), the second rule set is ignored due to the invalid character "&" in the selector:
<code>h1, h2 {color: green } h3, h4 & h5 {color: red } /* h6 {color: black }</code>
<code>body { background: red; xbackground: white; /* 属性名称未被识别 */ y background: blue; /* 属性名称格式不正确 */ }</code>
<code>/* 起始和结束标记之间(包括起始和结束标记)的字符将被解析器忽略, */</code>
However, do not use any characters, but stick to the C and Unix conventions and use # or //:
<code>body { background: red; /* background: white; */ }</code>
Semi-colon
The semicolon is the end mark of the rule declaration. Therefore, they cannot "comment" the text that follows. In specification, the parser treats a dangling semicolon as a malformed declaration (a declaration of name, colon, or value is missing).
As mentioned earlier, when the regular multiline comment format is erroneous, i.e. when the start and end tags are not balanced around the rule set or declaration, the parser ignores subsequent declarations or rulesets. The following actually "commented" thetwo background declarations, because the parser will search for the next declaration end tag (semi-colon) of the affected declaration:
<code>/* body { background: red; } */</code>
<code>body { background: red /* 缺少分号 */ background: blue; }</code>
<code>body { background: background: blue; /* 此声明未应用 */ }</code>
<code>h1, h2 {color: green } h3, h4 & h5 {color: red } /* h6 {color: black }</code>
Inline with the next line
This is where "pseudo" enters the word "pseudo comment". This may be a good reason not to call it "comments" at all, because they violate the end-of-line convention of C or Unix style line comments.Pseudo comments placed on one line will suppress declarations on the next line. In the following example, the background will be blue:
<code>body { background: red; xbackground: white; /* 属性名称未被识别 */ y background: blue; /* 属性名称格式不正确 */ }</code>
are suppressed by . In the following example, the background will be white instead of blue:
<code>selector { ~ property-name: ignored; ` property-name: ignored; ! property-name: ignored; @ property-name: ignored; # property-name: ignored; $ property-name: ignored; % property-name: ignored; ^ property-name: ignored; & property-name: ignored; * property-name: ignored; _ property-name: ignored; - property-name: ignored; + property-name: ignored; = property-name: ignored; | property-name: ignored; \ property-name: ignored; : property-name: ignored; property-name: ignored; . property-name: ignored; > property-name: ignored; , property-name: ignored; ? property-name: ignored; / property-name: ignored; }</code>
<code>// background: ignored; # background: ignored;</code>
(Same as follow-up content. Due to space limitations, the remaining pseudo-original creations of the remaining part are omitted here.)
The above is the detailed content of Pseudo-comments in CSS (Or, How Browsers Parse Styles). For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Two articles published the exact same day:

GooFonts is a side project signed by a developer-wife and a designer-husband, both of them big fans of typography. We’ve been tagging Google

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

The first part of this two-part series detailed how we can get a two-thumb slider. Now we'll look at a general multi-thumb case, but with a different and

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:
