28

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

leeand00
  • 24,318
  • 34
  • 129
  • 277
  • because they forgot it could be useful when they created it. – Michael Ekoka Mar 19 '10 at 17:37
  • 10
    Why isn't grass purple? – Dan Tao Mar 19 '10 at 17:39
  • 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
  • 17
    @Dan: Because grass cells contain Chlorophyl: http://en.wikipedia.org/wiki/Chlorophyl – Daniel Vassallo Mar 19 '10 at 17:41
  • 6
    @Dan, And green is more optimal than purple for obtaining energy from the sun. – jball Mar 19 '10 at 17:43
  • 1
    @Daniel heheh only on a programming site would someone have an answer to a question like that. – leeand00 Mar 19 '10 at 17:44
  • 1
    I guess we're nerds, eh? – jball Mar 19 '10 at 17:49
  • 2
    @Daniel Vassallo: OK, why isn't *Chlorophyll* purple? @jball: Your answer seems like it answers both of these questions, but only from a chemical standpoint, not a perceptual standpoint (not that I specified, to be fair). So: why is it that physical materials having such characteristics that they predominately reflect light falling within a particular range of frequencies in the electromagnetic spectrum *don't look purple* to us? Doesn't that seem like an arbitrary question to you? That's my point. Same with just about any question asking why something is *not* a certain way. – Dan Tao Mar 19 '10 at 19:45
  • P.S. I'm just jealous that you guys got to be nerds and I felt left out... – Dan Tao Mar 19 '10 at 19:52
  • @Daniel Vassallo: All the six-year-olds in the world beg to differ. – Dan Tao Mar 19 '10 at 20:55
  • @Dan: Well, grass may be green, but so are also strawberries - if you have a certain degree of color blindness... – awe Sep 03 '10 at 08:28
  • 3
    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

53

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,270
  • 9
  • 68
  • 91
  • 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
  • 1
    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
  • 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
  • 326,724
  • 71
  • 495
  • 436
  • @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
  • 666,277
  • 106
  • 705
  • 986
  • 2
    @Nate: Just trying to widen the perspective... @downvoter: If you don't explain what it is that you don't like, it's completely pointless. – Guffa Mar 19 '10 at 17:57
  • 1
    +1, one of the many reasons I feel that down-votes should be both *owned* and *explained* (I don't *care* -so much- if someone down-votes my answers/questions, so long as they give a reason to explain *why*). – David Thomas Mar 19 '10 at 18:05
  • # 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
  • @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
  • @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
2

Because the CSS language is defined so.

Tomas Aschan
  • 55,875
  • 53
  • 227
  • 386
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
  • 327,498
  • 120
  • 583
  • 623
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.

Kevin Sylvestre
  • 36,131
  • 31
  • 148
  • 228
  • 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