Comments are integral parts of any coding or programming language. They are used to explain a code section, indicate where certain actions occur, or disable parts of the code during debugging. They are readable only in the source code and not visible to users on the webpage.
In HTML, to create a comment, we use the <!-- -->
syntax. This is in contrast to languages like Javascript and C++ where comments can be added with //
or /* */
.
Correct usage of HTML comment looks like the following:
<!-- This is a comment -->
Treat <!--
as the start of the comment and -->
as the end of the comment. These angle brackets distinguish HTML comments from others. Everything written in between these brackets is considered a comment.
For example, in an HTML document, it could be:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<!-- This is a comment documenting the header above -->
<p>My first paragraph.</p>
<!-- This is a comment documenting the paragraph above -->
</body>
</html>
In this example, the text 'This is a comment documenting the header above' and 'This is a comment documenting the paragraph above' are comments. They provide explanations of what the heading and paragraph elements do, helping others who are reading or going through the source code.
Bear in mind that the comment tag in HTML is not seen by the end users, but only visible when you "view source" in your web browser.
When adding comments in your HTML code, keep them short and relevant. It's good practice to use comments to document how your code functions and to keep track of changes made, especially when working in a team.
In summary, adding comments in HTML is simple; it comes down to what text you enclose within <!-- -->
. Remember, healthy commenting makes code readable, easy to understand and maintain.