Why don't we have a // comment in CSS? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-08 14:34:14

问题


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

In CSS there is only one way to create comments: /* I'm a comment */

The problem is that it isn't nestable.

Does anyone know why we don't have an alternative comment syntax like //?


回答1:


I think the real answer is that CSS treats newlines like any other whitespace, so it wouldn't make sense to have comments that are terminated by a newline. This is from the CSS1 spec: http://www.w3.org/TR/REC-CSS1

A CSS style sheet, for any version of CSS, consists of a list of statements. There are two kinds of statements: at-rules and rulesets. There may be whitespace (spaces, tabs, newlines) around the statements.

Of course, this also makes a lot of sense in the context of minification, as mentioned here: Why do /**/ comments work in stylesheets but // comments don't?.




回答2:


It's not in the spec, and because CSS is widely used and supported, adding it in is virtually impossible. You can't just publish a new spec and expect all browsers to magically support it. IE6, a browser more than 10 years old, is still widely used, so you can safely assume that even if this addition to the spec were made, it'd take another 10 years to be supported enough to bother. The problem with //-style comments is that they don't scale - unlike new HTML tags, which can be safely ignored as long as the rest of the document makes sense, adding a //-comment will break unaware user agents.

So the short answer is, we don't have it because we don't.

If it really means that much to you, write a script or macro that converts //-comments into /* */-comments, and apply it before running your web app.




回答3:


There is a way to have // comments in css. If you use SASS/compass. I really like using compass because it gives me everything I miss about css, like funktions, variables and so on..

Here is a link to compass http://compass-style.org/ and to the underlying SASS-language http://sass-lang.com/

Compass is very nice because you just have a program running in the background that compiles your SASS-code into real css, so your workflow is exactly as normal but in other files (scss or sass) and with very extended functionality!



来源:https://stackoverflow.com/questions/4656546/why-dont-we-have-a-comment-in-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!