HTML Comments: Adding Comments within your Code

HTML comments help explain the code and are not visible to your website visitors.

Any HTML document can contain comments. Although comments are optional, they can be very useful.

Now, I'm not talking about the comments section at the bottom of an article, where readers can post their comments. I'm referring to comments within the code.

What is an HTML Comment?

An HTML comment is a part of the HTML code and it is used to explain the code. This can be helpful for other HTML coders when trying to interpret someone elses code. It can also be useful for yourself if you have to revisit your code in many months, or even years time. Comments aren't displayed in the browser — they are simply there for the programmer's benefit.

Comments can be used to introduce major sections of the code, or they can be used to explain what a small section of code does. They are also often used at the top of a document to explain things like, author, date first created, purpose of the file, provide info about related files, and more.

How to write Comments

You write comments like this:

Comments always start with <!-- and end with -->. This tells the browser when a comment begins and ends.

Example HTML Code:

View Output

As you can see, the comments are invisible to the user viewing the page in the browser. It is there, simply for the HTML coder's benefit.

Multiline comments (i.e. comments that span across multiple lines), use the same syntax as single line comments. You begin the comment with <!-- and end with -->. Whether the comment spans one line or ten, you still use the same opening and closing tags.

Well, we have now finished the first part of this tutorial. We have covered a lot, and by now, you have learned enough to build a website.

Now we'll move on to more specific content items such as tables, iframes, image maps, and forms.