Search code examples
htmlcomments

I want to actually show the HTML comment code


I want the output in my window to show the actual "<!-- -->". How do I quote that so it prints out? Same for printing out <h2>Text Here</h2> - I want it to actually print that out, and not do the <h2> style.


Solution

  • Method 1: Escape characters (recommended option)


    &lt; is <

    &gt; is >

    Have a look at this code snippet:

    This will appear as a comment:
    <br><!-- Test -->
    <br>
    <br>This will actually appear:
    <br>&lt;!-- Test --&gt;

    You can find out how to escape characters here: https://ascii.cl/htmlcodes.htm


    Method 2: Use the xmp tag


    Warning: this feature is discouraged by MDN webdocs:

    This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

    Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp

    This will appear as a comment:
    <br><!-- Test -->
    <br>
    <br>This will actually appear:
    <br><xmp><!-- Test --></xmp>