Does HTML5 change the standard for HTML commenting?
Indeed, HTML5 holds onto the HTML comment syntax: <!-- comment here -->
. Here are your top rules:
<!-->
is a no-no.--
inside comments is bad news; it can cut your comment short.- Commenting within
<script>
or<style>
? Heck no, stick to language-specific comments like//
or/* */
.
Your perfect HTML5 comment:
Ensure your comments are both useful and standard-compliant for readability and interoperability across an assortment of browsers and tools.
Commenting practicalities
Utilize comments to clarify your code and render it more readable. Here are a few legit reasons to add comments:
- Unraveling convoluted code sections
- Highlighting to-dos or fixmes
- Temporarily sideline a code block from execution without deleting it
- Scribing documentation for other coders
However, exercise restraint. Overzealous commenting can muddle your code. Strive to craft clear, self-explanatory code whenever feasible.
Beware of browser and validator quirks
Diverse browsers and validation instruments might play sly tricks with non-standard comments. Here's what might happen:
- Chrome and a number of IDEs might accept
<!--- This --->
. - Validator.nu seems cool with
<!-- This is a> comment -->
.
Keep in mind, such behaviors are inconsistent and can spawn cross-browser issues. To stay on the safe side, stick with the tried and tested: <!-- comment -->
.
Markup declarations, not to be overlooked
Did you notice the exclamation mark (!
) in <!-- Comment -->
? This perky character holds a special position. It's a markup declaration signifying that the content within <!--
and -->
isn't your run-of-the-mill content. In this case, it marks a comment, but it moonlights in DOCTYPE declarations and conditional comments in older IE versions. Clever, isn't it?
Uncommon comments and clarity: A love story?
While some developers might fancy dabbling in non-traditional commenting such as <!-- This -- is a comment -->
, it's prudent to prioritize clarity. Convoluted commenting can lead to misinterpretation and should be sidestepped where possible.
Was this article helpful?