21

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.

4
  • 2
    Why do you comment everything ? This looks like a source management problem. Aug 9, 2013 at 11:44
  • 2
    Seconding the Why?; possible duplicate of Are nested HTML comments possible?
    – Alex K.
    Aug 9, 2013 at 11:47
  • 1
    Possible duplicate.
    – edsioufi
    Aug 9, 2013 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 9, 2013 at 12:38

6 Answers 6

17

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

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.

4
  • Why is it bad in production? Dec 7, 2014 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". en.wikipedia.org/wiki/Dead_code Dec 7, 2014 at 17:08
  • Thanks - just checking if there was some special badness about this particular way of creating dead code! Dec 7, 2014 at 21:19
  • See the this solution
    – Reza Taba
    Nov 5 at 13:42
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!

1

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.

1
  • great suggestion. Don't know why it didn't occur to me to search for extensions.
    – Keith
    Apr 10 at 15:37
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.

-2

This works for me:

<!--[if False]>
Lots of html including <!-- comments -->
<![endif]-->
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 Sep 22, 2017 at 14:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.