Why do /**/ comments work in stylesheets but // co

2019-01-23 23:48发布

Is there a good reason for this? Lame question, but I just wondered if there was a reason why.

6条回答
做自己的国王
2楼-- · 2019-01-24 00:16

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.

查看更多
相关推荐>>
3楼-- · 2019-01-24 00:22

Because the CSS language is defined so.

查看更多
Melony?
4楼-- · 2019-01-24 00:24

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.

查看更多
混吃等死
5楼-- · 2019-01-24 00:26

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.

查看更多
Emotional °昔
6楼-- · 2019-01-24 00:29

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

{ }

;

#

查看更多
来,给爷笑一个
7楼-- · 2019-01-24 00:34

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.

查看更多
登录 后发表回答