HTML comments

Comments are used for documentation in any programming language. It is a good practice to add comments into your code, especially for complex document, to add note in code or to indicate sections in document.

In html we can also use comment. It is a code that is ignored by any web browser and helps others to understand code and increases code readability.

HTML comments are written in between <!-- … -->

Comments Syntax:

<!-- Write comments here -->

Single line Comments

Example:

<html><head>
<title> HTML Comments </title>
</head><body>
<!-- about comment -->
<p> Comment is a code that is ignored by any web browser and helps others to understand code and increases code readability. </p>
</body>
</html>

Output:

Multiline Comments

We can comment multiple lines by the beginning tag <!-- and ending tag --> placed before the first line and end of the last line.

Example:

<html>
<head>
<title> Multiline Comments </title>
</head>
<body>
<!--
Multiline comment can
extend through as many as
lines you like.
-->
<p> Multiline comment can extend through as many as lines you like. </p>
</body>
</html>

Output:

Conditional Comments

Conditional comment defines some HTML tags to be executed by Internet Explorer (IE) only on Windows but they are ignored by other browsers.

Example:

<html>
<head>
<title> Conditional Comments </title>
<!-- [if IE 9] >
…. some HTML here ….
<![endif]-->
</head>
<body>
<p> Example of conditional comment. </p>
</body>
</html>

Output:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.