HTML Comments: Search Engine Optimization Basics
HTML comments syntax is unclear at first glance. An HTML comment starts with:
It does not have “–” nor “>” in any part within the comment. For the reason that HTML is an SGML application, the syntax for comment that is used in HTML documents is really the syntax for SGML comment.
SGML comment is basically defined as a comment declaration which starts with:
to be followed by none or more comments, followed by:
A comment should start and end with:
and does contain any instance of:
The following are legal SGML comments:
Take note that a comment tag that is “empty” with only “–” characters should be legal if it will have a multiple of four “-” characters. It is also interesting to note that is a legal comment too, pertaining to an empty comment.
In addition to that, take note that not all parsers for HTML will get this right. For instance, you can verify with the above rule whether:
would be legal or not. It is actually a comment tag that has two comments, the first one being empty while the second one containing “> hello”.
When you try this code in a browser, you will see that the text will be displayed on the screen attributed to two possible reasons. It’s either:
- The browser thinks that the comment ends at “>”, seeing it as a character; or
- The browser thinks that the comment ends at “–>”, seeing it as text.
There is also a problem present in the “–” sequence. Some people would get used to using separators in their source as “”. In most situations, they use a number of “-“ which is not in multiples of four. This could mean that a browser which tries to get it right will get it wrong and would instead hide the rest of the document.
Due to this, the following simple rule in composing acceptable and valid comments can be used:
HTML comments begin with:
and would not have “–” nor “>”
anywhere within the comment.
When To Use HTML Comments
HTML comments tag inserts a comment into the source code. The browser will ignore a comment when properly tagged to be ignored. You may use comments in order for you to explain your code, that will come handy in case you need to edit the source code on a later time.
Program-specific data may also be stored inside comments. Users might not see this but such information can be accessed by the program. The older browsers which don’t support styles or scripting might show comment as plain text. To prevent this from happening, the text can be placed inside style elements and text.
Remember too that HTML comments tag would not support standard attributes or event attributes.