- HTML - Layouts
- HTML - Javascript
- HTML - Style Sheet
- HTML - Header
- HTML - Marquees
- HTML - Embed Multimedia
- HTML - Forms
- HTML - Fonts
- HTML - Colors
- HTML - Backgrounds
- HTML - Blocks
- HTML - Iframes
- HTML - Frames
- HTML - Email Links
- HTML - Image Links
- HTML - Text Links
- HTML - Lists
- HTML - Tables
- HTML - Images
- HTML - Comments
- HTML - Meta Tags
- HTML - Phrase Tags
- HTML - Formatting
- HTML - Attributes
- HTML - Elements
- HTML - Basic Tags
- HTML - Overview
- HTML - Home
HTML References
- HTML - Deprecated Tags
- HTML - Character Encodings
- Language ISO Codes
- HTML - URL Encoding
- MIME Media Types
- HTML - Events Ref
- HTML - Fonts Ref
- HTML - Entities
- HTML - Color Names
- ASCII Table Lookup
- HTML - ASCII Codes
- HTML - Fonts Reference
- HTML - Events Reference
- HTML - Attributes Reference
- HTML - Tags Reference
HTML Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 readabipty.
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 −
Vapd vs Invapd 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 vapd comment and will be wiped off by the browser.
<!DOCTYPE html> <html> <head> <title>Vapd Comment Example</title> </head> <body> <!-- This is vapd comment --> <p>Document content goes here.....</p> </body> </html>
This will produce the following result −
But, following pne is not a vapd 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>Invapd Comment Example</title> </head> <body> < !-- This is not a vapd comment --> <p>Document content goes here.....</p> </body> </html>
This will produce the following result −
Multipne Comments
So far we have seen single pne comments, but HTML supports multi-pne comments as well.
You can comment multiple pnes by the special beginning tag <!-- and ending tag --> placed before the first pne and end of the last pne as shown in the given example below.
Example
<!DOCTYPE html> <html> <head> <title>Multipne Comments</title> </head> <body> <!-- This is a multipne comment and it can span through as many as pnes you pke. --> <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 sopd #4a7d49; } //--> </style> </head> <body> <span class = "example">Hello , World!</span> </body> </html>
This will produce the following result −
Advertisements