Table of Contents
INTRODUCTION
A comment in HTML is a piece of text that is ignored by the browser and not displayed on the web page. It prevents the browser from rendering specific parts of the document.
Even Though HTML comments are not displayed on the page—they're visible in the code. As Browsers ignore them during rendering, it makes them useful for adding notes or explanations about your code and logic.
Places Where Comments can be Used
You can place them in various parts of your document, such as before and after the <!DOCTYPE> declaration or the <html> element.
HTML comments are allowed inside most elements, except for <script>, <style>, <title>, and <textarea>, as these treat their contents as raw text and do not recognize comments.
Syntax of HTML Comments
HTML comments begin with <!-- and end with -->, with the comment text placed in between. Comments in HTML can be written on a single line or span across multiple lines.
The text inside a comment must not start with > or ->, must not contain --> or --!>, and cannot end with <!--, although starting with <! is permitted.
Syntax
<!-- Your comment goes here -->
<!-- This is a single-line comment -->
<!--
This is a multi-line comment.
It can span several lines.
-->
Shortcut for Adding HTML Comments
Here are the keyboard shortcuts to quickly comment or uncomment HTML in popular code editors:
- Windows/Linux: Ctrl + /
- Mac: Cmd + /
Select the line and press the shortcut, will comment the selected lines. Select the same line and Press the same shortcut again, will uncomment the commented line(s).
Use cases
- Code Explanation - Use comments to describe what a section of HTML does—for yourself or other developers.
- Temporarily Disable Code - Comment out elements or blocks of HTML to test or debug layout/content without deleting code.
- Mark Sections or TODOs - Use comments to organize code or leave reminders.
- Version Notes or Metadata - Document updates, authorship, or version info directly in the HTML file.
- Hide Content from Older Browsers - Used in older HTML for compatibility—rarely needed today.
Frequently Asked Questions (FAQ)
Does HTML support nested comments?
<!--
Outer comment
<!-- Inner comment -->
More text
-->
The browser ends the comment at the first -->. The rest is treated as broken HTML, which may be rendered or ignored unpredictably.
How to add Comment in Markdown?
<!-- This is a comment in Markdown -->
These comments:
- Are not rendered in the final HTML output.
- Are invisible to users viewing the page.
- Can be multiline just like in HTML.