Commenting makes HTML easier to write.
This is a spin off from another article I wrote entitle Good Comments Make Good Scripts. While the code is different, the concepts for comment are the same.
Enjoy!
Disclaimer!
This tutorial is intended to be used to show the benefites of commenting your HTML in order to write clean, easy to read code. For the purpose of this tutorial, HTML, XML, XHTML validating is not taken into consideration. While I will make an effort to point out non-standardized code, validation has never been my priority and I'll probably miss a few things. Always run your HTML through an HTML validater to check for compatability issues.
Reasons for commenting HTML:
[/tab]- Makes your code easier to trouble shoot.
[tab]- Add a friendly reminder of things you wanted to include in your page.
[/tab]- Allows you to save information about the page without showing the general public.
[tab]- Provides a way of giving yourself credit for creating a nice page.
- Used as instructions for JavaScript and other client side script usage.
Getting Started!
-------------------------------------------------------------------
An HTML comment tag is quite simple, everything inside the tag is hidden from the browser and is considered to be the comment.
The HTML comment tag is:
CODE
<!-- -->
Example with a comment:
CODE
<!-- Here is a comment! -->
Example of a multi-line comment:
CODE
<!-- I would like take this oppurtunity to descript the color of the sky.
The color of the sky is a unique shade of the color blue.
Unless of course there is overcast and then its more like gray.
Then again in the fog evreything is white.
Appearently the sky is multicolored! -->
The color of the sky is a unique shade of the color blue.
Unless of course there is overcast and then its more like gray.
Then again in the fog evreything is white.
Appearently the sky is multicolored! -->
Now lets talk about when and where to comment yout HTML.
The first place I like to add a comment in a web page is at the beginning of a table.
Example:
CODE
<!-- Start table 1 here - 3 rows high and 4 columns wide -->
<TABLE BORDER="1">
<TR>
...
<TABLE BORDER="1">
<TR>
...
Another really good use for a comment in a table is when a cell spans multiple rows or columns.
Say that you have a table that is 4 rows and 4 columns and you want one really large cell in the middle.
Attachment:
Click to view attachment
Comments will help you keep track of where cells used to be.
Example:
CODE
<!-- Table 1 - 4 rows high and 4 columns wide -->
<TABLE BORDER="1">
<TR>
<TD>
Cell 1-1
</TD>
<TD>
Cell 1-2
</TD>
<TD>
Cell 1-3
</TD>
<TD>
Cell 1-4
</TD>
</TR>
<TR>
<TD>
Cell 2-1
</TD>
<TD COLSPAN="2" ROWSPAN="2">
Cell 2-2
</TD>
<!-- Cell 2-3 would have been here -->
<TD>
Cell 2-3
</TD>
</TR>
<TR>
<TD>
Cell 3-1
</TD>
<!-- Cell 3-2 would have been here -->
<!-- Cell 3-3 would have been here -->
<TD>
Cell 3-4
</TD>
</TR>
<TR>
<TD>
Cell 4-1
</TD>
<TD>
Cell 4-2
</TD>
<TD>
Cell 4-3
</TD>
<TD>
Cell 4-4
</TD>
</TR>
</TABLE>
<!-- End table 1 here -->
<TABLE BORDER="1">
<TR>
<TD>
Cell 1-1
</TD>
<TD>
Cell 1-2
</TD>
<TD>
Cell 1-3
</TD>
<TD>
Cell 1-4
</TD>
</TR>
<TR>
<TD>
Cell 2-1
</TD>
<TD COLSPAN="2" ROWSPAN="2">
Cell 2-2
</TD>
<!-- Cell 2-3 would have been here -->
<TD>
Cell 2-3
</TD>
</TR>
<TR>
<TD>
Cell 3-1
</TD>
<!-- Cell 3-2 would have been here -->
<!-- Cell 3-3 would have been here -->
<TD>
Cell 3-4
</TD>
</TR>
<TR>
<TD>
Cell 4-1
</TD>
<TD>
Cell 4-2
</TD>
<TD>
Cell 4-3
</TD>
<TD>
Cell 4-4
</TD>
</TR>
</TABLE>
<!-- End table 1 here -->
I use this frequently as I tend to get confused when I start spanning cells.
Occasionally I have very lengthy forms that I need to comment in order to keep track of what's what.
Example:
CODE
........
<INPUT TYPE="TEXT" NAME="Var126" SIZE="25"> <!-- This is the 126th input variable for this form -->
........
<INPUT TYPE="TEXT" NAME="Var126" SIZE="25"> <!-- This is the 126th input variable for this form -->
........
Comments are great for saving places for all of our content.
For example, say you have an advertiser with a banner and just below his banner you have room for one more banner, just comment that this is where you can place a new add.
Example:
CODE
<!-- Start Foo.com's Banner Code Here-->
<A HREF="http://www.foo.com/foo_sale.html"><IMG SRC=="http://www.foo.com/foo_sale.gif"></A>
<!-- End Foo.com's Banner Code Here -->
<!-- To Quit Losing Money Place Advertisement Here!;) -->
<H1><CENTER>My Title Here</CENTER></H1>
<A HREF="http://www.foo.com/foo_sale.html"><IMG SRC=="http://www.foo.com/foo_sale.gif"></A>
<!-- End Foo.com's Banner Code Here -->
<!-- To Quit Losing Money Place Advertisement Here!;) -->
<H1><CENTER>My Title Here</CENTER></H1>
This works great for code you don't know how to write yet as well.
Example:
CODE
Fill in the form below to order your free money:<BR>
<!-- Use some of my money surplus to learn how to add an HTML form here! -->
If you have any questions <A HREF="mailto:jdoe@foo.com">Email Me!</A><BR>
<!-- Use some of my money surplus to learn how to add an HTML form here! -->
If you have any questions <A HREF="mailto:jdoe@foo.com">Email Me!</A><BR>
Okay here's an example of what didn't work:
CODE
<FONT COLOR="RED"> <!-- Yellow font does not work here you already tried it three times! -->
Another method I use to help keep track of the flow of HTML is indenting.
Example:
CODE
<HTML>
<HEAD>
<TITLE>
My Title.
</TITLE>
</HEAD>
<BODY>
Body content here.
</BODY>
</HTML>
<HEAD>
<TITLE>
My Title.
</TITLE>
</HEAD>
<BODY>
Body content here.
</BODY>
</HTML>
See how easy it is to check if all of your closing tags are present.
This is great if you have tables inside of tables, but will always make reading the code a lot easier.
You may have noticed that all of my tags use uppercase type. I have found that this makes picking the tags out from all of the content text much easier. When I learned HTML, tags were always like this but now validation requires lowercase type as I understand it. You will have to decide for yourself how best to write your code but I suggest that you keep in mind the possibility that someday you may need to convert your HTML to another markup language.
Tells us about your suggestions for commenting.
Happy coding,
vujsa


