HTML - Comments

HTML - Comments

A Comment is a piece of code which is ignored by any web browser. Comments is used to indicate sections of a document, and any other notes to anyone looking at the code. It comes in handy for complex documents or in the training of others. Comments help you and others understand your code and increases code readability. HTML comments are placed in between <!-- ... -->

Example

  •       
      <!DOCTYPE html>
    <html>
    <head>
    < ! - - D o c u m e n t H e a d e r S t a r t s - -> 
    <title> commenting in a html document</title> 
    </head>
    <body> 
    <! - - D o c u m e n t  content- ->
    html comment makes a html document easy to read by both the developer and others .
    </body> 
    </html>
    

    The result will displaying without all the areas stated as a comments.

    Valid vs Invalid Comments

    For a comment to be valid, a few things has to be typed properly, when this is not done the comment becomes invalid.

    Things to avoid

    • Comments do not nest which means a comment cannot be put inside another comment.
    • 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 a comment string.

    Multiline Comments

    When putting a comment that is more than a single line,To get a Multiline comment You must have a start and an ending tag.

    Example

  •  
    <!DOCTYPE html>
    <html>
    <head>
    <title> Multiline</title> 
    </head>
    <body> 
    <! - - a l o t c a n b e p l a c e d i n s i d e 
     a m u l t i l i n e c o m m e n t, 
    y o u  c a n u s e i t t o d i s c r i b e
     w h a t e v e r  y o u w a n t- ->
    
    </body> 
    </html>
    

    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>
    <! - - [ i f I E 6 ] >    S p e c i a l i n s t r u c t i o n s 
    f o r I E 6 h e r e  <! [ e n d i f ] - - >
    <head>
    <title> Conditional comments</title> 
    </head>
    <body> 
    <p> moving forward </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.

    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> Comments</title> 
    </head>
    <body> 
    <p> moving <comment>forward</comment> to a better place.</p> 
    </body> 
    </html>
    

    Results

    If you are using IE, then it will produce following result− moving to a better place. 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> script code in comment</title> 
    </head>
    <script> < !-- document.write("Hello World!")  / / - - > </script> 
    <body> 
    <p> Hello world!</p> 
    </body> 
    </html>
    

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1