HTML Comments
Comments are notes in your source code that the browser ignores. They are never displayed on the page, but anyone who views the source can read them. Use them to explain why something is done, to label sections of a long document, or to temporarily switch off a piece of HTML while debugging.
Comment syntax
A comment starts with <!-- and ends with -->:
<!-- This is a comment -->Everything in between is ignored, including tags.
<!-- Page header -->
<h1>Weekend Market</h1>
<!-- TODO: replace with the real opening hours -->
<p>Open every Saturday.</p>Only the heading and paragraph appear. Open the result in developer tools (right-click → Inspect) and you'll see the comments are still in the document — just invisible.
Multi-line comments
A comment can span as many lines as you like:
<!--
Navigation section
Last updated by: Luan
Links must stay in alphabetical order.
-->
<p>
<a href="#about">About</a> |
<a href="#contact">Contact</a> |
<a href="#shop">Shop</a>
</p>Hiding content while testing
"Commenting out" is a quick way to remove something without deleting it. Wrap the HTML in a comment, check the page, then remove the comment markers to bring it back.
<h2>Today's specials</h2>
<p>Tomato soup</p>
<!--
<p>Mushroom risotto</p>
<p>Lemon tart</p>
-->
<p>Green salad</p>Try removing the <!-- and --> lines and running again — the hidden dishes reappear.
You can also hide just a few words in the middle of a sentence:
<p>This product is <!-- not --> available today.</p>Rules and pitfalls
| Rule | Example |
|---|---|
| Comments cannot be nested | <!-- outer <!-- inner --> still outer? --> breaks: the first --> ends the comment |
Avoid -- inside a comment |
<!-- a -- b --> is discouraged and may confuse tools |
| Comments are public | Anyone can read them with View Source |
| Comments add bytes | Huge comment blocks make pages slightly heavier |
Because the first --> always ends a comment, you can't comment out a block that already contains a comment. Remove or rewrite the inner comment first.
Note: Never put passwords, private notes or secrets in HTML comments. They are sent to every visitor's browser along with the rest of the page.
Good commenting habits
- Explain intent, not the obvious.
<!-- paragraph -->above a<p>adds nothing;<!-- Legal text required by the shop's payment provider -->does. - Label big sections so long files are easy to scan:
<!-- ===== Footer ===== -->. - Mark unfinished work with a consistent word like
TODOso you can search for it later. - Clean up commented-out code before publishing; it confuses future readers.
Comments in CSS and JavaScript
Inside <style> and <script> blocks you are no longer writing HTML, so the comment syntax changes:
<style>
/* CSS comment: make notes stand out */
.note { color: darkgreen; }
</style>
<p class="note">Styled by a commented stylesheet.</p>
<script>
// JavaScript single-line comment
/* JavaScript multi-line comment */
document.body.insertAdjacentHTML('beforeend', '<p>Added by a script.</p>');
</script>| Language | Comment syntax |
|---|---|
| HTML | <!-- … --> |
| CSS | /* … */ |
| JavaScript | // … or /* … */ |
Tip: Most code editors toggle a comment on the selected lines with Ctrl+/ (Cmd+/ on a Mac) and automatically use the right syntax for the language you're in.
Test yourself
Which is a valid HTML comment?
// comment/* comment */<!-- comment --><comment>text</comment>
Can visitors see your HTML comments?
- No, they are removed before sending
- Yes, by viewing the page source
- Only in Internet Explorer
- Only if JavaScript is enabled
What happens with <!-- a <!-- b --> c -->?
- Everything is commented out
- The first
-->ends the comment, soc -->is shown as text - Only
bis hidden - The browser shows an error message