Are HTML tags / comments auto-corrected by browsers?




I used



... and it works.



source to share

2 answers

Modern browser parsers (i.e. those using the HTML5 parsing algorithm) work as follows. If they are expecting the next text or new tag and they see <!

, then they check the next few characters to see if they are --

either DOCTYPE

or, if they handle inline SVG or MathML [CDATA[

, (See /html5/spec/tokenization.html#markup-declaration-open-state )

If, as is the case <!- foo

, none of them match, then the parser enters a dummy comment state , where all the characters following, up to the next >

, are read and converted into a comment that should be placed in the DOM.

Hence, the behavior you see with the help <!-

works like the start of a comment. Note that this behavior is a "repair" behavior for broken markup, and it is wise not to rely on it.

You can see how such markup creates the DOM here: Live DOM Viewer

Also note that this is different from what @Amber says. It is not considered a tag in any meaningful sense, and it certainly is not ignored.



It doesn't actually work - it just interprets it as an actual tag and then throws that tag out as invalid.

<!- foo bar -->


is treated as a tag <!-foo bar-->

which is obviously not a standard HTML tag and is therefore ignored.

Try this and you will see that it doesn't really work as a comment:

<!- >foo bar-->




All Articles