HTML data-* attributes
- type
- attribute (any name starting with data-)
- applies to
- any element
- lives inside
- the opening tag
- common values
- data-id="42", data-state="open", data-color="red"
Where it goes
On the tag the information is about: data-id on a list item, data-state on a toggle, data-track on a button.
Example
<li data-id="42" data-state="done">Buy milk</li>Open this example in the editor →
Common mistakes
- Using it for text the visitor should see. It is for facts about a tag that code reads, not a place to hide content.
- Skipping the data- prefix, like <li state="open">. Made-up attributes mostly work, but they are invalid, and a future HTML attribute with that name would change the meaning.
- Stuffing a whole blob of data into one. A few small, clearly named attributes are easier to read and to target from CSS.