HTML single line comment

Is there a way to comment on a single line in HTML using only the escape sequence at the beginning of the line? Similar to using # or // in other languages? Or is <!-- ... --> only option for commenting in html?

+48
html comments documentation
Apr 14 '10 at 19:36
source share
5 answers

from http://htmlhelp.com/reference/wilbur/misc/comment.html

Since HTML is officially an SGML application, the comment syntax used in HTML documents actually has SGML comment syntax. Unfortunately, this syntax is a bit unclear at first.

The definition of an SGML comment is basically as follows:

The comment announcement begins with <! , and then zero or more comments, followed by <COD>. A comment begins and ends with a " - " and does not contain any " - ".
This means that all legal SGML Comments:
  1. <! - Hello - >
  2. <! - Hello - - Hello - >
  3. <CODE> <! ---->
  4. <! ------ - >
  5. <! >
Please note that the "empty" comment tag, with just the characters " - ", must always have a multiple of four. The characters < - "are legal. (And yes, <! > Is also a legitimate comment - this is an empty comment).

Not all HTML parsers get this right. For example, " <! ------ > hello - > " is a legitimate comment, since you can rule above. This is a comment tag with two comments; the first is empty and the second contains "> hello". If you try it in a browser, you have that text displayed on the screen.

There are two possible reasons for this:

  1. the browser sees the ">" character and thinks the comment ends there.
  2. The browser sees the " - > " text and thinks the comment ends there.
There is also a problem with the " - " sequence. Some people have a habit of using "<CODE> <! -------------->" as delimiters in their source. Unfortunately, in most cases the number of " - " characters - not a multiple of four. This means that a browser that is trying to get it right will not be right here and actually hide the rest of the document.

For this reason, use the following simple rules to compose valid and accepted comments:

HTML comments begin with " <! - ", end with " - > " and do not contain " - " or " > " anywhere in the comment.
+58
Apr 14 '10 at 19:44
source share

No, <!-- ... --> is the only comment syntax in HTML.

+21
Apr 14 '10 at 19:38
source share

No, you must close the comment with โ†’.

+5
Apr 14 '10 at 19:38
source share

TL DR To match browsers yes; but there are no corresponding browsers, so no.

According to the HTML 4 specification, <!------> hello--> is an absolutely correct comment. However, I did not find a browser that implements this correctly (i.e., in the specification) due to the crappy developers who do not know and do not follow the standards (as DigitalDreamer pointed out).

You can find the comment definition for HTML4 on the w3c website: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

Another thing is that many browsers are mistaken in that -- > closes the comment in the same way as --> .

+3
Oct 26 2018-11-21T00:
source share

Let it be easy. Loved @digitaldreamer is the answer, but it may leave newcomers confused. Therefore, I will try to simplify it.

The only HTML comment <!-- --> It can be used as a single line or double comment, it really depends on the developer.

So, the HTML comment starts with <!-- and ends with --> . It's really that simple. You should not use any other format to avoid any compatibility issue, even if the comment format is legal or not.

+3
Sep 29 '16 at 16:19
source share



All Articles