Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nested HTML comments possible?

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?

like image 784
QAZ Avatar asked Jan 14 '09 12:01

QAZ


People also ask

Can we have comments inside comments?

Yes.In CPP you can do it like this: /* Comment 1 // Comment 2 Comment 3 . . . */ But Normally It isn't used cause it may confuse you. @Alexandra if you often have this problem you should do like @Alphonso gives an example of and just use // for commenting code and then you will be able to use /* ... */ for debugging.

How do you put a comment in a comment in HTML?

How to Write Inline Comments in HTML. You can also add comments in the middle of a sentence or line of code. Only the text inside the <! -- --> will be commented out, and the rest of the text inside the tag won't be affected.

What is comment nesting?

A nested comment is a comment inside another comment.


2 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 --> 
like image 173
Aaron Digulla Avatar answered Oct 15 '22 15:10

Aaron Digulla


TL;DR: Unfortunately, no, it's not possible (and never will be).

Short answer:

An HTML comment is not quite what many think it is. HTML is a form of SGML, in which comments are delimited by pairs of double-dashes (-- … --).

Thus, any pair of a double-dashes inside a pair of angle brackets with an exclamation point after the opening bracket (<! ---- >) is a comment. The spec says it better than I can: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

This is why comments like this one (which we've all likely done one time or another) are a bad idea:

<!-- ------------------ HEADER BEGINS HERE -------------------- -->

Truth: I am too lazy to tell you how many comments are represented by the above tag pollution, but it's at least 10.

I got less lazy: This so-called "comment" actually consists of 10 comments, three words outside any comment (i.e., just bad SGML), and the beginning of a comment that is not terminated. It's a real mess:

<!--1----2----3----4----5-- HEADER BEGINS HERE --6----7----8----9----10-- -->

Of course, it's not quite that simple, due to differences in how each browser chooses to interpret the spec.

Here's an excellent article that explains it:

    http://weblog.200ok.com.au/2008/01/dashing-into-trouble-why-html-comments.html

Long answer: Why we get it wrong

Most of us who grew up with HTML (without delving into the SGML that underlies it)) have come to believe that the string <!-- begins a comment, and the string --> ends a comment.

Actually, <! and > delimit an SGML declaration within your HTML document, such as the DOCTYPE declaration we've all seen at the top of our pages. Within an SGML declaration, comments are delimited by double-dashes. Thus, the HTML comment

<!-- this is a comment -->

which most of us would believe is parsed like this <!-- this is a comment --> is actually parsed like this:
<!-- this is a comment -->. It is an SGML declaration that is empty except for a comment.

Because HTML is a form of SGML, this "comment-within-a-declaration" functions as an HTML comment.

Out of interest, here's a chunk of pure SGML that shows comments functioning as they were intended in SGML: this attribute list definition contains a comment on each line:

<!ATTLIST LINK   %attrs;                              -- %coreattrs, %i18n, %events --   charset     %Charset;      #IMPLIED  -- char encoding of linked resource --   href        %URI;          #IMPLIED  -- URI for linked resource --   hreflang    %LanguageCode; #IMPLIED  -- language code --   type        %ContentType;  #IMPLIED  -- advisory content type --   rel         %LinkTypes;    #IMPLIED  -- forward link types --   rev         %LinkTypes;    #IMPLIED  -- reverse link types --   media       %MediaDesc;    #IMPLIED  -- for rendering on these media -- >
like image 34
Dave Land Avatar answered Oct 15 '22 16:10

Dave Land