Comments
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:
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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn HTML on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly7 hours