Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an HTML element have arbitrary attributes?

Tags:

html

Can an HTML element be assigned arbitrary attributes?

For example:

<div imMakingUpAnAttribute="whatever"></div> 
like image 885
Don P Avatar asked Oct 24 '12 00:10

Don P


People also ask

Can HTML elements have custom attributes?

Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our own information to HTML tags. These are not specific and can be used with all the HTML elements.

Can all HTML elements have attributes?

Global attributes are attributes common to all HTML elements; they can be used on all elements, though they may have no effect on some elements. Global attributes may be specified on all HTML elements, even those not specified in the standard.

Can a HTML element have multiple data attributes?

You can have multiple data attributes on an element and be used on any HTML element.


2 Answers

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> 
like image 176
Sushanth -- Avatar answered Sep 18 '22 12:09

Sushanth --


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

like image 43
Adam Avatar answered Sep 19 '22 12:09

Adam