HTML Comments: Syntax, Uses and Safety
HTML comments leave notes in a document’s source without displaying them as page content. Learn the comment syntax, when comments help, and what should never go in one.
Quick answer: Start a comment with <!-- and end it with -->. Browsers do not show the comment text as normal page content, but visitors can still inspect the HTML source.
Write an HTML comment
A comment can label a section or explain a decision that may not be obvious to someone maintaining the page.
<!-- Main navigation links -->
<nav aria-label="Main navigation">
<a href="/">Home</a>
</nav>
The comment is visible to someone reading the source, while the navigation element is parsed and displayed by the browser.
When comments help
Use comments sparingly for context that is not clear from the markup itself. For example, a short note can explain why a legacy embed must stay in place or mark the beginning of a complex region. Prefer meaningful element names and simple structure when those make the code clear without extra notes.
Comments are not private
Comment text is delivered to the browser along with the rest of the HTML. Never put passwords, API keys, personal details or confidential notes in a comment. If a secret is published in page source, remove it from the code and replace or revoke the exposed credential.
Comment syntax to avoid
Do not put one HTML comment inside another. The sequence -- is also not valid inside a comment’s text in standard HTML syntax, so keep notes simple and use the closing marker only once.
<!-- TODO: Update the navigation after the redesign -->
HTML comment questions
Do comments affect what visitors see?
Browsers do not render a comment as ordinary page content. The markup around it still works normally, and anyone can inspect the comment in the page source or developer tools.
Can comments be used to hide an element?
Commenting out markup can temporarily remove it from the parsed page, but it remains in the source. Use CSS or a deliberate template change for lasting presentation or content decisions.
Practice: add a maintenance note
Add a short comment above a group of related links that explains their purpose. View the page and confirm the note is not shown as page text. Then inspect the HTML source and confirm that it is still present there.