Skip to main content

HTML Tags

HTML Comment Tag

Written by Updated HTML Comment Tag exercises

An HTML comment is a note for people reading the code. The browser skips it, so it never shows on the page, but anyone can still see it with View Source.

Syntax

html

<!-- This is a comment -->

Everything between <!-- and --> is ignored. Comments have no attributes.

When comments are useful

  • Explaining why. "Hidden until the sale starts on 1 March" helps the next person more than a comment that repeats what the code already says.
  • Marking sections in long pages, like <!-- Footer -->, so you can find your way around.
  • Switching code off for a moment while you test something, without deleting it.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Comment Example</title>
</head>
<body>
  <h2>HTML Comment Example</h2>

  <!-- This is a comment and will not be displayed in the browser -->
  <p>This paragraph is visible to the user.</p>

  <!-- 
    Multiple line comment:
    1. This is a note for developers.
    2. Can temporarily disable code.
  -->
  <p>This paragraph is also visible.</p>
</body>
</html>

Only the two paragraphs appear on the page. Both comments, the single-line one and the one spread over several lines, are invisible.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Things that catch people out

  • Comments are public. Anyone can open the page source and read them. Never leave passwords, API keys, private notes or "TODO: fix this security hole" in HTML comments.
  • You cannot nest comments. The first --> ends the comment, so <!-- outer <!-- inner --> outer --> leaves the text "outer --&gt;" showing on the page.
  • Do not put -- inside a comment. Keep the text between the markers simple.
  • CSS and JavaScript use different comments. Inside a <style> block, write /* ... */, and inside a <script>, write // or /* ... */. An HTML comment there does not work the way you expect.

For more examples, see HTML Comments.

HTML Comment Tag exercises

Write a comment

Write the comment Page title, and below it the level 1 heading Welcome. The comment must not show on the page.

Result

Check your answer to see it here.