Skip to main content

HTML - Comments

Comment is a piece of code which is ignored by any web browser. 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 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>
This will produce the following result without displaying the content given as a part of comments −

Valid vs Invalid Comments

Comments do not nest which means a comment cannot be put inside another comment. Second the double-dash sequence "--" may not appear inside a comment except as part of the closing --> tag. 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>
This will produce the following result −

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>
This will produce the following result −

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>
This will produce the following result −

Conditional Comments

Conditional comments only work in 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>
You will come across a situation where you will need to apply a 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.
Note − The <comment> tag deprecated in HTML5. Do not use this element.

Example

<!DOCTYPE html>
<html>

   <head>
      <title>Using Comment Tag</title>
   </head>
 
   <body>
      <p>This is <comment>not</comment> Internet Explorer.</p>
   </body>
 
</html>
If you are using IE, then it will produce following result −

But if you are not using IE, then it will produce following result −

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>
This will produce the following result −

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.

Example

<!DOCTYPE html>
<html>

   <head>
      <title>Commenting Style Sheets</title>
      
      <style>
         <!--
            .example {
               border:1px solid #4a7d49;
            }
         //-->
      </style>
   </head>
 
   <body>
      <div class = "example">Hello , World!</div>
   </body>
 
</html>
This will produce the following result −

Comments

Popular posts from this blog

The new TAG Heuer Carrera Calibre Tourbillon Nanograph is a lot of buzzwords in a beautiful package

Almost every word in the name of TAG Heuer’s new watch – the Carrera  Calibre Heuer 02T Tourbillon Nanograph – is important. Carrera connects it to TAG’s long history of chronographs while Calibre suggests a handmade watch made with some technical prowess. Tourbillon means you can expect this thing to cost more than a car (about $25,000 when it goes on sale) and Nanograph suggests that this thing is doing something quite unique. And it is. TAG Heuer loves experimenting with new materials and the Nanograph features a new hairspring design that is unique to TAG. The hairspring, which is made of carbon-composite, is lightweight and unaffected by gravity or shock. It also offers “perfect concentric oscillations” and is completely antimagnetic. Couple that with the rotating tourbillon and the suggestion is that this watch will remain accurate under all sorts of pressure. Further, rest of the movement includes carbon fiber and aluminum which reduces the effects of temperat...

Realme U1 Review: Perfect Package With Required Features

Hello friends, the Realme U1 smartphone has MediaTek Helio P70 processor. You will see what features & drawbacks there are in it. You also know whether this Realme smartphone is worth buying or not. Just a 6-month-old smartphone company, Realme, has made great recognition in a short span of time. The company has so far launched 5 of its total smartphones and they all come in less than Rs 20,000. The aim of the company is to draw the youth of the country on their side so that real-time smartphones can be seen in more hands. Realme U 1 has a starting price of Rs 11,999 and with this price, the Chinese company wants to give tough competition to the companies like xiaomi, Huawei & Honor in the budget category. Realme U1 is the world's first handset with the MediaTek Helio P70 processor. The most important feature of the phone is the 25 megapixel front camera and 3500mAh strong battery. Design of Realme U1 At first glance, Realme looks like the previous company o...

Asus ROG Phone goes up for pre-order in the UK with £100 discount

After  yesterday's launch in the UK , the Asus ROG Phone is finally set to hit the shelves in the country. However, you still need to wait until December 14 when the actual shipments start but if you hurry up and pre-order you can get the phone for £100 off. Otherwise, the actual price of the phone is £799 and can be found on Asus' official website. Unfortunately, though, there's still no word on the accessories and how much will they cost. We guess we will have to wait some more to find out.