Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML nested comments

Tags:

comments

html

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.

like image 294
Billy Moon Avatar asked Aug 09 '13 11:08

Billy Moon


People also ask

Can you have nested comments in HTML?

Look for shortcuts in your editor to do nested comments in html. 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.

How do I make multiple comments in HTML?

--...--> Tag.

How do you comment out comments in HTML?

The HTML Comment Tag Comments in HTML start with <! -- and end with --> . Don't forget the exclamation mark at the start of the tag! But you don't need to add it at the end.


3 Answers

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.

like image 53
Maxime Lorant Avatar answered Oct 20 '22 12:10

Maxime Lorant


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

like image 24
guido Avatar answered Oct 20 '22 11:10

guido


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!

like image 20
MPrazz Avatar answered Oct 20 '22 11:10

MPrazz