Why Don't We Have a // Comment in CSS

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

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.

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.

CSS is getting apply despite of comments??

Because you're using the wrong comments syntax, you should be using: /* ... */ to comment out CSS, the <!-- ... --> is html comment syntax; and once you're inside of the style element html is itself invalid and can, therefore, be handled by the browser as it sees fit.

Therefore you may want to try:

<style type="text/css">
/*
body { }
...

*/
</style>

It's worth noting that, like html, there is no single-line comment syntax, which in JavaScript for example would be anything following //.

Some further information: CSS - smarter code comments

compass removes css comments in compiled css

Compass basically uses the SASS settings for comments and output style which are documented here

Basically, it should leave in your block comments, but need to make sure you're not using the compressed output style, otherwise everything will be removed anyway.

If you're not using compressed and your block comments are still being removed, it might be because they're still declared in-line (I'm not sure what behaviour compass has in that regard). Try moving them to the line before like this:

/*!YUI compressor*/
@media all and (max-width: 480px) {}

The line_comments option just outputs comments before each selector saying where that rule is defined in your SCSS file (as long as your not using compressed) , it was useful before compass supported source-maps

Sass: Why can't I have a comment after an @import (on the same line)?

  1. The CSS spec defines the @import rule as

    <import-rule> = @import [ <url> | <string> ] <media-query-list>? ;

    which means @import followed by a string or a url followed by an optional list of media types

  2. In Sass syntax, comments must be on their own line

    Like everything else in the indented syntax, comments are line-based.
    This means that they don’t work the same way as in SCSS. They must
    take up an entire line, and they also encompass all text nested
    beneath them.

The parser expects a list of media types, finds a comment mixed with content and chokes on it.

You will have to split the comment and the content in Sass syntax:

 // Bar
@import "foo"

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.

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



Related Topics



Leave a reply



Submit