31

Is there a way to comment multiple lines... which already have some comments in them?

i.e.

<html>
<!-- Multi-line comment begin
  <head>
    <!-- This script does abcxyz -->
    <script>...</script>
  </head>
  <body>
    Hello world!
  </body>
Multi-line comment end -->
</html>

It seems that even SO's syntax hilighting won't accept this...

Tony R
  • 11,224
  • 23
  • 76
  • 101
  • It's a little annoying, but you can leave the other comments in by just removing the `-->` part of each one. – naught101 Jan 14 '19 at 01:43

7 Answers7

12

I think the key point is this:

Note that comments are markup.

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

This is not valid markup:

<div <span/> />

... so neither is the one you mention.


Since all my sites are written in PHP I normally comment out code with PHP comments:

<?/*?>
<div>...</div>
<p>...</p>
<?*/?>

Perhaps you can use a similar trick.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 7
    sheesh, really? Am I the only one aggravated by this? o.O Thanks for the php advice though... – Tony R Sep 22 '10 at 21:33
7

No. Comments cannot be nested and HTML has only one style of comment.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
5

No. The closing comment tag --> will always end the comment section so if your comment includes a comment the closing tag of your included comment will end the comment section.

You can do a replace of --> in the section you are about to comment out to something unique so you can later just do another replace back to --> if you choose to undo your commenting.

Florian
  • 538
  • 4
  • 11
  • Lolz. I guess doing this works...: `<--blahblah<--moreblahblah--` (then when you uncomment you get: `blahblah<--moreblahblah--). – Andrew Feb 19 '18 at 17:52
5

If you're really stuck with some piece of HTML – pre-rendered at some uncontrollable source – which contains comments, and you need to make sure none of it is rendered on your page, you can always wrap it with a script tag like below, only thing is, you can't comment out script tags this way.

 <html>
   <head>
   </head>
   <body>
     <!-- multiline "comment" below using script type="text/html" -->
     <script type="text/html">        
        Hello world!
        <!-- Look at me, I'm a comment :) -->
        <div>Yeah, whatever, I'm an element..</div>        
    </script>
    <span>Who cares, span is the man, the only visible one anyway!</span>
  </body>
</html>

If you need to comment out script tags, you could use a textarea as wrapper instead, off course doing it this way, you can't comment out textarea tags.

<html>
  <head>
  </head>
  <body>
    <!-- multiline "comment" below using textarea style="display:none;" -->
    <textarea style="display:none;"> 
      <script>  
        alert("which won't show up..");  
      </script>
      Hello world!
      <!-- Look at me, I'm a comment :) -->
      <div>Yeah, whatever, I'm an element..</div>        
    </textarea>
    <span>Who cares, span is the man, the only visible one anyway!</span>
  </body>
</html>
Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
3

Nope, unfortunately HTML comments don't nest.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
0

it may still be useful for some developers, if use use vsCode as your IDE you can use an extension named Nest Comments in visual studio code market which work like a charm.

this is the link Nest Comments

Moni
  • 826
  • 10
  • 15
0

One can embed it in single or double quotes as a tag attribute. This requires then of course that the corresponding quotes are not occurring in between.

<html>
<br comm='Multi-line comment begin
  <head>
    <!-- This script does abcxyz -->
    <script>...</script>
  </head>
  <body>
    Hello world!
  </body>
Multi-line comment end'>
</html>

http://jsfiddle.net/cehfumb2/2/