TutorialLab
HTML Comments with examples
Home » Web Development » HTML Tutorials » HTML Comments

HTML Comments

An HTML comment is a part of your HTML document that a browser does not display. You can think of the comment as an invisible Post-it note that is only revealed when someone looks at the HTML source code by viewing the source in the browser or any HTML document editor or text editor.

There are several reasons to include HTML comments in your HTML code. One reason is to help remind yourself what a tag does or what changes you did in the document.

Secondly, comments can help other people to understand your HTML document. You might also use comments as a way to put notes about your plans for a document (For example, “Update this section in June!”). Another use of comments is to temporarily remove (“comment out”) a section of your document without actually having to delete the section entirely.

Most people ask about the difference between HTML 4 Comments and HTML 5 comments, But there is no difference between HTML 4 and HTML 5 Comments.

How to Use HTML Comment

To use HTML comment in your document, start HTML comment with the <!– characters, then type whatever text you want to be in the comment. Then end the comment with the –> characters.

HTML Comments tags are different from other tags because the comment characters are really SGML, and they can be much more complicated than this. However, most browsers only understand simple comments.

What is SGML

SGML stands for Standard Generalized Markup Language. This is actually a slight misnomer since SGML is actually a meta-language that is, a language for writing markup languages. HTML is a markup language written in SGML, an “SGML application”, to use the terminology.

 

HTML Comment Tag

These are the two tags used for HTML comments, HTML comments opening tag “<!–” and HTML comments closing tag “–>” and between that, we place our comment text.

 

Types Of HTML Comment

Basically, there are three types of HTML comments, single line HTML comments, Multiple lines HTML Comments and Conditional Comments.

Single Line HTML Comment

Single line HTML comment are those which only contain one line and also we can declare this type of comment in front of any HTML tag inside HTML documents.

For example:

In the above example, you can see, I have declared a single line comment in front of <h1></h1> tags. so single line comment can be always a single line and it can be declared in the front of any tag in the HTML document.

Multi-Line HTML Comments

Multiline HTML comments are those which contain multiple lines, we start with the HTML comment stating tag (<!–), then our comments line and end with HTML comment closing tag (–>).

For example:

Conditional HTML Comments

Conditional comments are those which contain some condition, but these type of comments were only used by internet explorer 5 and onward, and conditional comments are not used by any other browser and are ignored, it is only for internet explorer in windows.

For example:

HTML Comment Out

you can comment out any HTML object/tag with the help of HTML Comments Tags. Through this method, you can html comment any code in the HTML document.

Here is an example of some HTML code with part of the document “Commented out”. Anything in between the comment start (<!–) and the comment end (–>) won’t appear in the browser.

output result:

Welcome to Tutoriallab

The advantage of commenting out code is that it’s easy to delete the comment characters and restore the code. Just delete the comment opening tag and closing tag from the above example and you will see the result.

One danger of using this technique is that comments aren’t really hidden. anyone can view the source of the web page to see them. so remember to be tasteful and don’t try to hide any company secrets or details which is important in the comment.

Using Comment Tags

This method of commenting is deprecated in HTML 5 and can only be used in internet explorer and is not a good practice to use. but we explain it here for the purpose of knowledge.

Comment tags can be used to comment out any part of HTML element.

For example:

Result in internet explorer:

This is Internet Explorer.

Result in other browsers:

This is not Internet Explorer.

This is best practice not to use this <comment> </comment> tags and it is deprecated in HTML 5.

Invalid HTML Comment

What is invalid HTML Comment?

If you insert a wrong opening tag and closing tag,  or if there is any white space for the comment opening tag, then the HTML comments become invalid and will not work for you.

For example:

The result of the Invalid HTML Comment:

Welcome to Tutoriallab
< !– Call now –>

Commenting CSS (Cascade Style Sheet)

you will be learning CSS (Cascade Style Sheet) after learning HTML, so it’s a good practice to comment out style sheet in the HTML document, so that old browser may properly understand that style is used.

For example:

Same technique apply to javascript or any other scripting language.

Commenting Javascript/scripting language

Though you will learn JavaScript with HTML, in a separate tutorial, here you must make a note that if you are using JavaScript or VB Script in your HTML code then it is recommended to put that script code inside proper HTML comments so that old browser can work properly.

 

Conclusion

so far in this section, you learned about HTML commenting, How to use HTML Comments, How to Comment out HTML Code, How to comment HTML tag, Type of HTML Comments and There uses.

You should strongly consider using comments in your HTML documents, at the very least to indicate copyright information and when you last made changes to the document.

if you have any question about HTML comments, then fee free to use the comment box below to ask your question.

hassan

A Freelancer web developer, graphic designer and own this blog to spread knowledge.My hobby is computer problem's solving and i love to help other in this field.

HTML Tutorials