It is a very good working practice to comment your code in HTML. In all programming languages, comments serve two major functions:
- As notes and reminders
- Comments allow you leave yourself notes as to what sections of code do, and what they are there for. It is common for a developer to return to code that has not been touched in six months or more: comments allow you to quickly remember what each piece of code does.
- As “traffic cones” around code that is problematic
- Code surrounded by comment markup immediately ceases to work. Because HTML code is read from the top down, you can narrow down the cause of problems by removing sections from execution by surrounding them with comments, moving the start and ending comment markup around to analyse problems via a process of elimination.
All content inside of comments, including markup and code, is not rendered on a web page, but can be read by anyone with access to your source code. (In HTML this is as easy as using View / Source in the browser).
<!-- this is an HTML comment -->
Note that HTML comments are not tags; this can be confusing to many neophyte coders, as they appear to break all the rules of HTML. Comments may be placed over multiple lines.
<!-- this is a comment over multiple lines -->
Technically, there are only one character that, when used inside a comment, could cause errors:the dash (-), which could be confused with the closing tag of comments:
<!-- -- -->
However, this is being worked on, so future browsers should ignore uses of the dash inside comments.
Photograph by Chi Chi Tsai, licensed under Creative Commons
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.