Don P
Don P

Reputation: 63567

Can an HTML element have arbitrary attributes?

Can an HTML element be assigned arbitrary attributes?

For example:

<div imMakingUpAnAttribute="whatever"></div>

Upvotes: 36

Views: 10373

Answers (3)

Sushanth --
Sushanth --

Reputation: 55740

Yes, you can have custom attributes:

<div imMakingUpAnAttribute="whatever"></div>

To differentiate between element and custom attributes, a good practice is to prefix it with data-.

<div data-imMakingUpAnAttribute="whatever"></div>

Upvotes: 40

Adam
Adam

Reputation: 44929

Yes, but they must be prefixed with data-.

A custom data attribute is an attribute in no namespace whose name starts with the string "data-", has at least one character after the hyphen, is XML-compatible, and contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z).

W3C HTML5 Spec: http://dev.w3.org/html5/spec/single-page.html?utm_source=dlvr.it&utm_medium=feed#embedding-custom-non-visible-data-with-the-data-*-attributes

Upvotes: 21

mrk
mrk

Reputation: 5117

Yes.

HTML5 custom data attributes post by John Resig http://ejohn.org/blog/html-5-data-attributes/

HTML4 attributes http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2

Upvotes: 2

Related Questions