Greg Gum
Greg Gum

Reputation: 37909

Is there a reason not to use a custom "comment" attribute?

Is there a reason not to do the following?

<div comment="start display area">
     ...
</div>

I like to add notes, and this seems like an ideal way to do it.

I know I could do this as well:

<!--Start display area-->
<div">
     ...
</div>

The first syntax just seems cleaner to me. The issue I run into with <!-- Is that sometimes, I do want to comment out an area of HTML for debugging purposes. That doesn't work when there are comment strings in there.

I found this much earlier discussion on it: https://www.w3.org/XML/2000/04schema-hacking/comment-test.html

Upvotes: 1

Views: 69

Answers (2)

Bassie
Bassie

Reputation: 10390

To add custom attributes you should use data-* :

<div data-comment="start display area">
     ...
</div>

Source: W3C

Another option would be to use DTD attributes:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
 [
  <!ATTLIST tag comment CDATA #IMPLIED>
 ]>

As to the downside, the only thing I can think of is that for others looking at your code it may not be as readable, as most are probably used to standard format comments.

Upvotes: 0

devlin carnate
devlin carnate

Reputation: 8590

You should use data attributes. Data attributes are designed exactly for what you're intending to do:

HTML5 is designed with extensibility in mind for data that should be associated with a particular element but need not have any defined meaning. data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, extra properties on DOM, or setUserData.

Your div would look like:

<div data-comment="start display area">

Upvotes: 5

Related Questions