To fill in a little bit of everything that’s already been said, and hopefully not creating any redundancy of information, the big reason why we can’t break the rule when it comes to WEB development, particularly with HTML, CSS and Javascript, is that the code we create will be interpreted by a browser, this browser that is development only and exclusively on the standards created for each language.
Not enough the various problems we have with the development of code that will behave in the same way in the different browsers, if we develop code that works by a "mere chance", the thing becomes even more complicated.
Examples
Based on the code of your question, the //
to flag a comment follow 3 practical cases, where the first one is just to highlight the correct way to identify a comment in CSS so that the browser knows what to do, the other two examples being an illustration of potential problems:
Example 01 - Correct Use
A scenario with comments within what the browsers expect to receive because the standard so says, where we can observe various methods to apply comments and all of them valid:
/* texto de aviso grave */
p {
color: red; /* vermelho */
text-transform: uppercase;
}
/*
* A partir daqui seguem estilos para a conta do utilizador
* -------------------------------------------------------- */
p {
font-size: 2em;
}
<p>Super BuBu</p>
Example 02 - Ignored Statement
Comment incorrectly that will cause the browser to ignore the statement, because according to CSS standards any error will result in the ignored statement.
In the example, the browser will ignore everything from the //
until the close of the declaration which is identified by the character }
:
// texto de aviso grave
p {
color: red; /* vermelho */
text-transform: uppercase;
}
/*
* A partir daqui seguem estilos para a conta do utilizador
* -------------------------------------------------------- */
p {
font-size: 2em;
}
<p>Super BuBu</p>
Example 03 - CSS compression, ignored statement and invalid comment is in the resulting code
Assuming I used server-side code to get all the CSS in one line, which also takes comments, a technique known as compression.
As at the beginning I have a comment with //
, the same will stay in the code after compressed, and I will still lose the first statement, this because the CSS compressor will not collect the //
as the identification of a comment:
If you pass the CSS of example #2 by this tool, Online CSS Minifier/Compressor, the result is what we can see in the example below:
// texto de aviso grave p{color:red;text-transform:uppercase}p{font-size:2em}
<p>Super BuBu</p>
Considering the mustache’s answer, isn’t the question answered? It showed what problems can occur if you use this trick even at the beginning of the line.
– bfavaretto
I understood the question and the answer was given to all the people who will read here. That’s how you make a website Q&A, we respond to help everyone and not only to those who ask. But if understanding is wrong, you can say what you expected. I said it’s wrong because it’s not in the specification and may give you trouble in the future. Your brother :P gave an example of how to use properly and some more precaution to take and the bfavaretto showed that there is a different case than what you posted that gives problem. This is how knowledge is built in a collaborative way.
– Maniero
@bigown yes, I believe I expected something more interesting to answer, but I saw that your answer seemed to be the most explanatory and seemed to me the most correct too. Thank you for your attention. :)
– Rigotti