Single-line comment in HTML
Solution 1
From HTML comments:
Since HTML is officially an SGML application, the comment syntax used in HTML documents is actually the SGML comment syntax. Unfortunately this syntax is a bit unclear at first.
The definition of an SGML comment is basically as follows:
A comment declaration starts withThis means that the following are all legal SGML comments:<!
, followed by zero or more comments, followed by>
. A comment starts and ends with "--
", and does not contain any occurrence of "--
".Note that an "empty" comment tag, with just "
<!-- Hello -->
<!-- Hello -- -- Hello-->
<!---->
<!------ Hello -->
<!>
--
" characters, should always have a multiple of four "-
" characters to be legal. (And yes,<!>
is also a legal comment - it's the empty comment).Not all HTML parsers get this right. For example, "
<!------> hello-->
" is a legal comment, as you can verify with the rule above. It is a comment tag with two comments; the first is empty and the second one contains "> hello". If you try it in a browser, you will find that the text is displayed on screen.There are two possible reasons for this:
There is also the problem with the "
- The browser sees the ">" character and thinks the comment ends there.
- The browser sees the "
-->
" text and thinks the comment ends there.--
" sequence. Some people have a habit of using things like "<!-------------->
" as separators in their source. Unfortunately, in most cases, the number of "-
" characters is not a multiple of four. This means that a browser who tries to get it right will actually get it wrong here and actually hide the rest of the document.For this reason, use the following simple rule to compose valid and accepted comments:
An HTML comment begins with "<!--
", ends with "-->
" and does not contain "--
" or ">
" anywhere in the comment.
Solution 2
No, <!-- ... -->
is the only comment syntax in HTML.
Solution 3
Let's keep it simple. I loved digitaldreamer's answer, but it might leave the beginners confused. So, I am going to try and simplify it.
The only HTML comment is <!-- -->
. It can be used as a single line comment or double; it is really up to the developer.
So, an HTML comment starts with <!--
and ends with -->
. It is really that simple. You should not use any other format, to avoid any compatibility issue even if the comment format is legitimate or not.
Solution 4
No, you have to close the comment with -->.
Solution 5
TL;DR: For conforming browsers, yes; but there are no conforming browsers, so no.
According to the HTML 4 specification, <!------> hello-->
is a perfectly valid comment. However, I've not found a browser which implements this correctly (i.e. per the specification) due to developers not knowing, nor following, the standards (as digitaldreamer pointed out).
You can find the definition of a comment for HTML4 on W3C's website: 3.2.4 Comments
Another thing that many browsers get wrong is that -- >
closes a comment just like -->
.
Related videos on Youtube
Comments
-
aslum almost 2 years
Is there a way to comment out a single line in HTML using just an escape sequence at the start of the line?
Similar to using
#
or//
in other languages? Or is<!-- ... -->
the only option for commenting in HTML?-
Andrew almost 4 yearsTL;DR: No.
<!-- -->
only.
-
-
Matti Virkkunen about 14 yearsInteresting, I never knew
<!>
was a valid comment. I'd avoid things like that just for compatibility's sake though -
aslum about 14 yearsThanks for the awesome and exhaustive answer. I didn't think there was a way to do it, but figured it couldn't hurt to ask, and now I know a lot more about comments, and also to avoid
<!-- -------blah------ -->
which I had thought was alright since there was a space between the start and end '--'s. -
Yuhong Bao almost 13 yearsAFAIK HTML5 has moved away form the SGML comment syntax, since it is no longer based on SGML.
-
Robert almost 8 yearsYes, now with HTML5 comments must begin with
<!--
, end with-->
and the text must not start with>
or->
, not contain--
and not end with-
. But there are loads of rules for parsing invalid comments browsers can adhere to. -
Nick Matteo about 7 yearsHow does that mean that for "conforming browsers", there's a syntax for opening a comment which ends at the end of the line? The correct answer is "For conforming browsers, no; but for actual browsers, also no."
-
Andrew almost 4 yearsDownvote for not actually addressing the question but instead providing a bunch of extraneous detail.
-
Andrew almost 4 yearsThis should be the only answer to this question.....
-
aslum almost 4 years@Andrew If I could upvote again to counter your downvote I would. I think the exhaustive answer is more useful as people do OFTEN use extra -- in comments and that could lead to trouble. Matti's answer is fine, but it also leaves quite a bit of detail out.
-
Andrew almost 4 years@aslum This doesn't answer the question. At all.
-
David J. over 3 yearsI agree with Andrew. The OP wrote: "Is there a way to comment out a single line in HTML using just an escape sequence at the start of the line? Similar to using # or // in other languages?" (I added bold for emphasis)
-
David J. over 3 yearsThe source cited here: htmlhelp.com/reference/wilbur/misc/comment.html refers to "Wilbur", the code name for HTML 3.2. Any good skeptic would ask "to what degree does this standard still apply to HTML 5?" Also, it is notable that a good amount of Internet searching does not find content that claims that HTML comments follow this SGML spec as described by htmlhelp's site.