Lesson 8 – HTML Comments

What are HTML Comment Tags?

HTML Comment tags are used to show comments within your source code. These tags are not displayed by a browsers on your computer screen. But however, it is a good practice to include these tags in your source code as they help you and other coders to understand a html code easily.

HTML comment tags are represented by <!- – some text- ->. Therefore, if any text is inserted between these tags , will be marked as comments and will not be displayed by your browser. Example is explained below:

<!DOCTYPE html>
<html>
<header>
<title> Comment example</title>
</header>
<body>
<!- – Paragraph will start from here –>
<p>Hello, You can find free html tutorial at www.freetutorialpoint.com.</p>
<!- – New Paragraph will start from here –>
</body>
</html>

The result will be displayed as:

 
Comment example

Hello, You can find free html tutorial at www.freetutorialpoint.com.

  • Conditional Comments:

Conditional comments are only supported by Internet Explorer 5 onwards, and not by any other browser. These tags are used to pass on conditional instructions to I.E. to do some specific task when a particular criteria is matched.  For example, when you need to execute some additional html tags based on Internet Explorer versions, in such situations you can use conditional comment tags.

<!DOCTYPE html>
<html>
<head> 
<title>Conditional Comments Example </title>
</head>
<body>
<!- -[if IE 8]>
   write your conditional instructions here for IE 8
<![endif]- ->
<p>Some paragraph text</p>
</body>
</html>
  • Comment Tags for Other Browsers:

There are some comment tags, which are supported by other browsers, and not by Internet Explorer e.g. <comment>..</comment>. Let’s see this example:

<!DOCTYPE html>
<html>
<head>
<title> Comment Tag Example</title>
</head>
<body>
<p> You are <comment>not</comment> using Internet Explorer</p>
</body>
</html>

Result displayed by Internet Explorer:

 You are using Internet Explorer

Result displayed by other browsers:

 You are not using Internet Explorer

Chapter Summery:

  • Use html <!- – some text- -> tag to add ‘comments’ in your html source code.
  • Use html <!- -[if IE]> tag to add ‘comments’ for Internet Explorer only.
  • Use html <comment> tag to add ‘comments’ for other browsers.

Interview Questions:

  • Can we add multiple line comments in html comment tags?

Yes, you can add multiple line comments in html comment tag. Place opening tag <! – –  before first line and closing tag – -> after last line to add multiple line comments. Let’s learn with below example how to do this:

<!DOCTYPE html>
<html>
<head> 
<title>Example</title>
</head>
<body>
<!- –  
Line one goes here
Line two goes here
Line three goes here
– ->
<p>Write your paragraph text here</p>
</body>
</html>

The result will be displayed as:

 Write your paragraph text here
  • Can we put one comment tag inside other comment tag?

No, comment tags don’t nest with each other, which means you can’t put one comment inside another comment.

  • What if, we have to add the double dash sequence  “- -” inside the comment. Can we do this in html comment tag?

No, you can’t add double dash sequence inside the comment tag except for closing tag. 

Practice Exercise :

<!DOCTYPE html>
<html>
<head> 
<title>Practice Exercise</title>
</head>
<body>
 First practice question will start from here
<p>First question: HTML stands for ?</p>
</body>
</html>

In above html code:

  • Put comment tag around text “First practice question will start from here”
Click here to practice above exercise in real time HTML code editor

Leave a Reply

Your email address will not be published. Required fields are marked *

95 − 90 =