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.

Why don't we have an alternative comment syntax like //?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wabbitseason
  • 5,641
  • 9
  • 49
  • 60
  • 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
  • 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
  • 1
    @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 - on strike 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
  • 2
    @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,277
  • 1
  • 30
  • 55
5

It's not in the specification, and because CSS is widely used and supported, adding it in is virtually impossible. You can't just publish a new specification and expect all browsers to magically support it.

Internet Explorer 6, a browser more than 10 years old, is still widely used, so you can safely assume that even if this addition to the specification 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 application.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tdammers
  • 20,353
  • 1
  • 39
  • 56
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 functions, variables and so on...

Here is the Compass home page and the underlying Sass-language.

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jonepatr
  • 7,769
  • 7
  • 30
  • 53