Talking to Yourself

cartoon word bubble

Looking for the lessons? Get started!

Commenting in your HTML code: why and how.

The entire Web Design Principles section can be accessed through the menu below.

Unfortunately, not everyone’s browser will support the spiffy JavaScript menu behind that innocent-looking button. If your browser won’t display the menu, just click on the button and you’ll be taken to a separate page with the entire menu displayed in clear, non-JavaScript HTML.

[C]ommenting is a good habit to get into as it helps you fix problems later. — Ian Lloyd

html icon

You can leave comments to yourself, or the users who want to poke around in your code, simply by using the following format:

<!--here's my comment-->

Use this sparingly. For one, a bunch of comments eat up bandwidth and slow loading time for your pages; for another, using them improperly can “break” your page.

You’ve seen a good example, now here’s two bad examples. Don’t write your comments like these!

<!-------------Wow, what a lot of hyphens! Bet the browser chokes on them------------->

and

<!--This hyphenated comment in the middle---might also cause the browser to choke-->

So when will you use comments? I use them to indicate instances in my code where some large, complex chunk of code is beginning or ending. Or, as Ian Lloyd tells us, they are useful for keeping those pesky DIV tags straight:

      </div><!-- end of inner div -->
   </div><!-- end of nested div -->
</div><!-- end of outer div -->

Designers often use HTML comments to "deactivate" problem chunks of code, to see if they can isolate problems in their HTML. If you think a piece of code is causing your design to throw a hairball, you can do something like this to isolate it:

<!-- Problem code 
   troublesome code
 -->

with the possibly flawed code contained in between. You can display the page with the “deactivated” code in your browser and see if the problem continues to appear with the particular piece of code “turned off.”