-1

How reliable is double-slash for a single-line comment in CSS? What’s the experience of using single comments like this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kalabalik
  • 99
  • 1
  • 10
  • 4
    mmh not reliable at all? css comment is `/* comment */` – Rafalon Aug 14 '18 at 14:13
  • 4
    0%. It's not valid CSS comment syntax at all. https://developer.mozilla.org/en-US/docs/Web/CSS/Comments – j08691 Aug 14 '18 at 14:14
  • Yes I know the CSS syntax, thats not what i was asking for. The fact that // working as single-line comment in CSS, but can you rely on browsers to understand that´s a comment... – Kalabalik Aug 14 '18 at 14:16
  • 3
    No, `//` doesn't work as a CSS comment. Period. All you're seeing is the browser failing to understand what you've entered. It's being treated as a mistake, not a comment, the same way `xyzbackground: red;` would be ignored. – j08691 Aug 14 '18 at 14:19
  • What an unusual question. – Turnip Aug 14 '18 at 14:21
  • Aactually @j08691 the double slash will not be treaded the same way as the misspell you mentioned. Not in browser dev-console anyway... It will be treated as a comment as far as I can see? – Kalabalik Aug 14 '18 at 14:26
  • Thanks @Turnip for your useful input!. – Kalabalik Aug 14 '18 at 14:39
  • This is a mega duplicate. What is the canonical question? – Peter Mortensen Dec 31 '22 at 01:24
  • A candidate (2010): *[Why do /**/ comments work in stylesheets but // comments don't?](https://stackoverflow.com/questions/2479351/why-do-comments-work-in-stylesheets-but-comments-dont)* – Peter Mortensen Dec 31 '22 at 01:30

1 Answers1

10

Comments using double slashes // are invalid in CSS. The CSS spec states only the following about comments:

4.3.2. Consume comments

This section describes how to consume comments from a stream of code points. It returns nothing.

If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A ASTERISK (), consume them and all following code points up to and including the first U+002A ASTERISK () followed by a U+002F SOLIDUS (/), or up to an EOF code point. Return to the start of this step.

If the preceding paragraph ended by consuming an EOF code point, this is a parse error.

Return nothing.

In other words, only /* */ are valid comments, it does not mention //

However, // are valid in certain CSS processors such as Less and SASS.


Per your comment:

...can you rely on browsers to understand that´s a comment

No, the browser will attempt to interpret the syntax anyway and likely fail the rule based on it being a syntax error rather than it being a comment. The result will likely fail based on browser, but using it brings you into undefined behavior.

Browser Behavior with Double Slash Comments

Here are the results of the following rules being applied in different browsers. One styling uses the double slash at the beginning of the property, and one has the // right before the value.

#some {
    width: 500px;
    /*height: 400px;*/
    //color: blue;
    background-color: //red;
}

Firefox

Firefox Elements Panel

In Firefox ESR 52.9.0, you get a little yellow warning triangle next to color and background-color because //color is an invald CSS property and because //red is an invalid background-color value.

Chrome

Chrome Elements Panel

Interestingly, in Chrome 68.0.3440.106, I don't even see the //color: blue show up in the elements panel which might mean that Chrome tries to consider the line a comment, but since // being comments is not in the spec, you should not rely on it. However, background-color also has the warning since //red is an invalid value.

Safari

Safari Elements Panel

Safari 11.1.2 has the same behavior as Chrome where the // led property is not even listed and the // led value is a syntax error.

Internet Explorer 11

IE11 Elements Panel

Internet Explorer 11.0.9600.19080 considers the entirety of //color: blue to be the rule property and believes it has no value as though you had written //color: blue: ;. It also lists background-color: //red but considers it an error and does not apply it.


It should also be noted that for the following:

#some {
    // width: 400px;
    /* height: 400px; */
}

Most of the browsers will at least acknowledge the /* */ property and allow you to toggle it in the Dev tools. For Chrome and Safari, the // led rule isn't even listed meaning you can't toggle it as you could with the /* */.

zero298
  • 25,467
  • 10
  • 75
  • 100
  • Thank you, as you say chrome seems to handle it as a proper comment. I know thats is not proper css-syntax, and I´m not using today. – Kalabalik Aug 14 '18 at 14:34
  • 1
    @Kalabalik I didn't say that, I said that it doesn't even acknowledge the `//` led property in the panel. It will at least list `/* */` properties in the panel and allow you to toggle them. I would really advise against using `//`. – zero298 Aug 14 '18 at 15:09