Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to comment out in HTML

Why Commenting Out Code is Important

When you're learning to code, whether it's HTML or another language, one of the most useful tools at your disposal is commenting. Commenting out code means making certain parts of the code inoperative for a certain period. It's like a temporary pause button for your code. Imagine you're watching a movie and you want to pause a scene while you grab some popcorn. You wouldn't want to miss that scene, right? Commenting out code is similar; it allows you to pause certain parts of your code without deleting them.

What are HTML Comments?

In HTML, comments start with <!-- and end with -->. Anything in between these tags is ignored by the browser, and won't affect the final output of your HTML code. Think of HTML comments as invisible notes or instructions that only you (and others who access your code) can see.

Let's look at an example:

<!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- This is another comment -->

Here, "This is a comment" and "This is another comment" won't appear on the web page, but "This is a paragraph." will.

How to Comment Out Code in HTML

Commenting out code in HTML is as easy as wrapping the code you want to make inoperative in comment tags. Here's how you can do it:

<!-- <p>This is a commented out paragraph.</p> -->

In this example, "This is a commented out paragraph." won't appear on the web page because it's surrounded by comment tags.

Why Do We Comment Out Code in HTML?

There are several reasons why you might want to comment out code in HTML:

Testing and Debugging: Sometimes, your code might not work as expected. Commenting out sections of your code can help you identify the problem. It's like being a detective, eliminating suspects one by one until the culprit is found.

Leaving Notes: You can use comments to leave notes for yourself or for other developers. These notes might explain why certain code has been written or how it works. It's like leaving a trail of breadcrumbs to guide your future self or other developers through your code.

Temporarily Removing Elements: If you want to temporarily remove an element from your web page without deleting it, you can comment it out.

Commenting Out Multiple Lines

Sometimes, you may need to comment out multiple lines of code. Instead of wrapping each line in comment tags, you can wrap the entire block of code in a single pair of comment tags. Here's an example:

<!--
<p>This is a paragraph.</p>
<div>This is a div.</div>
<span>This is a span.</span>
-->

In this case, none of the elements will appear on the web page because they're all within the comment tags.

Un-commenting Code

If you want to make commented-out code operative again, you simply remove the comment tags. It's like pressing the play button after you've paused your movie. Here's an example:

<!-- <p>This is a commented out paragraph.</p> -->

To un-comment this code, you would change it to:

<p>This is a commented out paragraph.</p>

Now, "This is a commented out paragraph." will appear on the web page.

In Conclusion

Commenting out code in HTML is a useful tool for testing, debugging, leaving notes, and temporarily removing elements. It's simple to do, and can make your coding life much easier. Remember, it's like using a pause button for your code, and who doesn't love a good pause button?