23

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 //?

Community
  • 1
  • 1
Wabbitseason
  • 5,450
  • 9
  • 47
  • 60
  • It's a reasonable question to ask. I don't know if there's a good answer to be had though. – Spudley Jan 11 '11 at 10:23
  • 7
    This question is answered here: http://stackoverflow.com/questions/2479351/why-do-comments-work-in-stylesheets-but-comments-dont. And, there appears to be a "real" answer: minification. – Spiny Norman Jan 11 '11 at 10:23
  • @Cody Gray - Well I think the point is that there is no way to define a 'non-terminated' comment. Which is a good question. – UpTheCreek Jan 11 '11 at 10:31
  • 1
    There's a "C", but not a "C++", in CSS. So naturally it'd support C-style multi-line comments and not C++ single-line comments :) – BoltClock Jan 11 '11 at 10:31
  • 4
    @Cody Gray I don't agree. `//`-comments are very handy, as the poster said, because they don't interfere with other comments. Also, they are easier to write. And, all other languages that use `/* ... */`-comments that I'm aware of also have `//`-comments. – Spiny Norman Jan 11 '11 at 10:32
  • @BoltClock C doesn't have `//`-comments? I must have blocked that out at some point. Also, good random point about C being in CSS :) – Spiny Norman Jan 11 '11 at 10:34
  • @UpTheCreek: There's no official multi-line comment syntax in Perl, either. Is that a good question? Features aren't implemented by default. The only thing you can do is speculate as to why a design decision was made, and I don't really see the merit in that. Especially since it's an unresolvable discussion which isn't appropriate for SO. – Cody Gray Jan 11 '11 at 10:35
  • @Spiny Norman: Yeah, it doesn't. – BoltClock Jan 11 '11 at 10:37
  • 1
    @Spiny Norman - The minification point is a very good argument for keeping things as they are, but minification wasn't an issue when the CSS spec was originally conceived, so that doesn't explain the original design decision. (the same argument could be made for Javascript, but that does support // comments) – Spudley Jan 11 '11 at 10:50
  • 1
    @Spudley Yeah, I agree. I think it's more about CSS not distinguishing between newlines and other whitespace. See also my answer, below. – Spiny Norman Jan 11 '11 at 10:56

3 Answers3

16

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?.

Community
  • 1
  • 1
Spiny Norman
  • 8,152
  • 1
  • 32
  • 54
5

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.

tdammers
  • 19,940
  • 1
  • 35
  • 54
2

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!

jonepatr
  • 7,738
  • 7
  • 28
  • 53