Most of the time when you are marking up the content for your web page using (X)HTML you will be adding the minimum necessary tags around your content to clearly identify what that content is and then styling that content as necessary from your stylesheet. In some cases you will have a prewritten block of code that needs to be added to your page exactly as supplied eg. to add advertising code to your page or perhaps a prewritten Javascript or counter.

Where you add prewritten code supplied by someone else you may wish to clearly mark where that code starts and finishes in order to ensure that you don't alter the supplied code by mistake (they may require that you use it exactly as supplied) or perhaps in order to make it easier to find and remove at a later date.

In identifying where such a block of code starts and finishes as well as where it came from and what it is there for we don't want to have anything actually display in our actual web page when it is displayed by the web browser. What we actually want to do is to add a comment into the source of our web page that contains whhatever description that it is that we require. (X)HTML provides a special comment tag that we can use to add comments containing whatever information that we want to add to any spot within our web page. This tag is a bit different from the other tags and containers that we have looked at so far because it doesn't have separate start and end tags to mark the start and end of the comment but instead actually contains the entire comment within the tag.

A comment always starts with the same four characters and ends with the same three characters and can have whatever you like in between. The browser will then ignore everything within the comment when displaying the web page.

<!-- comment -->

A comment always starts with a less than symbol followed by an exclamation mark and two dashes. It then extends through until it finds two dashes followed by a greater than symbol which marks the end of the comment. An XHTML web page will work exactly the same if all such commented content is removed from the page. There are two situations where HTML pages behave differently from this and that is where the inline content of a script or style tag are contained within a comment. The HTML page allows the script and style commands to be processed by the appropriate processor that handles those commands even though they are commented out as far as the HTML is concerned. XHTML considers them to be comments and so removes them from the content before processing the page regardless of where the comments appear.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow