How to Comment in HTML (Single-Line, Multi-Line and Shortcuts)
Contents
Short answer: wrap the text in <!-- and -->. The browser skips everything between them, so comments never show up on the page.
That’s the whole syntax. Below: multi-line comments, commenting out code (and the one thing that breaks it), the keyboard shortcut, the rules for what a comment can contain, and how comments work in JSX, Vue and PHP templates.
HTML comment syntax
A comment starts with <!-- and ends with -->. Spaces inside are optional but make it easier to read. Comments can go almost anywhere in the document, except inside a tag itself.
<!-- This is a comment -->
<p>This paragraph is visible.</p>
<p>Visible <!-- an inline comment --> text.</p>
Multi-line comments
HTML has no separate single-line syntax: the same <!-- --> spans as many lines as you like. That makes it handy for notes at the top of a template.
<!--
Product card
Used on: home page, category pages
Data comes from the products API
-->
<article class="card">...</article>
Comment out code
To hide part of the page while you debug or test, wrap the markup in a comment. The browser doesn’t render it, but it’s still in the file, so you can bring it back in a second.
<nav>
<a href="/">Home</a>
<!-- <a href="/shop/">Shop</a> -->
<a href="/blog/">Blog</a>
</nav>
<!--
<section class="promo">
<h2>Black Friday sale</h2>
</section>
-->
You can't nest HTML comments
A comment ends at the first -->. If the code you comment out already contains a comment, its closing --> ends your outer comment early, and the rest spills onto the page. Remove the inner comment first, or comment out the pieces around it separately.
<!--
<div class="banner">
<!-- inner comment --> ← the outer comment ends here
<p>This text now shows up on the page</p>
</div>
--> ← and this shows up as text
Keyboard shortcut to comment in HTML
Every code editor can toggle comments for you. In an HTML file they insert <!-- -->; inside a <style> or <script> block they use the right syntax for CSS or JavaScript.
| Editor | Windows / Linux | macOS |
|---|---|---|
| VS Code (and Cursor) | Ctrl + / |
Cmd + / |
| VS Code block comment | Shift + Alt + A |
Shift + Option + A |
| JetBrains (WebStorm, PhpStorm) | Ctrl + / |
Cmd + / |
| Sublime Text | Ctrl + / |
Cmd + / |
Select several lines first to comment them all out at once. Press the shortcut again to remove the comment.
What you can’t put inside an HTML comment
The HTML standard has a few rules for the text of a comment. Browsers recover from breaking them, but the result is rarely what you meant. The comment text must not:
- start with
>or->(so<!-->is an empty, already-closed comment) - contain
<!--,-->or--!> - end with
<!-
Two dashes in the middle, like <!-- a -- b -->, are allowed in HTML5. Old XHTML and XML parsers reject them, so avoid them if your markup ever goes through an XML tool.