18

Assuming some HTML like this...

<section>
  <h1>Some stuff</h1>
  <!-- That was some stuff... -->
</section>

I add comment tags around the HTML I want to comment out. I want to comment out everything, but the comment is closed by existing comment.

<!--
<section>
  <h1>Some stuff</h1>
  <!-- That was some stuff... -->
</section>
-->

What is the best way to handle this scenario without losing all my inline comments.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • 2
    Why do you comment everything ? This looks like a source management problem. – Denys Séguret Aug 09 '13 at 11:44
  • 1
    Seconding the Why?; possible duplicate of [Are nested HTML comments possible?](http://stackoverflow.com/questions/442786/are-nested-html-comments-possible) – Alex K. Aug 09 '13 at 11:47
  • 1
    Possible [duplicate](http://stackoverflow.com/questions/442786/are-nested-html-comments-possible). – edsioufi Aug 09 '13 at 11:48
  • 13
    @dystroy It's common during development to want to temporarily comment out a section. If that section happens to contain comments, you'll want them intact so you can revert by uncommenting the section. The fact that this isn't possible in XML is a language problem, not a user problem. Maxime's solution might help with HTML, but it won't work for header blocks / XML in general. – nmclean Aug 09 '13 at 12:38

6 Answers6

16

A HTML comment start with a <!-- and ends at the first --> encountered. There's no way to change this behavior. If you want to hide a large section with may contains comments during the development, you can wrap in a <div style="display:none"></div>. But don't do that in production, it's bad.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • Why is it bad in production? – GreenAsJade Dec 07 '14 at 12:28
  • 3
    Because it can be qualified as "dead code": something that is useless, takes place, make the code less readable... You shouldn't have any code in comments in production because even if *"it could be useful some day"*. http://en.wikipedia.org/wiki/Dead_code – Maxime Lorant Dec 07 '14 at 17:08
  • Thanks - just checking if there was some special badness about this particular way of creating dead code! – GreenAsJade Dec 07 '14 at 21:19
15

To comment block with nested comments: sub inner (block) comments from "--" to "~~"

<!-- *********************************************************************
     * IMPORTANT: to uncomment section
     *            sub inner comments "~~" -> "--" & remove this comment
     *********************************************************************
<head>
   <title>my doc's title</title> <~~! my doc's title ~~>
   <link rel=stylesheet href="mydoc.css" type="text/css">
</head>

<body> 
<~~! my doc's important html stuff ~~>
...
...
...
</body>

*********************************************************************
* IMPORTANT: to uncomment section
*            sub inner comments "~~" -> "--" & remove this comment
*********************************************************************
--> 

thus, outer most comment ignores all "invalid" inner (block) comments

guido
  • 207
  • 2
  • 3
2

As far as I know, there is no way to block that. You need to be careful what you are commenting out or not.

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

What you can try is to use PHP to comment out HTML code... Hope it helped!

MPrazz
  • 569
  • 4
  • 3
0

You cannot comment it out without removing inner comments because HTML will consider the code as

<!--
<section>
----
----            //All this code comes under commented
----
some stuff... -->

It will consider only the beginning comment tag before <section> and end comment tag after "some stuff ...". So HTML will not treat the one comment tag after <h1> which is already under commented.

0

Look for shortcuts in your editor to do nested comments in html.

In VSCode I use this extension: https://marketplace.visualstudio.com/items?itemName=philsinatra.nested-comments

This can with one keypress (un)comment a block of code and do a substitution of <!-- inner-comment --> to <!~~ inner-comment ~~> (just like guido's answer suggests), making commenting and uncommenting blocks just as easy as it is in other languages.

It also works for css /* comments */

There are probably similar extensions for other editors.

julaine
  • 382
  • 3
  • 12
-2

This works for me:

<!--[if False]>
Lots of html including <!-- comments -->
<![endif]-->
Jerry
  • 7
  • 1
  • False. If you add a label infront of last line in will show the text indicating that the closing tag of nested comment indeed closes the outer one – Tadej Magajna Sep 22 '17 at 14:39