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>
<!--opens the comment: a<, an exclamation mark!, then two dashes--.<p>Promo: ...</p>is what the comment holds. The browser ignores everything between the opening and the closing, including the tags inside.-->closes the comment: two dashes, then>.
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:
- A note for other developers (people who write code), or for yourself later. For example
<!-- Promo off until the price is final -->. - Hiding one part of the page without deleting its code.
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?
- A missing exclamation mark.
<-- note -->is not a comment. The browser shows it as plain text. - Wrapping only the text.
<p><!-- I am invisible --></p>hides the words, but the empty paragraph is still on the page. Wrap the whole paragraph, from<p>to</p>. - Making a copy instead of wrapping. If you leave the original paragraph and write a copy of it inside a new comment, the original still shows.
- A comment inside a comment. You cannot put a comment inside another comment: the first
-->the browser finds closes the comment. Say you want to hide B and C, but there is already a comment between them:<p>A</p><!-- <p>B</p> <!-- note --> <p>C</p> --><p>D</p>. Only B is hidden. Paragraph C shows, and the last-->shows as text. First remove any old comment inside the part you want to hide.
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.