Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single line comment in HTML

Tags:

comments

html

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?

like image 294
aslum Avatar asked Apr 14 '10 19:04

aslum


People also ask

What is single line comment?

Single-line comments allow narrative on only one line at a time. Single-line comments can begin in any column of a given line and end at a new line or carriage return. The // character sequence marks the text following it as a single-line comment.

How do I make single line comments?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by Java (will not be executed).

How do I comment out multiple lines in HTML?

You can comment multiple lines by the special beginning tag <! -- and ending tag --> placed before the first line and end of the last line as shown in the given example below.

Which tag is used for single line comment?

Single-line comment: Single line comment is given inside the ( <! – comment –> ) tag.


2 Answers

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

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 with <!, followed by zero or more comments, followed by >. A comment starts and ends with "--", and does not contain any occurrence of "--".
This means that the following are all legal SGML comments:
  1. <!-- Hello -->
  2. <!-- Hello -- -- Hello-->
  3. <!---->
  4. <!------ Hello -->
  5. <!>
Note that an "empty" comment tag, with just "--" 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:

  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 the problem with the "--" 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.
like image 197
digitaldreamer Avatar answered Sep 20 '22 15:09

digitaldreamer


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

like image 31
Matti Virkkunen Avatar answered Sep 20 '22 15:09

Matti Virkkunen