HTML - Comments



A comment in HTML is like a note for yourself and others reading your code. It is a good practice to add comments into your HTML code, especially in complex documents, to indicate sections of a document, and any other notes to anyone looking at the code. Comments are completely ignored by web browsers, so they don't affect how your page looks or works.

Comments help you and others understand your code and increases code readability. HTML comments are placed in between <!-- ... --> tags. So, any content placed with-in <!-- ... --> tags will be treated as comment and will be completely ignored by the browser.

Example

<!DOCTYPE html>
<html>
<head>
   <!-- Document Header Starts -->
   <title>This is document title</title>
</head>
<!-- Document Header Ends -->
<body>
   <p>Document content goes here.....</p>
</body>
</html>

If you run the above program, it displays the sentence "Document content goes here....." neglecting the content given as a part of comments.

Valid vs Invalid Comments

Comments in HTML have certain rules that you need to follow. Below is the list of rules −

  • Comments do not support nesting which means a comment cannot be put inside another comment.

  • You can't have the sequence "--" within a comment, except to close it.

  • You must also make sure that there are no spaces in the start-of-comment string.

Example

Here, the given comment is a valid comment and will be wiped off by the browser.

<!DOCTYPE html>
<html>
<head>
   <title>Valid Comment Example</title>
</head>
<body>
   <!--   This is valid comment -->
   <p>Document content goes here.....</p>
</body>
</html>

But, following line is not a valid comment and will be displayed by the browser. This is because there is a space between the left angle bracket and the exclamation mark.

<!DOCTYPE html>
<html>
<head>
   <title>Invalid Comment Example</title>
</head>
<body>
   < !--   This is not a valid comment -->
   <p>Document content goes here.....</p>
</body>
</html>

Multiline Comments

So far we have seen single line comments, but HTML supports multi-line comments as well.

You can comment multiple lines by the special beginning tag <!-- and ending tag --> placed before the first line and end of the last line as shown in the given example below.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Multiline Comments</title>
</head>
<body>
   <!--
      This is a multiline comment and it can
      span through as many as lines you like.
   -->
   <p>Document content goes here.....</p>
</body>
</html>

Conditional Comments

Conditional comments are a feature specific to Internet Explorer (IE) on Windows but they are ignored by other browsers. They are supported from Explorer 5 onwards, and you can use them to give conditional instructions to different versions of IE.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Conditional Comments</title>
   <!--[if IE 6]>
      Special instructions for IE 6 here
   <![endif]-->
</head>
<body>
   <p>Document content goes here.....</p>
</body>
</html>

These tags are used in the situation where you need to apply different style sheet based on different versions of Internet Explorer, in such situation conditional comments will be helpful.

Using Comment Tag

There are few browsers that support <comment> tag to comment a part of HTML code.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Using Comment Tag</title>
</head>
<body>
   <p>This is <comment>not</comment> Internet Explorer.</p>
</body>
</html>

Commenting Script Code

Though you will learn JavaScript with HTML, in a separate tutorial, but here you must make a note that if you are using Java Script or VB Script in your HTML code then it is recommended to put that script code inside proper HTML comments so that old browsers can work properly.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Commenting Script Code</title>
   <script>
      <!-- document.write("Hello World!") -->
   </script>
</head>
<body>
   <p>Hello , World!</p>
</body>
</html>

Commenting Style Sheets

Though you will learn using style sheets with HTML in a separate tutorial, but here you must make a note that if you are using Cascading Style Sheet (CSS) in your HTML code then it is recommended to put that style sheet code inside proper HTML comments so that old browsers can work properly.

To comment a CSS script within style tag, we need to use the /* symbol as a starting point and */ symbol as a ending.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Commenting Style Sheets</title>
   <style>
      /* commenting in stylesheet 
         .example {
         border: 1px solid #4a7d49;
      } */
   </style>
</head>
<body>
   <div class="example">Hello , World!</div>
</body>
</html>
Advertisements