Table of Contents
The HTML comment tag
HTML comments add notes or annotations within the HTML code. The web browser ignores such code when it renders the webpage. They are primarily intended for developers and designers to provide explanations, reminders, or instructions within the code. HTML comments do not appear on the webpage itself, but visitors can view them when inspecting the source code.
HTML comments are written using the <!– opening tag and the closing tag –>. Anything between these tags is a comment, and the browser won’t display. For example:
The HTML Comment tags are:
<!-- The comment here -->
Comments are highly useful for debugging. When working on a full-stack project, there are many developers who work in different departments. Some of them simultaneously work in more than one department. They use comments to communicate. We also use comments to define the purpose of anything in HTML. They are valuable for those people who do not understand HTML. For example, if a person knows nothing about HTML, he would not understand what does the following code does.
<a href="google.com">link</a> <img src="URL"> <button onclick="alert("hello world")> Click </button>
But if we use comments with the code, it will be easy for everyone to understand. Have a look at the following code.
<!-- This is a hyperlink. Clicking on it will open the home page of google --> <a href="google.com">link</a> <!-- This is an img tag which will display an image of Cristiano Ronaldo --> <img src="cristiano-ronaldo.jpg"> <!-- This is a button. Click it and an alert box will show the message "Hello world" --> <button onclick="alert("hello world")> Click </button>
Observe the difference in both the HTML codes. These are the comments. Read the two codes and we can see, the second code is now more understandable. This is how comments are used in HTML. But keep in mind, comments are not displayed on the browser.
Sample HTML code with comments
<html> <body> <!-- This will not appear on the browser. Ane even this will not appear on the browser. Again, this will also not appear on the browser. --> But this will appear on the browser because this is not a comment. </body> </html>
The output is:
Observe the above code. The text that was written between <!– and –> will not appear on the browser while the text written after that will. This is because anything we write between <!– and –> in HTML is a comment. It is only visible in source code.
How to comment multiline in HTML
We often write comments in HTML that consist of more than one line. The comment could be for documentation, debugging or directions. We need to understand that how can we comment a section in HTML. The following code has three headings and one paragraph.
<h1> Heading1 </h1> <h2> Heading2 </h2> <h3> Heading3 </h3> <p> This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph. </p>
The output is:
Now if we don’t want all the heading to appear on browser, for the time being, we can comment them out.
<!-- <h1> Heading1 </h1> <h2> Heading2 </h2> <h3> Heading3 </h3> --> <p> This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph .This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph. This is a paragraph.This is a paragraph.This is a paragraph. </p>
The output is:
The above output does not contain those three headings. We put that part into <!– and –>. This is how we comment a section in HTML. We can put any number of lines between <!– and –> in HTML.
How to comment one line in HTML
We can also comment on a single line in HTML. It is also done in the same way as we comment on multiple lines. In the following code, We will comment second heading.
<h1> Heading1 </h1> <!-- <h2> Heading2 </h2> --> <h3 Heading3 </h3> <p> This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph .This is a paragraph.This is a paragraph.This is a paragraph.This is a paragraph. This is a paragraph.This is a paragraph.This is a paragraph. </p>
The output is:
Now, We cannot see the second heading. We can also comment on multiple lines separately. In the following code, we will comment all the heading separately.
<!--<h1> Heading1 </h1> --> <!--<h2> Heading2 </h2> --> <!--<h3 Heading3 </h3> --> <p> This is a paragraph.This is a paragraph.This is a paragraph. This is a paragraph .This is a paragraph.This is a paragraph.This is a paragraph. This is a paragraph. This is a paragraph.This is a paragraph.This is a paragraph. </p>
The output is:
In the above output, none of the headings appear on the browser. This is because we separately put each heading in <!– and –> tags. This is how we comment a single line in HTML.
PHP Comments
In PHP, there are two ways to add comments to your code:
Single-line comments: Single-line comments in PHP start with //
or #
and continue until the end of the line. They are used for adding short explanations or notes on a single line. For example:
// This is a single-line comment in PHP
$variable = 5; // Assigning a value to a variable
# This is also a single-line comment in PHP
$anotherVariable = "Hello"; # Assigning a value to another variable
Multi-line comments: Multi-line comments in PHP start with /*
and end with */
. They can span multiple lines and are useful for adding longer explanations or commenting out blocks of code. For example:
/*
This is a multi-line comment in PHP.
It can span multiple lines.
*/
$number = 10; /* Another variable */
Comments in PHP are ignored by the PHP interpreter and do not affect the execution of the code. They are meant for documentation purposes and to provide insights to developers reading or working with the code.
Using comments effectively can make your PHP code more readable, maintainable, and easier to understand for yourself and others who may work on the codebase.
CSS Comments
Commenting in CSS is just the same as in PHP. We can use // for single line comments or /* …. */ for multiline comments.
.myclass { width: 100%; //height: 100px; background: blue; } /* any other comment here */
Javascript Comments
Just like comments in HTML, in JavaScript, you can add comments to your code to make it more understandable and provide additional information for yourself and other developers. There are two types of comments in JavaScript: single-line comments and multi-line comments.
Single-line comments: Use double forward slashes (//) to create a single-line comment. Anything after the double slashes on the same line is a comment and will be ignored by the JavaScript interpreter.
// This is a single-line comment
var x = 5; // This line declares a variable and assigns a value
Multi-line comments: Use forward slash followed by an asterisk (/) to begin a multi-line comment, and end it with an asterisk followed by a forward slash (/). Anything between these markers is a comment.
/* This is a multi-line comment
It can span across multiple lines
and is useful for longer explanations or commenting out blocks of code */
var y = 10; /* This line declares a variable and assigns a value */
Comments are not executed by JavaScript and do not affect the functionality of your code. They are purely for human-readable documentation and clarification. It’s good practice to include comments in your code to make it easier to understand and maintain.
FAQ about HTML comments
Why are HTML comments useful?
HTML comments play an important role in web development for several reasons:
- Code Documentation: Comments allow developers to add explanatory notes directly within the HTML code. This helps in documenting the purpose, functionality, or intent of specific sections of code. It serves as a form of self-documentation, making it easier for developers to understand and maintain the codebase, especially when revisiting it after a period of time.
- Collaboration and Communication: Comments facilitate effective collaboration among team members working on a project. Developers can use comments to communicate ideas, share insights, or provide instructions to others who might be working on the same code. It helps foster clear and concise communication within the development team.
- Debugging and Troubleshooting: Comments are valuable during the debugging and troubleshooting process. Developers can use comments to temporarily disable or “comment out” sections of code to isolate and identify issues. By selectively enabling or disabling specific code blocks, developers can narrow down the cause of a problem more effectively.
- Code Maintenance and Updates: Over time, web projects may undergo updates, enhancements, or modifications. Comments aid in understanding the original purpose and functionality of code sections, making it easier to identify areas that need modification or improvement. They provide context and guidance for developers working on maintenance tasks.
- Learning and Educational Purposes: HTML comments can be used as educational tools, particularly when sharing code examples or tutorials. Comments allow developers to provide explanations, step-by-step instructions, or annotations within the code, making it easier for learners to follow along and understand the underlying concepts.
Overall, HTML comments contribute to code readability, maintainability, collaboration, and troubleshooting. They help ensure that the code remains comprehensible to both the original developers and others who may work on it in the future. Properly utilized comments can enhance the efficiency and effectiveness of web development projects.
What HTML tags can we comment?
You can apply HTML comments to any section of HTML code. For example, individual elements, blocks of code, or entire sections. They are not specific to any particular HTML tag. You can use comments within your HTML code to annotate and provide explanations for various elements, such as <div>, <p>, <a>, <img>, <table>, <form>, and more.
Here’s an example of how comments can be used with different HTML tags:
<div class="container">
<!-- This is a comment for the container div -->
<h1>Welcome to my website</h1>
<p>This is a paragraph of text.</p>
<!-- <img src="image.jpg" alt="An image"> -->
<a href="https://example.com">Visit Example.com</a>
</div>
In the example above, comments are used to provide additional information about the container div, temporarily disable an image element, and add clarity to the purpose of the anchor (link) element.
Remember that HTML comments are not rendered or displayed on the webpage itself. They are purely for developer and designer reference, aiding in code readability, documentation, and communication.
Can we run a JavaScript code inside a comment in HTML?
No, you cannot run JavaScript code inside an HTML comment. HTML comments are specifically designed to be non-executable and are meant for human-readable documentation or explanatory purposes only.
When the browser encounters an HTML comment (<!– … –>), it treats the content inside it as plain text and ignores any code or script tags within it. The browser will not execute, either implement JavaScript code inside an HTML comment.
Here’s an example to illustrate this:
<!--
<script>
var x = 5;
console.log(x);
</script>
-->
In the above example, the JavaScript code inside the HTML comment is not executed by the browser. It is treated as a regular text and ignored. The commented-out JavaScript code will have no effect on the behavior or functionality of the webpage.
To run JavaScript code in an HTML document, you need to place the code directly within a valid <script> tag or refer to an external JavaScript file using the <script> tag’s src attribute. JavaScript code placed outside of HTML comments will be executed by the browser as expected.
Is there any difference between single and multi-line comments in HTML?
In HTML, comments serve a similar purpose as in JavaScript, providing a way to include notes or explanations in your code that are not rendered or displayed in the browser. However, HTML only supports one type of comment, which is the multi-line comment.
Unlike JavaScript, HTML does not have a single-line comment syntax using double slashes (//). The only way to create comments in HTML is by using the multi-line comment syntax with <!– to start the comment and –> to end it.
How to place comments in HTML5?
HTML5 is the latest version of the HTML standard, and it maintains the same syntax and rules for comments as previous versions of HTML. There is no difference in the usage of comments between HTML and HTML5.
Both HTML and HTML5 use the same multi-line comment syntax with <!--
to start a comment and -->
to end it. You can include comments anywhere in your HTML or HTML5 code to provide explanations, instructions, or to temporarily disable sections of code without affecting the rendering of the webpage.
Whether you are working with HTML or HTML5, comments function the same way, providing a way to add notes and explanations to your code that are not rendered by the browser.
When was it first implemented?
HTML comments were first implemented in the initial version of HTML, which was HTML 2.0. HTML 2.0 was released in November 1995 and introduced the concept of comments to the HTML language.
They included comments to provide a way for developers to add explanatory or instructional notes within the HTML code without affecting the rendering of the webpage in browsers. Since then, comments have been an essential part of HTML and have been carried forward into subsequent versions, including HTML4 and HTML5, maintaining the same syntax and purpose.
Conclusion about HTML comments
HTML comments are very important tags for web developers. No project in web development is complete without comments. Although comments are not visible on the browser, they are very helpful in debugging, documentation and directions.
It’s possible to hide HTML buttons, images, form elements or any other elements. Also, you cannot use HTML comments to hide Javascript content. Javascript have its own commenting structure.
Every developer should have the habit of using comments while developing for the web.