Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to comment in HTML

Understanding the Importance of Comments

Let's start with an analogy. Imagine you're reading a book and you come across a complex paragraph that's hard to understand. It would be great to have a note in the margins explaining it, right? In programming, those notes are what we call 'comments'.

Comments are a vital part of coding, even though they don't affect the way your code runs at all. They are primarily for you and other people reading your code, helping to explain what your code is doing and why you chose to do it that way. In this blog post, we'll be focusing on how to comment in HTML.

The Syntax of HTML Comments

HTML comments start with <!-- and end with -->. Anything between these two symbols is considered a comment and will be ignored by the browser. Let's look at an example:

<!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- Remember to close all your tags! -->

In this example, the text This is a comment and Remember to close all your tags! are comments. They won't appear on the webpage when you load it, but they will be visible in your code for reference purposes.

Single-Line and Multi-Line Comments

There are two types of comments you can make: single-line and multi-line comments.

Single-line comments are comments that occupy only one line in your code. Here's what a single-line comment looks like:

<!-- This is a single-line comment -->

On the other hand, multi-line comments span across several lines. This is what a multi-line comment looks like:

<!--
This is a multi-line comment
It spans across several lines
-->

Using multi-line comments can be beneficial when you need to explain complex code, or when you want to temporarily disable a block of code without deleting it.

Where and When to Use Comments

You might be wondering: where should I put my comments? The answer is: anywhere you think they are needed. Here are some common places where comments are used:

  • At the start of a file: To describe the overall purpose of the code
  • Before a complex piece of code: To explain what it does
  • Before a tricky piece of code: To explain why it's there and how it works
  • To 'turn off' code: If you want to prevent a piece of code from running without deleting it, you can comment it out.

For instance, let's say we have a section of code that creates a button, but we're not quite ready for that button to be functional. Instead of deleting the code, we can comment it out:

<!-- 
<button>Click me!</button>
-->

Remember, the goal of commenting is to make your code easier to understand. So when in doubt, comment out!

Best Practices for Commenting

When commenting your code, there are a few best practices to keep in mind:

  1. Be clear and concise: Make sure your comments are easy to understand, but don't make them unnecessarily long.
  2. Don't state the obvious: Comments should provide new information, not just repeat what the code already says.
  3. Keep them up-to-date: There's nothing more confusing than a comment that contradicts what the code does. Whenever you update your code, make sure to update your comments too.

Here's an example of a well-commented piece of HTML code:

<!-- Main navigation menu -->
<nav>
  <!-- Home link -->
  <a href="/">Home</a>
  <!-- About link -->
  <a href="/about">About</a>
  <!-- Contact link -->
  <a href="/contact">Contact</a>
</nav>

Wrapping Up

Just like writing a note in the margin of a book, commenting your code can make it much easier to understand. Whether you're working on a team or on your own, good commenting practices can save a lot of time and confusion in the long run. So next time you're coding in HTML, remember to leave a comment!