Cary Bondoc
Cary Bondoc

Reputation: 2988

Reason why I can't insert a comment inside the HTML tag?

Are there any reason why I can't insert a comment inside the HTML tag?

Sample: In HTML (Not Possible)

 <img <!-- sample comment--> src="" alt="Sample Picture" class="img-circle center-block" />

While in JavaScript it can be easily done.

Sample: In JS (Possible)

verticalCentered: /* Sample Comment*/ true

Please explain it in a detailed way.

Upvotes: 4

Views: 4490

Answers (4)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201648

HTML was originally defined formally as an application of SGML, and the comment syntax was taken from SGML. SGML uses two hyphens -- as comment delimiters, but it permits comments in certain contexts only. Of these contexts, only a comment declaration is relevant in HTML. (Well, in theory they are also allowed in entity declarations like <!ENTITY foo "foo" -- Comment -- >, but entity declarations were never implemented in any browser before XHTML, and in XHTML, following XML syntax, entity declarations cannot contain comments.)

Thus, comments are only allowed in comment declarations that contain nothing but comments, e.g.

<!-- Comment one -- -- Comment two -->

Browsers actually implemented this partly in a simplified (wrong) way, so the practical recommendation is to have comments only in comment declarations that contain a single comment:

<!-- Comment -->

Comment declarations are at the same structural level as tags and therefore cannot appear within tags. (Comments cannot contain tags either: anything that would otherwise constitute a tag, like <p>, is taken just as character data when inside a comment.)

This has not been changed in later HTML versions. They have simplified the syntax in matters like this, not extended.

Upvotes: 5

user663031
user663031

Reputation:

If you are using a templating language, you may be able to use the templating language's comment mechanism to do what you want: <img {{! this is a comment }} src=.... This also has the advantage that the comment will be removed at compile time, and thus won't litter your HTML with comments.

Upvotes: 4

Jobayer
Jobayer

Reputation: 1231

Because comment is also a tag & you can not write html tag inside anther html tag.

Upvotes: -2

Chris Weltes
Chris Weltes

Reputation: 3

You can insert a comment into html tag. If im using a netbeans then you can disable HTML error checking when the error appears

Upvotes: -6

Related Questions