Recently I found that there is, possibly, a new way of commenting in HTML5.
Instead of the typical <!-- -->
multi-line commenting I've read about, I thought I noticed that my IDE made a regular <!div >
commented out. So I tested it out, and to my surprise Chrome had commented out that tag. It only commented out the tag and not the contents of the div, so I had to comment out the closer <!/div>
to avoid closing other divs.
I tested another and it appears that generally putting an exclamation marker in front of the opening of any tag, this symbol <
, makes that tag commented out.
Is this actually new? Is it bad practice? It is actually very convenient, but is it practical yet(if not new)?
Edit extra details: Although a syntax error or misinterpretations of this particular syntax is a good reason, how come Chrome actually renders them as full comments?
The code is written as:
<!div displayed> some text here that is still displayed <!/div>
And then it is rendered as:
<!--div displayed--> some text here that is still displayed <!--/div-->
I don't think this is a good habit to take since
<!
stands for markup declarations like<!DOCTYPE
. Thus you think it's commented (well... browser will try to interpret it).Even if it doesn't appear, this seems not to be the correct syntax for commenting HTML code.
There is no new standard for comments in HTML5. The only valid comment syntax is still
<!-- -->
. From section 8.1.6 of W3C HTML5:The
<!
syntax originates in SGML DTD markup, which is not part of HTML5. In HTML5, it is reserved for comments, CDATA sections, and the DOCTYPE declaration. Therefore whether this alternative is bad practice depends on whether you consider the use of (or worse, the dependence on) obsolete markup to be bad practice.Validator.nu calls what you have a "Bogus comment." — which means that it's treated like a comment even though it's not a valid comment. This is presumably for backward compatibility with pre-HTML5, which was SGML-based, and had markup declarations that took the form
<!FOO>
, so I wouldn't call this new. The reason they're treated like comments is because SGML markup declarations were special declarations not meant to be rendered, but since they are meaningless in HTML5 (with the above exceptions), as far as the HTML5 DOM is concerned they are nothing more than comments.The following steps within section 8.2.4 lead to this conclusion, which Chrome appears to be following to the letter:
8.2.4.1 Data state:
8.2.4.8 Tag open state:
8.2.4.45 Markup declaration open state:
Notice that it says to switch to the comment start state only if the sequence of characters encountered is
<!--
, otherwise it's a bogus comment. This reflects what is stated in section 8.1.6 above.8.2.4.44 Bogus comment state:
In plain English, this turns
<!div displayed>
into<!--div displayed-->
and<!/div>
into<!--/div-->
, exactly as described in the question.On a final note, you can probably expect other HTML5-compliant parsers to behave the same as Chrome.