Why Do /**/ Comments Work in Stylesheets But // Comments Don'T

Why do /**/ comments work in stylesheets but // comments don't?

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.

Why don't we have a // comment in CSS?

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

Why does ! -- comment out a style rule, but !-- does not?

CSS doesn't accept HTML <!-- comment here --> comments. Instead CSS uses /* comment */ to comment lines. Try that instead.

Commenting internal CSS code is not working

Use /* */, not <!-- -->.

The second type is HTML, not CSS. The use of HTML comments is a hack having to do with some Microsoft browser and does not work as you might expect it to do.

Older answer

Is it bad practice to prefix single lines of CSS with // as a personal comment style?

I see that there were/are lots of people complaining about this and since this is an older question, there is probably a lot of people reading it wondering if it is still true, or if there is actually a standard in the first place. Allow me to clear the air. The following are the core reasons for the strict CSS comment policy:

#1 It is not standard

Standardized at least since CSS 2.1, comments are to ONLY be encased in /* and */. While some browsers tolerate //, they aren't supposed to, and are only one inch from someone saying "oh yeah that's non-standard" or "hey! That's non-standard, fix it!"; and then guess what, your CSS code, which WAS working, now doesn't for thousands of people (and may have already not been working for hundreds of others). I will add that <!-- and --> are allowed but only (and I mean ONLY) when they appear within an HTML document, not in a .css source file. If your browser is so old that it can't skip over <style> tags, it's probably time for a new browser 10 years ago. Even Lynx and other text browsers know not to read them, so commenting it out is only useful in very isolated situation where hardware and software are landlocked in their current working state.

#2 It is not (very) cross-platform friendly

The single line comment, which starts anywhere on a line with //, is terminated by 'newline' which is/are not a cross-platform standardized character(s). Worse, some might have one character for a newline, or 2... and when these platforms mix together, a newline could be lost, and there goes your terminator...and some or all of your code is now being commented out that was not supposed to be, you don't have to be a genius to know what the consequences of that might be, especially if you control features of your site solely through CSS which many do.

#3 The Standard IS Friendly and Uniform to All

The /* and */ delimiters are ALWAYS going to be the same characters on EVERY computer regardless of architecture, operating system, etc.

#4 Newlines are Whitespaces

The last reason (yes, there is one more), newline character(s) are considered (in CSS and many other languages) to be whitespace, and */ is not whitespace is it? And if you think about it at this point, it should be pretty clear you should NOT be using a whitespace to terminate a comment especially since whitespace is and can be stripped by many HTML/CSS parsers, or reformatted without you even knowing about it.

#5 CSS != C/C++

Now if you are about to fly out of your seat and yell at me about "Hey, but C++...", remember those compilers and IDEs have tons of newline checking and detection built into them so they can take it. Most compilers do not reformat your code unless asked, and many IDEs will usually ask you what kind of newlines your document is using if it can't guess on its own. If we did this with CSS pages for the end user every time one was loaded, imagine the nightmare it would be trying to get around. Furthermore, C/C++ code is not parsed at run-time and is compiled, so then much of the time, the user never gets the document in question in the first place. The source files are not being constantly viewed by the entire world on hundreds of platforms and many operating systems, and a million different browsers either. The comments are stripped out before they ever get to the end user. CSS source comes right to the user's browser and has to be very resilient not knowing what is on the other side, so the caveat is that it must be ready for anything the end user has or does, not anything the developer does or has!

#6 It's inconvenient

No, it is very annoying having to type that extra */, but the blame for this mainly goes to developers of CSS editing software who don't offer auto completion. If you use a specialized editor that can do that, preferably out of the box, then you'll find it is just as easy as using //. Get in the habit of typing /**/ and then backspace 2, it will help you to not forget and makes it a little easier. Better still, you can set up a hot key to just lay down those for you. Windows and Linux both have powerful tools to allow this (KDE is very good for that).


I hope this helps everyone understand the "why" behind the "how", and remember just because something works for you, doesn't mean it is the standard, and to sum up:

YES, IT IS BAD PRACTICE to use it, just say NO to the double slash!!!
If you need a visual aid to remind you of this important fact, just burn this image into your mind (thanks to those of you who have nothing better to do but make pictures like this):

No double slash


PS: If you really want something to complain to the ones making/breaking CSS standards (W3C, elbow), someone starts a discussion about how unnecessarily long and wrong the "!important" keyword is! But that is not part of this question so I won't go into it.

References

  • W3C: CSS 2.1 working draft: comment characters.
  • W3C: CSS syntax module level 3: railroad diagrams of parser-to-character interpretations.
  • Stack Overflow: Various Stack Overflow articles with practically the same subject as this one.
  • w3schools: CSS 3 syntax standard (which in turn references W3C).
  • sitepoint: CSS syntax annotation on "not using double-slash".
  • mozilla|mdn: relaxed CSS 3 processing allows double slash in input files.

CSS Nested Comments

CSS does not have a nestable comment syntax.

You could instead wrap the rules in something which does nest, but will not match anything, such as a non-existent media type:

@media DISABLED {
#container {
width: 90%; /* nested comment here */
margin: 0 auto;
}

#container .class {
width: 25%;
}
}

This does have the caveat that it will not be obviously a comment (so it will not be removed by tools that remove comments), and will cause the browser to spend time on parsing it and finding that it doesn't match. However, neither of these should be a problem for temporary development use, which is the usual reason one would want to comment out a large chunk simply.

Can comments in HTML and CSS ever create rendering problems?

The comments you have shown wouldn't cause any display issues but if you do:

<!-- My comment ---------- -->

You can run into problems with Firefox. This is because two dashes signal the end of the comment and the > is the end comment marker. So technically this is valid:

<!-- My -- comment >

So what can happen is that your comments aren't ending where you think they should and so some content remained commented out, or the comment displays on the page.

More detail can be found here:

http://weblog.200ok.com.au/2008/01/dashing-into-trouble-why-html-comments.html

How reliable is double-slash comment in CSS

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 /* */.

Commenting in Dynamic CSS

If you want the comments to appear in the generated CSS, then use CSS comments, like this:

echo "/* My css comment */";
echo "body { margin: 0 }";

If you don't want them to be visible in the generated CSS and only want to be able to read them when editing the PHP file, then use regular PHP comments:

// Just a regular PHP comment, this won't be visible in the generated CSS!
echo "body { margin: 0 }";
/* You can use this style of comments too, it's up to you what you prefer. */


Related Topics



Leave a reply



Submit