As you write code, you’ll sometimes look back at your own work and think, “wait, what was this for again?” For moments like that, HTML has a way to write notes that don’t show on screen. Those are comments.
Let’s write one
<!-- Start of the self-introduction area -->
<p>Hi, I'm Shima, and I love drawing.</p>
<!-- End -->Open this page in the browser and the only thing you see is “Hi, I’m Shima, and I love drawing.” The parts wrapped in <!-- --> don’t appear on screen. But open the code and they’re still there — that’s a comment.
When do you use them?
Here are some times comments come in handy.
| Use | When |
|---|---|
| Leave a note | A word to your future self, like “swap this out for an image later” |
| Mark off sections | A landmark in long code, like “header starts here” |
| Remove temporarily | Wrap a tag you don’t want to delete but don’t want to show right now, to put it “to sleep” |
The third one, putting things “to sleep,” is handy.
<!-- <p>Work-in-progress message</p> -->Wrap a whole tag in <!-- --> and you can hide it without deleting it. Remove the wrapper later and it shows again. Disabling something by wrapping it in a comment like this is called commenting out in the programming world. “I want to try removing this, but I’d be scared if I couldn’t get it back” — for times like that, commenting out instead of deleting means you can always restore it, so you can relax.
One shortcut does it
You don’t have to type <!-- and --> by hand every time. In VSCode, just select the line you want to comment and press Ctrl + / (command + / on a Mac), and the whole line gets wrapped in <!-- -->. Press it again and the wrapper comes off, back to normal. You can toggle “put to sleep ↔ bring back” in an instant, so it’s well worth remembering.
Summary of this lesson
- Wrap comments in
<!--and--> - They’re notes inside the code that the browser doesn’t display
- You can use them as landmarks, or to put a tag to sleep temporarily
A comment is a letter to your future self. Leave them carefully, and the person they help most later is you.