HTML Comments
HTML Comments
The HTML comments are used to indicate sections of a document or insert notes explaining the code. They help understand the code and increase its readability. The comment tag can also be used for excluding temporary code blocks instead of deleting them. (HTML注释用于指示文档的部分或插入解释代码的注释。它们有助于理解代码并提高其可读性。注释标记也可用于排除临时代码块,而不是删除它们。)
For defining HTML comments we use the <!– … –> tag. Browsers ignore this tag and do not show its content to the user.
Only a few browsers support the comment tag for commenting a part of an HTML code. (只有少数浏览器支持注释标签来注释HTML代码的一部分。)
HTML comments can be used anywhere in the document except for the <title> tag. They do not work within the <style> tag either, as CSS uses another syntax for comments.
Valid vs Invalid Comments
Valid vs Invalid Comments (有效与无效评论)
A comment cannot be inserted into another comment. The double-dash sequence (“–”) may not come inside a comment if it is not a part of the closing (–>) tag. It is also important to ensure that there aren’t spaces in the start-of comment string. (评论不能插入到另一条评论中。如果双划线序列( “–” )不是closing ()标记的一部分,则可能不会出现在注释–>中。确保注释开始字符串中没有空格也很重要。)
Multiline Comments
Multiline Comments (多行注释)
HTML supports both single-line and multi-line comments. We can use multi-line comments by the beginning tag <!– and ending tag –>.
Commenting Style Sheets
Commenting Style Sheets (批注样式表)
When using CSS in an HTML code, it is recommended to put the style sheet code inside appropriate HTML comments for the proper work of old browsers. (在HTML代码中使用CSS时,建议将样式表代码放在适当的HTML注释中,以便旧浏览器正常工作。)
Commenting Script Code
Commenting Script Code (注释脚本代码)
When using JavaScript or VB Script in the HTML code, it is recommended to put the script code inside appropriate HTML comments for the proper work of old browsers. (在HTML代码中使用JavaScript或VB脚本时,建议将脚本代码放在适当的HTML注释中,以便旧浏览器正常工作。)
Syntax
Syntax (语法)
Comments are written between <!– and –> symbols.
Example of an HTML comment:
<!DOCTYPE html>
<html>
<head>
<title>The title of the document.</title>
</head>
<body>
<h1>The main heading</h1>
<!-- <p>We want to hide this part of the code temporary. </p> -->
<a href="https://www.w3cdoc.com">Homepage</a>
<!-- <p>This is a link to the homepage</p> -->
</body>
</html>