Adding comments in HTML can help you write and organize the backend of your webpage. They’re so useful that it’s considered a best practice to use them.
You can add comments to explain your code, which will make it easier to edit in the future or to work with other developers. You can also use comments to simplify your debugging efforts by “commenting out” lines of code without deleting them.
In this post, we’ll look more closely at what a comment is in HTML, as well as
- how to write HTML comments
- how to write multi-line HTML comments
- how to write inline HTML comments
- how to "comment-out" in HTML
What is a comment in HTML?
In HTML, a comment is a section of text that is not processed by the web browser. Comments are enclosed in <!-- --> tags. These tags tell the browser that the text inside them is a comment and should not be rendered on the front end.
With the comments tag, you can leave notes to remind yourself where you left off in the build process. You could explain the intended functionality of a section of code for another developer or your future self. Or, you might assign someone a task or point out an error for them with a comment.
In short, commenting in HTML helps you work smarter when building or debugging a website. Here's a video that goes into this in more detail:
Now, let's look at an example.
How to Write a Comment In HTML
To leave a comment in HTML, place a <!-- tag before the code and a --> tag after the code that you want to hide. These tags tell browsers to ignore anything between them.
For example, say you’re building a website with a team of developers. You want to leave a note reminding them that all buttons should use the same color.
Here’s what your HTML might look like, as well as the result on the front end:
As you can see, the comment is not rendered on the front end.
You can also quickly comment a line of code with the keyboard shortcut Ctrl + / on PC or Command + /. This method is much faster than typing the tags manually.
How to Write a Multi-line Comment in HTML
To create a comment in HTML over multiple lines, you can use the same method — just enclose your target text inside <!-- --> tags.
How to Write an Inline Comment in HTML
It’s also possible to leave inline comments between active sections of HTML code:
Commenting Out in HTML
Aside from leaving notes to developers (or your future self), HTML comments also come in handy for “commenting out” sections of code. Commenting out is when you temporarily “deactivate” a piece of working code with a comment.
Commenting out has two main purposes. The first is debugging: Upon discovering an error, you can “deactivate” different parts of your code with comments, check if the error is still occurring, and repeat the process until the buggy code is found.
Here’s what “commenting out” a button from the last example would look like:
The second purpose is saving old versions of your code. Since commenting out means the code will remain visible in the back end, it can be a way of keeping old sections of code for developers who are just joining or have inherited the web project.
Say I run some A/B tests and discover that the button isn’t getting any clicks and I want to remove it. In that case, I could comment it out, leaving a note that a CTA button at the bottom page didn’t convert. Then, the next person who comes along and tries to optimize the conversion rate on the page will know to start with another button placement.
Free Guide: 25 HTML & CSS Coding Hacks
Tangible tips and coding templates from experts to help you code better and faster.
- Coding to Convention
- Being Browser-Friendly
- Minimizing Bugs
- Optimizing Performance
You're all set!
Click this link to access this resource at any time.Access now
Leaving Comments in HTML
Comments are another way you can “talk” to people in your code. You can add explanatory notes for other collaborators on a web project, or you can leave yourself notes reminding you to come back to a section or prioritize it during your next redesign.
The best part? Comments will not appear on the front end of your site and they’re simple to master, even if you’re just getting started learning HTML.
Editor's note: This post was originally published in April 2021 and has been updated for comprehensiveness.