HTML commentDocument structure

<!-- … -->

A comment in HTML code: a note for humans that the browser doesn’t display, although anyone can see it in the page source.

Display
none (not displayed)
Permitted content
Any text, also across multiple lines, without the sequences -->, --!>, and <!--
On this page

Example

#
HTML
<!-- Lesson 4, content corrected on September 12 -->
<h2>Lesson 4: Lists in HTML</h2>
<p>You create a numbered list with the &lt;ol&gt; tag and a bulleted list with the &lt;ul&gt; tag.</p>

<!--
<p>Homework: make a shopping list for the weekend.</p>
<button type="button">Check the solution</button>
-->

<p>In the next lesson we’ll deal with tables.</p>
Preview

The preview shows only the heading and two paragraphs. The commented-out exercise with the button is still in the code and will return to the page when you remove <!-- and -->.

Definition and usage

#

A comment starts with <!-- and ends with -->. Everything in between is skipped by the browser when displaying the page, tags included. A comment can take one line or many and can stand almost anywhere in the document, even before the <html> tag and after </html>. The only place you can’t put one is inside a tag, for example between attributes.

Comments are used in two ways. The first is notes for yourself and your team: explaining a non-obvious fragment, marking the start and end of a large section, a reminder about something to fix. The second is commenting out, that is, temporarily disabling a piece of code without deleting it. In VS Code you do this with the shortcut Ctrl + / (Cmd + / on a Mac).

A comment ends at the first --> it encounters. That’s why comments can’t be nested: the rest of the outer comment appears on the page as plain text. For the same reason, --> or <!-- can’t occur inside a comment’s text. A missing closing --> is even more dangerous, because the comment then swallows the whole rest of the page. In <style> and <script> elements, CSS comments (/* … */) and JavaScript comments (// …) apply, and in <textarea> and <title> the notation <!-- … --> is displayed literally.

Remember that comments are public. The browser doesn’t display them, but anyone can see them in the page source (e.g. with the shortcut Ctrl + U) and in the developer tools, so don’t leave passwords, API keys, or notes you don’t want to show the world in them. Internet Explorer conditional comments, e.g. <!--[if IE]> … <![endif]-->, used to be popular too. Modern browsers treat them as ordinary comments, and Internet Explorer itself stopped understanding them in version 10, so today you’ll mostly meet them in email templates for classic Outlook on Windows.

Syntax

#
Syntax
<!-- Comment text -->

<!--
  A comment
  across several lines
-->

More examples

#
Comments can’t be nested
HTML
<!-- Old price list, to be removed
  <!-- Pro plan, price to be determined -->
  <p>Pro plan: $49 per month</p>
-->
<p>Basic plan: $0</p>
Preview

The outer comment already ends at the first -->, right after the words “to be determined”. That’s why the paragraph with the Pro plan and the final --> are displayed on the page, even though they were meant to be hidden.

An Internet Explorer conditional comment
HTML
<!--[if IE]>
  <p>You are using Internet Explorer. Update your browser to use the course.</p>
<![endif]-->
<p>Every browser sees this paragraph.</p>
Preview

For a modern browser, the whole block from <!--[if IE]> to <![endif]--> is one ordinary comment, so the preview shows only the second paragraph.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Comment on what isn’t visible in the code itself, namely why something was done one way and not another. A comment saying “paragraph” above a <p> element adds nothing.
  • Keep an HTML fragment that a script will insert into the page later in a <template>, not in a comment. The browser understands template contents as HTML, so a script can easily copy it.
  • In JSX, that is, in React components, HTML comments don’t work. There you write {/* comment */}.

Related entries

#

Spotted a mistake or missing an example? Write to us.