Old websites sometimes wrap JavaScript or CSS in HTML comment markers. Modern HTML does not need that trick, and it can create confusing parsing and debugging behavior.
What You Need to Know
Use JavaScript comments inside script code and CSS comments inside style code. Do not use HTML comments as a substitute for the language's own comment rules.
The goal is not to collect as many tags as possible. Pick the element that best describes what the content really is.
Example: See It in Action
Here is a simple example to experiment with. Type it, save the file, and check what the browser displays.
If this is only part of a document, place metadata and style elements in the head of a complete HTML page. Keep ordinary visible content in its body.
<style>
.message { color: rebeccapurple; }
</style>
<script>
console.log('Use JavaScript comments inside scripts.');
</script>
Let’s Understand the Example
- The style element holds CSS rules that affect appearance, not the meaning of the HTML.
- A script adds JavaScript or declares another kind of script data. HTML alone does not run most interactive behavior.
Look for the text “.message { color: rebeccapurple; }” in the page. If the visible result surprises you, inspect the rendered elements to see what the browser actually built.
What Should You See?
The page should include “.message { color: rebeccapurple; }”. If it looks different from what you expected, open developer tools and compare the source with the elements the browser created.
Try removing one piece of markup in a scratch copy, refresh the page, and compare the behavior. This is often the fastest way to understand what that piece contributes.
Try It Yourself
- Add one JavaScript comment and one CSS comment, then remove any unnecessary HTML comment wrappers.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Putting <!-- inside modern JavaScript as a normal code comment without checking its meaning.
- Avoid picking tags just for their default bold, italic or spaced appearance. Use CSS for presentation.
Key Takeaways
- Old websites sometimes wrap JavaScript or CSS in HTML comment markers.
- The style element holds CSS rules that affect appearance, not the meaning of the HTML.
- Use JavaScript comments inside script code and CSS comments inside style code. Do not use HTML comments as a substitute for the language's own comment rules.