Comments

StevenSwiniarski's avatar
Published Mar 21, 2022
Contribute to Docs

HTML comments are used for documentation and excluding markup from being rendered. Like with comments in other languages, developers may also use HTML comments for historical or debugging purposes.

Syntax

HTML comments always start with an opening <!-- and end with a closing tag -->. They may contain anything from text to other markup that will not be rendered.

<!-- Comments go between these tags. -->

Nested comments are not recommended because they may cause unexpected behavior. Comments generally end after the first closing tag -->. Anything after that will be rendered on the page, including all outer closing tags. Thus, when commenting out HTML code, developers should remove any interior comment tags (or at least the trailing ones -->).

<!-- Outer comment <!-- Inner comment --> This text will render. -->

This example will render as follows:

HTML Comments A

Comments can also be placed anywhere inside another element.

<p>Some text that <!-- This is a legal comment --> gets displayed</p>

However, they cannot be placed inside the element tags themselves (e.g. between the left angle < and right angle bracket > of a tag).

<p <!-- This is an illegal comment -->>Some more text.</p>

Example

<p>This will display.</p>
<!--
<p>This will not</p>
-->

This will render as follows:

HTML Comments B

All contributors

Contribute to Docs

Learn HTML on Codecademy