开发者

Why do /**/ comments work in stylesheets but // comments don't?

Is there a good reason for this? Lame question, but I just wondered if there was a rea开发者_StackOverflow中文版son why.


Because the specification allows for /**/ but not // :)

Seriously, though, CSS treats newlines like all other whitespace, and would not be able to determine the end of the comment without a terminating delimiter.


The syntax for comments in CSS is: /* comment here */

The // is not a valid syntax. I guess this allows CSS to work correctly when stripped from whitespace and new line characters during minification.


Because /* */ is the style that is defined for comments in CSS.

There are a lot of other ways to write comments in other environments that doesn't work in CSS, like:

//

<!-- -->

--

'

REM

{ }

;

#


Because the CSS language is defined so.


Different languages have different specifications with different functionality. In another language you may have comments that start with # instead of //.

See the specification.

4.1.9 Comments

Comments begin with the characters /* and end with the characters */. They may occur anywhere between tokens, and their contents have no influence on the rendering. Comments may not be nested.

CSS also allows the SGML comment delimiters (<!-- and -->) in certain places defined by the grammar, but they do not delimit CSS comments. They are permitted so that style rules appearing in an HTML source document (in the STYLE element) may be hidden from pre-HTML 3.2 user agents. See the HTML 4 specification ([HTML4]) for more information.

Note: There is no mention of comments that begin with 2 slashes and end at the line break. So that's why it's not supported.


If you want this style of comment (and a variety of other useful features that should have been in the CSS specification), try using a LESS CSS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜