HTML Comment Syntax
<html xmlns="http://www.w3.org/1999/xhtml"> <!-- ... comment ... --> <head> ... <title>My Title</title> <!-- no comments inside title element --> ... <!-- ... comment ... --> ... </head> <body> ... <!-- ... comment ... --> ... </body> <!-- ... comment ... --> </html>
How to code comments in HTML or XML
Rules for commenting out HTML code
- Code HTML comments anywhere between the starting
<html xmlns="http://www.w3.org/1999/xhtml">
tag and ending</html>
tag, but not where text content is expected. (See the issues with HTML 5 comments.) - Code the starting comment delimiter characters
<!--
at the beginning of the HTML code that is to be commented out. - Code the ending comment delimiter characters
-->
at the end of the HTML code that is being commented out. - Make sure the body of the comment does not include any sequence of
--
characters (double hyphens).
Child elements in HTML comments
Although the code within HTML comments can include other tags, all of the tags between the beginning of the comments and the end of the comments will be ignored. Tags inside comments do not need to be matched up since they are not treated as nested elements.
HTML comments within scripts
However, HTML comments within scripts will not cause the script code to be ignored. This HTML code pattern was designed to allow hiding content inside a script element from any browsers that might otherwise treat that content as phrasing content rather than as JavaScript code.
Comments in JavaScript
In JavaScript code, use two slash characters (//
) to comment out code to the end of the line. For a comment that can span more than one line of HTML code, start the comment with the delimiter characters /*
and end it with the delimiters */
.