Comments — HTML

An HTML comment is written <!-- text --> and doesn't show on the page. Use a comment for notes or to hide an element: wrap the element in <!-- and -->, from…

Remember the line <!-- Write 3 lines of headings here --> in the heading exercise? It was in the code, but it never showed on the page. That's a comment: a note inside HTML code that the browser doesn't show. Only people who read the code can see a comment, like a pencil note in the margin of a book that only the book's owner sees. A person who opens the page doesn't see the comment.

Writing a comment

Here is a recipe page. Its promo paragraph is hidden with a comment:

<h1>Fried Rice Recipe</h1>
<p>You need: cooked rice, shallots, garlic, sweet soy sauce.</p>
<!-- <p>Promo: free delivery on spice orders until Sunday.</p> -->
<p>Fry the spice paste until fragrant, then add the rice and soy sauce.</p>

The page shows only the title and two paragraphs: the ingredients and the cooking steps. The promo paragraph is still in the code. It just doesn't show.

Comments are used for two things:

To hide one element, write <!-- right before its opening tag and --> right after its closing tag. The element stays where it is. Don't write a copy of it in a new comment, because the original element would still show. Wrap the element that is already there.

A comment can also take several lines, as long as it starts with <!-- and ends with -->. In most code editors (programs for writing code), such as VS Code, Ctrl + / (Cmd + / on a Mac) turns the selected lines into a comment, or back into code.

Forget the --> and the rest of the page disappears

Once the browser finds <!--, it keeps looking for -->. If there is no closing, the browser treats everything after <!-- as a comment, up to the end of the page:

<!-- <p>Promo: free delivery on spice orders until Sunday.</p>
<p>Fry the spice paste until fragrant, then add the rice and soy sauce.</p>

Here the paragraph with the cooking steps disappears from the page too. So always write the opening and the closing as a pair.

HTML only knows <!-- -->

// is a comment mark in JavaScript. /* */ is a comment mark in JavaScript and CSS. # is a comment mark in other languages, such as Python. In HTML these marks are not comments, so the mark and its note both show on the page.

Hidden, but not secret

A comment is sent to the visitor's browser with the rest of the page's code. Anyone can right-click the page, choose "View Page Source" (see the page's code) and read every comment. So never write a password, an API key (a secret code for using a service) or a private note in a comment.

⚠️ Why does the part you hid still show?

TL;DR: An HTML comment is written <!-- text --> and doesn't show on the page. Use a comment for notes or to hide an element: wrap the element in <!-- and -->, from its opening tag to its closing tag. If you forget the -->, the rest of the page disappears too. //, /* */ and # are not HTML comments. Anyone can still read a comment in the page's code (the View Page Source menu), so never write a secret there.

⚡ A note that doesn't show

Write:

<h1>Fried Rice Recipe</h1>
<!-- <p>Promo: free delivery on spice orders until Sunday.</p> -->
<p>Fry the spice paste until fragrant, then add the rice and soy sauce.</p>

Meaning: Everything between <!-- and --> is a comment. The browser ignores it, including the tags inside, so the promo paragraph doesn't show. Want to hide an element? Wrap that element from its opening tag to its closing tag.

⚡ Forget the closing

Guess first: if you forget the -->, what disappears?

Everything after <!--, up to the end of the page. The browser keeps looking for -->, so the paragraph with the cooking steps below it becomes part of the comment too.

Meaning: Always write <!-- and --> as a pair.

⚡ HTML only knows <!-- -->

Guess first: does // note show in HTML?

It does. // is a comment mark in JavaScript, /* */ in JavaScript and CSS, # in other languages such as Python. In HTML, the mark and its note both show on the page.

Meaning: A comment doesn't show, but it isn't secret. Anyone can read it in the page's code (the View Page Source menu), so never put a password there.

What you will learn