Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 data-* attribute rules?

Tags:

html

I was wondering if you can have a data-* attribute that is just data-? or just data?

What are the rules here?

like image 606
Tony R Avatar asked Jan 31 '12 03:01

Tony R


People also ask

Can data -* attribute contain HTML tags?

The data-* attribute is a Global Attribute, and can be used on any HTML element.

What are data attributes in HTML5?

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, or extra properties on DOM.

How do you name data attributes?

The data attribute name must be at least one character long and must be prefixed with ' data- '. It should not contain any uppercase letters. Attribute Value. The attribute value can be any string.

Can an element have multiple data attributes?

A data attribute is a custom attribute that stores information. Data attributes always start with “data-” then followed with a descriptive name of the data that is being stored. You can have multiple data attributes on an element and be used on any HTML element.


1 Answers

The data-* attribute rules are specified in the HTML5 specification.

From the spec:

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).

Note that it also restricts the usage to lower case, however another note applies:

All attributes on HTML elements in HTML documents get ASCII-lowercased automatically, so the restriction on ASCII uppercase letters doesn't affect such documents

like image 182
zzzzBov Avatar answered Sep 22 '22 03:09

zzzzBov