start learning
Image 1
32021231

HTML comment tag

The HTML comment tag is used to add comments within your HTML code that will not be displayed in the browser. Comments are useful for adding notes, explanations, or reminders for yourself or other developers who may be working on the code.

You can use our free live html editor to test tutorial codes .

The right way to use comments in your HTML code with explanation:

The syntax for the HTML comment tag :


<!-- Your comment goes here -->
  

Anything you put between the <!-- and --> will not be displayed in the browser. Here's an example:


<!DOCTYPE html>
<html>
<head>
	<title>My Website</title>
</head>
<body>
	<!-- This is a comment -->
	<h1>Welcome to my website</h1>
	<!-- This is a comment -->
	<p>Here is some content.</p>
</body>
</html>
  

In this example, the comment <!-- This is a comment --> will not be displayed in the browser. It is simply there to provide additional information for the developer who may be working on the code.


Multi-Line Comments in HTML :


<!--
This is a multi-line comment in HTML.
It can span multiple lines and is useful for adding
longer explanations or comments about your code.
-->
  

Multi-line comments in HTML are used to add longer comments or explanations that span multiple lines. They are created using the <!-- and --> characters and can be used to explain code blocks, sections of code, or entire pages.


Commenting Style Sheets :


<!DOCTYPE html>
<html>
<head>
	<title>My Website</title>
	<style>
/* This is a comment in CSS */
p {
    font-size: 16px;
    /* This style is disabled */
    /* color: #000000; */
}
</style>
</head>
<body>
	<!-- This is a comment -->
	<h1>Welcome to my website</h1>
	<p>Here is some content.</p>
</body>
</html>
  

In addition to commenting HTML code, you can also comment your CSS code using the /* and */ characters. Commenting style sheets can be useful for explaining the purpose of styles or for temporarily disabling styles without deleting them.


HTML-style comments within a <script> tag :


<script>
   <!-- This is a comment in a script tag -->
   document.write("Hello World!");
</script>
  

In this example, the HTML-style comment <!-- This is a comment in a script tag --> is used to comment out the document.write() line of JavaScript code.

Note that in modern HTML documents, it is not necessary to use HTML-style comments to comment out JavaScript code. Instead, you can simply use JavaScript-style comments like this:


<script>
   // This is a comment in a script tag
   // document.write("Hello World!");
</script>

In this example, the document.write() line of code is commented out using JavaScript-style comments instead of HTML-style comments.


Conditional Comments :


<!--[if IE]>
This content is only displayed in Internet Explorer.
<![endif]-->
  

In this example, the content between the <!--[if IE]> and <![endif]--> tags will only be displayed in Internet Explorer. Conditional comments are special comments that are used to target specific versions or types of browsers. They are no longer supported in modern browsers, but were commonly used in the past to work around browser compatibility issues.