Home HTML HTML Comment Tag with Example

HTML Comment Tag with Example

50
0

HTML comment tag is used to insert comments inside source code. Contents within comment (<!–..–>) tag will not be displayed in web browsers.

Writing comments inside source code is a good practice because it becomes difficult to review or remember your code especially when you are working with thousand lines of code. It helps you to explain or review your source code so that you can easily get and understand or/and edit your source code later on.

Note: HTML comment tag does not support any HTML Attributes.

Following are the types of comments that are supported by HTML document.

HTML Comment – Description

HTML comment starts with “<!–“ and ends with “–>” (without quotes).

Sample Code
<html>
<head>
<title> HTML Comment Tag Example</title>
</head>
<body>
<!–
Body Part Starts this is a
valid comment statement
–>
<p> This is an example code for HTML Comment Tag </p>
<!– Body Part Ends –>
<!– both above comment statements will not display in web browsers –>
</body>
</html>

Note: It does not support “–“ or “>” anywhere inside comment tag.

Sample Result

This is an example code for HTML Comment Tag

CSS Comment – Description

CSS comment starts with “/*” and ends with “*/” (without quotes).

Sample Code
/* styling paragraph tag */
p
{
text-align: left; /* setting paragraph's text align to left */
color: blue;
padding: 5px; /* padding paragraph to 5px from top, right, bottom, left */
}
Sample Result

This is an example code CSS Comments

JavaScript Comment – Description

Comments are also available in JavaScript. JavaScript comments falls into two different types.

1. Single Line Comment: Supports only single line comment which starts with “//” and ends when line ends
2. Multi Line Comment: Supports multiline comment which starts with “/*” and ends with “*/” (without quotes)

Sample Code
<script type="text/javascript">
// Example of Single Line Comment
document.write("<p>This is a simple example for single line comment</p>");
/*
Example of Multi Line Comment
this is the second line for multi line comment
this is the third line for multi line comment
*/
document.write("<p>This is a simple example for multi line comment</p>");
</script>
Sample Result

This is a simple example for single line comment

This is a simple example for multi line comment

Tip: You can see the complete List of HTML Elements here.
Previous articleHTML DOCTYPE Tag with Example
Next articleHTML a Tag with Example
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

eighteen − six =