HTML Comments

Comments in html

HTML Comments are some text or code written in your code to give an explanation about the code, and not visible to the user. Comments which are used for HTML file are known as HTML comments. Anything written between these tags will be ignored by the browser, so comments will not be visible on the webpage.

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>

How to add Comments:

You can add comments in your HTML file using <! — … —> tag. So if you will write anything between theses comment tag that will be treated as comment and browser will not read it.

Example:

                    <! — Write commented text here –>

Multiline Comments:

In HTML code, we can also comments multiple lines at a time. In multiline comment we can use any description about code or multiple line code to debug, etc.

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 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>

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.

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>

Recommended Posts:

Spread the love

Leave a Comment

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