โœ๏ธ HTML Text and Content Formatting
Estimated reading: 3 minutes 365 views

HTML Comments Explained: Syntax, Uses, and Best Practices 2025

HTML comments are special annotations within your HTML code that are ignored by web browsers and do not appear on the rendered webpage. They are used to explain, organize, and document code, making it easier for you and others to understand, maintain, and debug your HTML documents.

HTML comments are notes inside the code that donโ€™t appear on the webpage.
Theyโ€™re helpful for explaining, organizing, and debugging your HTML documents .

Fun Fact: Browsers ignore comments completely โ€” they only live in your code!


How to Write HTML Comments

HTML comments start withย <!--ย and end withย -->. Any text placed between these tags is treated as a comment and will not be displayed in the browser.

Syntax

<!-- This is a comment -->

Example

<p>This is a visible paragraph.</p>

<!-- This paragraph is temporarily disabled -->
<!-- <p>This paragraph is hidden using a comment.</p> -->

Types & Uses of HTML Comments

Single-line Comments

Used for brief notes or explanations.:

<!-- TODO: Add contact form here -->

Multi-line Comments

Useful for longer explanations or temporarily disabling sections of code:

<!--
This section contains upcoming features.
Keep hidden until launch.
-->

Inline Comments

Placed within a line of code to explain or temporarily hide part of it.:

<p>This is a paragraph.</p> <!-- Inline explanation -->

Common Use Cases

  • Documenting code sections or logic for yourself or collaborators
  • Adding to-do notes or reminders.
  • Temporarily disabling (commenting out) code for testing or debugging.
  • Hiding content from browsers without deleting it.

Best Practices for HTML Comments

Do Don’t
Use clear, short commentsOver-explain obvious code
Keep comments up-to-dateNest one comment inside another
Clarify complex code blocksUse -- inside comments
Use inline comments wiselyAdd comments inside tag attributes

Note: HTML does not support nested comments and -- (double dash) is not allowed inside them!


Limitations & Rules

  • Comments cannot be placed inside tag attributes or within certain elements likeย <script>,ย <style>,ย <title>, orย <textarea>
  • Comments cannot contain the sequenceย --ย except to close the comment (-->).
  • Browsers completely ignore comments-they are only visible in the source code.

FAQ: HTML Comments

What is the purpose of HTML comments?

Theyโ€™re used to add notes, explanations, and debugging helpers inside HTML code. Great for solo work or collaboration!

Do comments appear in the browser?

Nope! Theyโ€™re invisible to users โ€” only visible in the source code.

Can HTML comments be nested?

No! Only the first --> ends the comment โ€” avoid nesting.

What canโ€™t go inside a comment?

Avoid using -- and placing comments inside tag attributes or within certain elements like <script> or <style>.

Is there a shortcut for adding comments?

Yes!
Windows/Linux: Ctrl + /
Mac: Cmd + /
(Most editors support this!)


Share Now :
Share

๐Ÿ’ฌ HTML Comments

Or Copy Link

CONTENTS
Scroll to Top