Are nested HTML comments possible?

Viewed 47548

as per the title; is it possible to have nested comments in valid HTML? see the example below...

<p>some text</p>

  <!-- comment 1

    <p>commented out html</p>

    <!-- comment 2

      // are nested html comment allowed?

    end of comment 2 -->

    <p>more commented out html</p>

  end of comment 1 -->

<p>some more text</p>

It appears not, does anybody know how I could get nested comments to work?

11 Answers

When you nest a comment, replace "--" with "- -". When you un-nest, reverse the procedure. It's not the <!-- that is forbidden but the --.

Example:

<!-- some stuff
<!- - some inner stuff - ->
<!- - a sibling - ->
the footer -->

Use template tag. The fastest way to block all the comment and other html from showing up.

<template>
    <!-- first paragraph-->
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    <!-- second paragraph-->
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</template>

    <!-- third paragraph-->
    Ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.sunt in culpa qui officia deserunt mollit.

It cannot be done. --> will always end an existing HTML comment.

Nested HTML comments <!-- comment1 <!-- comment2--> --> are NOT allowed.

But you could achieve by using <script> tag and /* */ (Hacky solution):

<script>
/*
  HTML code you want to comment on goes between the Javascript comment section
*/
</script>

Note: Also you could have more than one <script>...</script> pair's in the same HTML file for commenting out the different parts of the code.

Example: Below comments the "Some Statement 2, 3, 4, 5 and 6"

<p> Some Statement 1 </p>

<script>
/*
<p> Some Statement 2 </p>

<!-- explanation about below statement 3 by the comment tag -->
<p> Some Statement 3 </p>

<!-- 
<p> Some Statement 4 </p> 
<p> Some Statement 5 </p>
-->

<p> Some Statement 6 </p>
*/
</script>

<p> Some Statement 7 </p>

A VS add-in that fakes nested comments by automatically converts <!--...--> to <!~~...~~> then comments out that whole section. It lets you toggle it on and off.

nested-comments

Some editors have commenting/uncommenting commands which can automatically handle existing comments in a block of text. Visual Studio e.g. is doing that when you press Ctrl+KC and Ctrl+KU.

I think it isn't allowed, but as far as I know it works in most of the major browsers except in Firefox.

Related