29

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • because they forgot it could be useful when they created it. – Michael Ekoka Mar 19 '10 at 17:37
  • 4
    @micheal or more likely, because they realized it wasn't that useful, and would require making newlines special. The two additional characters required for /**/ is not a great burden considering the flexiblity it gives the rest of the format. – jball Mar 19 '10 at 17:40
  • 4
    Despite the snark and "because it is" answers, this is a legitimate question. The existing syntax is quite cumbersome for inline comments, so it's reasonable to wonder if there's a rationale for not having an inline comment syntax. (And kudos to @jball for providing one.) – Jordan Gray Apr 16 '12 at 10:29

6 Answers6

54

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.

jball
  • 24,791
  • 9
  • 70
  • 92
  • 11
    +1 for mentioning the rationale. I had never considered that the `//` comment in C++ was an exception to 'whitespace neutrality' – egrunin Mar 19 '10 at 17:39
  • 3
    Tagalong: `//` comments in C outside of functions can cause serious compiler puke-age depending on your compiler. – rlb.usa Mar 19 '10 at 17:43
  • 2
    I'm not sure why this prevented the use of '//' comments. Most languages provide some exception to 'whitespace neutrality' and I don't see any major downside. – Kevin Sylvestre Mar 19 '10 at 17:48
  • @Kevin I could agree, but then I think of all the cases I've seen a xsl file end up with the entire thing on a single line. It seems alot of tools(maybe less now that they've matured more) used to discard your whitespace/returns and spit everything out on one line. – AaronLS Mar 19 '10 at 17:52
  • @Aaron Good point. I forgot about the huge movement to compress all web assets. – Kevin Sylvestre Mar 19 '10 at 17:55
  • 1
    Keep in mind that `//` comments are not an exception to whitespace neutrality. `//[^\n]*\n` **is** a whitespace in C++, and is treated as such by the lexer. I believe the actual reason is given by AaronLS - at the time when CSS was designed, the web infrastructure as a whole was not newline-friendly (as it had evolved along with newline-indifferent HTML), so reliance on the newline *character* for anything was risky at best. – Victor Nicollet Jan 11 '11 at 10:41
  • @Victor Nicollet - the `//` does require an exception to whitespace neutrality, since the `\n` is treated differently than all other whitespace characters in the comment. – jball Jan 11 '11 at 16:23
28

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.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • @Daniel That seems like a really good reason for @jbail's answer above. – leeand00 Mar 19 '10 at 18:32
  • 3
    Wait a minute though...you can minify Javascript and it supports the // method of commenting...*scratches head*..the js minifiers remove the comments... – leeand00 Mar 19 '10 at 18:33
  • @leeand00 You can specify the end of line with a semi-colon though. If you have your semi colon insertion wrong, it will break when minified. – alex Mar 20 '10 at 07:36
  • 6
    It would be surprising for CSS minification to be the rationale for a design decision that has no impact on minification whatsoever - for all purposes, comments are treated like whitespace and a minification process should remove them (or turn them to spaces) accordingly. Sure, there are IE fixes that rely on `/**/` being there, but a specific post-specification implementation bug is hardly taken into account in the pre-specification design phases ... – Victor Nicollet Jan 11 '11 at 10:37
14

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

{ }

;

#

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    # comments are supported in CSS (IE doesn't though). I use it when i want something in IE but not in other browsers. – Fr0zenFyr May 14 '13 at 11:47
  • 1
    @Fr0zenFyr: I can't see anything about # for comments in the spec: http://www.w3.org/TR/CSS2/syndata.html#comments – Guffa May 14 '13 at 13:28
  • @Guffa: I didn't mean it's officially documented anywhere, i was just sharing my personal experience. You can test it if you want. – Fr0zenFyr May 15 '13 at 11:06
  • 1
    @Fr0zenFyr: Mozilla doesn't have anything about it in the documentation either. It seems that it's not something that is supported, just a difference in how incorrect code is parsed. – Guffa May 15 '13 at 12:57
  • You are right @Guffa, it's not officially documented anywhere. It just works, maybe a bug...but a useful bug sometimes. – Fr0zenFyr May 16 '13 at 12:12
  • 1
    Regardless of the character used, many languages support single-line comments. – Tulains Córdova Aug 23 '16 at 11:51
  • @Fr0zenFyr: Re "It just works": It is undefined behaviour. Anything could happen, incl. sending an email to the moon. – Peter Mortensen Jan 02 '23 at 21:58
2

Because the CSS language is defined so.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
2

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.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Re *"comments that start with # instead of //."*: That would include [Perl](https://en.wikipedia.org/wiki/Perl), [PHP](https://en.wikipedia.org/wiki/PHP), [Bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29), and [Python](https://en.wikipedia.org/wiki/Python_%28programming_language%29). – Peter Mortensen Jan 02 '23 at 22:11
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
  • 1
    I would say that the possibility to use `//` as comment is not a reason to start using `LESS CSS`. But it looks like it has a lot of other useful features that would be very good reasons for using it.. – awe Sep 03 '10 at 08:22