Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to write comments in HTML

Understanding the Importance of Comments

Before we delve into the details of writing comments in HTML, let's understand what comments are and why they are essential. Comments in any programming language, including HTML, serve as explanatory notes for the code. Imagine you are reading a novel, and there are footnotes explaining challenging vocabulary or providing context. Comments in programming languages function similarly, offering valuable insights into the code.

Breaking Down HTML Comments

HTML comments are text that does not affect the actual code execution. They are notes left by developers for themselves or others to explain what the code is doing. In HTML, comments are written between <!-- and -->. The browser ignores everything written within these symbols.

For instance, consider this simple example:

<!-- This is a comment in HTML -->
<p>This is a paragraph of text.</p>

The text within the <!-- --> is the comment, and it's not displayed on the web page. The only visible part on the webpage will be "This is a paragraph of text."

The Significance of Comments in HTML

Now, you might wonder, why do we even need comments in HTML?

Imagine you're a detective and you're handed a case file with no notes or context, just raw data. It would be difficult to understand what's going on, right? Comments are like those case notes. They provide context and help a programmer understand the code quickly.

Without comments, even the author of the code might forget what a particular piece of code does if they return to it after some time. By using comments, developers can save their future selves (or others who might work on their code) a lot of time and confusion.

How to Write Effective Comments

The art of writing effective comments is a skill that develops over time. Here are some general guidelines:

  1. Be concise but clear: Comments should be short and to the point but should clearly explain what the code does.
<!-- This code creates a navigation bar -->
<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
  1. Avoid obvious comments: If the code is self-explanatory, it does not require a comment.
<!-- This is a header --> <!-- This comment is not necessary -->
<h1>Welcome to My Website</h1>
  1. Use comments to explain 'why' and 'how': If a piece of code is complex or uses a workaround, explain why it was done that way and how it works.
<!-- Using flexbox for aligning items horizontally -->
<div style="display: flex;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>

Commenting Out Code in HTML

Sometimes, you might want to prevent a section of your HTML code from executing without deleting it. This is where comments come in handy. By wrapping the code you want to disable within comment tags, you can 'comment out' that code.

For example:

<!-- 
<div>
  <p>This paragraph will not be displayed on the webpage.</p>
</div>
-->

In this example, the entire div block is commented out, and hence, it won't be displayed on the webpage.

Conclusion

Comments are an integral part of coding. They not only help others understand your code but also assist you when you revisit your code after a while. While HTML is a relatively simple language, the practice you get in commenting will prove valuable as you dive into more complex languages. Remember, clear communication is often the key to efficient coding and collaboration. Happy coding!