An HTML comment is text between <!-- and --> that the browser ignores when rendering the page. Use html comments to leave notes for yourself and other developers, to mark sections of a long file, or to temporarily switch off a piece of markup while testing. Anyone can still read them with View Source, so they must never contain secrets.
A comment starts with <!-- and ends with -->. Everything between them is skipped by the renderer:
Comments can go almost anywhere in a document: before the doctype is not recommended, but inside <head>, <body> and between elements is fine. They cannot go inside a tag's own angle brackets. This is invalid:
The page shows the paragraph, and a comment does not become part of the text. In the DOM, though, a comment is a real node (type Comment), so JavaScript can reach it, but it never affects layout.
Comments are not displayed, but they are downloaded. You can see them in View Source and in the Elements panel of DevTools. That has two consequences:
Comments are also invisible to search engines for ranking purposes, so do not use them to hide keywords.
Comments make large files easier to navigate, especially closing tags of deeply nested layouts:
Wrap markup in a comment to hide it without deleting it:
Most editors toggle comments for you with Ctrl+/ on Windows and Linux or Cmd+/ on macOS.
A short <!-- TODO: replace with final logo --> is common during development. Remove or resolve such notes before release.
--> ends the comment, so the rest of the outer comment is shown as text:> or ->, and must not contain <!--, -->, --!> or end with <!-. Keep comment text plain and avoid double hyphens inside.<!doctype html>. Doing so can trigger quirks mode in older browsers./* ... */, JavaScript uses // and /* ... */, and a <script> or <style> block follows its own language's rules, not HTML comment syntax.Comments should explain why, not repeat what the markup already says. Compare:
| Weak comment | Better comment |
|---|---|
<!-- div --> | <!-- Wrapper keeps sidebar and content side by side --> |
<!-- image --> | <!-- Hero image: 1600px wide for retina screens --> |
Keep them short, keep them current, and delete commented-out code you no longer need, because version control already stores the history.
There is also a special kind of comment you may meet: conditional comments such as <!--[if IE]>. They only worked in old Internet Explorer versions, which are no longer supported, so you should not write them in new pages.
Yes, anyone can view them through View Source or DevTools. They are only hidden from the rendered page.
In most editors, including VS Code, select the lines and press Ctrl+/ (or Cmd+/ on macOS). The editor wraps them in the comment markers.
No. The first --> closes the comment, so the remaining text appears on the page.
Only slightly, because they add bytes to the download. Build tools and minifiers usually remove them from production files.
Write an HTML comment with <!-- to start and --> to end. Use comments to explain why markup exists, label sections and disable code temporarily. They are visible in View Source, they cannot be nested, and they must never contain secrets. Remove stale comments and commented-out code, and let your build step strip the rest for production.