HTML Comments: How to Use Them

Download Now: 25 Free HTML & CSS Hacks
Anna Fitzgerald
Anna Fitzgerald

Published:

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.

two people writing html comments in an office

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

Download Now: 50 Code Templates [Free Snippets]

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:

See the Pen comment example by HubSpot (@hubspot) on CodePen.

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 + / on Mac. This method is much faster than typing the tags manually.

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

50 Free Coding Templates

Free code snippet templates for HTML, CSS, and JavaScript -- Plus access to GitHub.

  • Navigation Menus & Breadcrumbs Templates
  • Button Transition Templates
  • CSS Effects Templates
  • And more!
Loading your download form

You're all set!

Click this link to access this resource at any time.

Download Now
Learn more

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.

See the Pen comment example - multi-line by HubSpot (@hubspot) on CodePen.

How to Write an Inline Comment in HTML

It’s also possible to leave inline comments between active sections of HTML code:

See the Pen comment example - inline comment by HubSpot (@hubspot) on CodePen.

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:

See the Pen comment example: commenting out by HubSpot (@hubspot) on CodePen.

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.

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.

New Call-to-action

Topics: HTML

Related Articles

Pop up for DOWNLOAD NOW: 25 FREE HTML & CSS HACKS 25 FREE HTML & CSS HACKS

Tangible tips and coding templates from experts to help you code better and faster.

    CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

    START FREE OR GET A DEMO