Skip to main content

HTML Basics

HTML Comments

Written by Updated HTML Comments exercises

An HTML comment is a note in your code that the browser ignores. Write it between <!-- and -->, and it never shows on the page. It does show in View Source, so never put anything private in one.

The comment syntax in detail is also covered in the comment tag reference.

What Is an HTML Comment?

Comment Example

html

<!-- This is a comment -->

Everything between <!-- and --> is skipped. A comment can be on one line or spread over several.

Single-Line Comment Example

Single-line Comment

html

<p>This is a paragraph.</p>
<!-- This paragraph explains something important -->

The paragraph appears on the page; the comment after it does not.

Multi-Line Comment Example

Multi-line Comment

html

<!--
This is a multi-line comment.
You can write explanations,
notes, or reminders here.
-->
<p>HTML comments are helpful for developers.</p>

Nothing at all appears: the whole block is a comment.

Common Uses of HTML Comments

What comments are good for

  • Explaining why something is done a certain way, for whoever edits the page next, including you in six months.
  • Marking sections of a long page so you can find your way around:

Organizing Example

html

<!-- Header Section -->
<header>...</header>

<!-- Navigation Section -->
<nav>...</nav>
  • Switching code off for a moment while testing, without deleting it:

Example

html

<!-- <p>This paragraph is temporarily hidden.</p> -->

Mistakes to avoid

  • Nesting comments. The first --> ends the comment, so anything after it shows up on the page.
  • Private information. Passwords, internal notes and old prices in comments are readable by anyone.
  • Comment clutter. A comment that just repeats the code, like <!-- paragraph --> above a <p>, adds nothing.
  • The wrong syntax in CSS and JavaScript. Inside <style> use /* ... */, inside <script> use // or /* ... */.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Comments exercises

A comment above a heading

Write the comment Page title and below it a main heading that says Docs.

Result

Check your answer to see it here.