<!-- ... -->
The HTML tag <!--...-->
is used to insert comments into the HTML code.
These comments are ignored by browsers and do not appear on the web page. They allow developers to add notes, reminders, or explanations to the code without affecting the document's rendering.
Warning
Comments are visible to anyone inspecting the source code using:
- Right-click > View page source
- Right-click > Inspect
So, never include sensitive information in your comments! 😊
Structure
The HTML comment tag consists of an opening <!--
and a closing -->
. Everything between these two delimiters is considered a comment. Here’s a simple example:
<!-- This is a comment -->
They are also used when you want to temporarily hide an element on the page without removing it from the source code:
<!-- <p>This paragraph is disabled and will not be displayed.</p> -->
Syntax
An HTML comment always starts with <!--
and ends with -->
.
No spaces are required between the delimiters, but it is recommended to add them for readability.
For example, this works:
<!--Here-->
However, we prefer to use this:
<!-- Here -->
Comments cannot be nested.
Attributes
The <!--...-->
tag does not have attributes because it is not a classic HTML tag but a built-in language feature.
Compatibility
The <!--...-->
instruction is compatible with all browsers.
Element | Google Chrome | Safari | Mozilla Firefox | Edge |
<!--...--> | Yes | Yes | Yes | Yes |