Skip Navigation

Some Comments on HTML Comments

At some point in the distant past, BBEdit added syntax coloring to its long list of useful features. For example, as I type this text, the words I’m writing are colored black, but all the HTML tags are displayed in contrasting colors.

There was one inconsistency about this coloring that puzzled me for years. I actually convinced myself it was a bug in BBEdit until yesterday, when I learned it’s not a bug at all; I just didn’t understand it. What about HTML could I possibly not understand? (A lot, but we don’t have time for that now.) It’s the humble HTML comment. When I write an HTML comment, BBEdit colors the comment text gray, but I noticed that if the comment text contained two hyphens (that’s an em dash in “typewriter”), then the text after the comment suddenly turned gray! It turns out I didn’t know how to write comments.

I always thought that comments began with <!-- and ended with -->, but it’s not that simple. A comment actually begins and ends with two hyphens, so when my text happened to contain two hyphens, BBEdit interpreted that as the end of the first comment, and the --> sequence as the beginning of the next comment, thus turning text after the --> sequence gray as well.

I had always considered the opening of the comment to be <!--, but this sequence is actually two tags. <! is a markup declaration, a kind of generic sequence that tells the browser, “Here’s a tag.” It’s the double hyphens that mark the beginning of the comment. There are documents all over the World Wide Web Consortium explaining this.

It’s interesting that now that I finally understand this, I’ve stopped using double hyphens for em dashes and started using the entity &#8212;. At least there’s nothing wrong with BBEdit. It’s also interesting that every browser got it “wrong” and closed my comments at --> instead of at the next closing tag. I’m thankful for that.

Comment on “Some Comments on HTML Comments”

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)