The Tutorial SIte

Menu
  • Bootstrap 4
  • CSS
  • HTML
Home
HTML
HTML Comments – A Guide
HTML

HTML Comments – A Guide

HTML comments are an essential part of web development, allowing developers to add notes, explanations, or disable code without deleting it.

Comments do not affect the rendered page and are ignored by browsers, but they help in code readability, debugging, and collaboration.

In this tutorial, we will cover:

  1. Basic Syntax of HTML Comments
  2. Uses of HTML Comments
  3. Commenting Out Code
  4. Multiline Comments
  5. Best Practices for HTML Comments
  6. Using HTML Comments for Debugging
  7. Hiding JavaScript and CSS in Old Browsers
  8. Comments in Other Web Technologies

1. Basic Syntax of HTML Comments

An HTML comment is written inside <!– and –>.

Example: Basic Comment

<!-- This is a comment -->

This will not be displayed in the browser but remains visible in the source code.

Example: Commenting Inside HTML Code

<p>This is visible.</p>
<!-- <p>This is hidden.</p> -->

Only “This is visible.” will appear on the webpage.

2. Uses of HTML Comments

Comments are useful in several ways:

  1. Adding Explanations – Helps developers understand code structure.
  2. Temporarily Disabling Code – Allows quick removal of elements without deleting them.
  3. Debugging – Helps in testing different parts of a webpage.
  4. Collaboration – Assists teams by providing contextual information.
  5. Organizing Code – Makes large codebases easier to navigate.

3. Commenting Out Code

HTML comments can be used to disable certain elements while keeping them in the source code.

Example: Disabling a Section

<!-- 
<h1>Welcome to My Website</h1>
<p>This section is temporarily hidden.</p> 
-->

The h1 and p elements will not be displayed on the webpage.

4. Multiline Comments

HTML comments can span multiple lines.

Example: Multiline Comment

<!--
This is a multiline comment.
It can span multiple lines to describe a section in detail.
-->

Example: Commenting Out Multiple Elements

<!-- 
<nav>
    <ul>
        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
    </ul>
</nav> 
-->

The entire navigation section will be hidden from display but remains in the source code.

5. Best Practices for HTML Comments

1. Keep Comments Concise

Comments should be short and to the point.

<!-- Main content section -->
<section>
    <p>Content goes here.</p>
</section>

Avoid unnecessary comments:

<!-- This is a section element -->
<section>
    <p>Content goes here.</p>
</section>

If the tag name itself is self-explanatory, avoid redundant comments.

2. Use Comments to Separate Sections

For large pages, use comments to divide different sections.

<!-- Header Section -->
<header>...</header>

<!-- Main Content -->
<main>...</main>

<!-- Footer Section -->
<footer>...</footer>

3. Do Not Overuse Comments

Too many comments can make the code cluttered.

Bad Practice:

<!-- This is a div -->
<div>
    <!-- This is a paragraph -->
    <p>Some text</p>
</div>

Good Practice:

<!-- Hero Section -->
<div>
    <p>Some text</p>
</div>

6. Using HTML Comments for Debugging

When testing a webpage, temporarily removing code helps in debugging issues.

Example: Debugging a Button

<!-- Temporarily disabling the button -->
<!-- <button>Click Me</button> -->

If the button is causing layout issues, commenting it out can help identify the problem.

7. Hiding JavaScript and CSS in Old Browsers

Older browsers may not support JavaScript or CSS properly. HTML comments were historically used to hide JavaScript or CSS from those browsers.

Example: Hiding JavaScript in Older Browsers

<script>
<!-- 
document.write("This is JavaScript code.");
-->
</script>

Modern browsers ignore this since they properly recognize <script> tags.

Example: Hiding CSS in Old Browsers

<style>
<!-- 
body {
    background-color: lightgray;
}
-->
</style>

Again, modern browsers do not require this.

8. Comments in Other Web Technologies

1. CSS Comments

CSS uses /* */ for comments.

/* This is a CSS comment */
body {
    background-color: white;
}

2. JavaScript Comments

JavaScript supports:

  • Single-line comments: //
  • Multiline comments: /* */
// This is a single-line comment
console.log("Hello, World!");

/*
This is a multiline comment.
It spans multiple lines.
*/
console.log("Goodbye, World!");

3. XML Comments

Similar to HTML, XML comments use <!– –>.

<!-- This is an XML comment -->
<note>
    <to>User</to>
    <from>Admin</from>
</note>

Share
Telegram
Linkedin
Pocket
Pinterest
Tweet
Email
Prev Article
Next Article

Related Articles

HTML Paragraphs – A Guide
HTML paragraphs help structure text content on a webpage. The …

HTML Paragraphs – A Guide

HTML Favicon : A Tutorial
A favicon (short for “favorite icon”) is a small image …

HTML Favicon : A Tutorial

Categories

  • Bootstrap 4
  • CSS
  • HTML

Latest

  • HTML Classes : A Tutorial
  • HTML Tables : A Tutorial
  • HTML iframe : A Tutorial
  • HTML div : A Detailed Tutorial
  • Bootstrap 4 Tutorial Collection
  • CSS Tutorial Collection
  • HTML Tutorial Collection
  • HTML head : A Detailed Tutorial
  • Popular
  • Recent
  • Tags

The Tutorial SIte

Copyright © 2025 The Tutorial SIte
The Tutorial Site

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh