HTML data attributes are custom attributes whose names start with data-. You can put any string on an element, such as data-user-id="42", and read it from JavaScript through the dataset property or from CSS through attribute selectors. They let you attach small pieces of private information to markup without inventing invalid attributes.
The browser ignores data-* attributes for rendering and behaviour. They exist for your own scripts and styles, and they are valid in every HTML element. This guide covers the naming rules, how to read and write them, how to use them in CSS, and when not to use them.
A custom data attribute has two parts: the prefix data- and a name you choose. The HTML living standard sets a few rules for the name:
data-.xml.The value is always a string. Quote it, even when it looks like a number.
Each <li> carries its product id, price, stock flag and category. Nothing changes on screen, but the data is now available to scripts and selectors.
Every element has a dataset property. It is a DOMStringMap that exposes each data-* attribute as a property, with the data- prefix removed and the name converted from kebab-case to camelCase.
| Attribute in HTML | Property on dataset |
|---|---|
data-id | dataset.id |
data-in-stock | dataset.inStock |
data-user-name | dataset.userName |
Values are strings, so convert them yourself before doing arithmetic or comparisons:
A common mistake is to write if (item.dataset.inStock). The string "false" is truthy, so that test passes for every element that has the attribute. Always compare with === "true".
You can also use getAttribute("data-id"). It returns the same string, or null when the attribute is missing, and it uses the original kebab-case name.
Assigning to a dataset property creates or updates the matching attribute. Deleting the property removes it.
Notice that the camelCase property lastViewed became data-last-viewed in the markup. The browser converts in both directions. If you assign a non-string, it is converted to a string, so an object becomes "[object Object]". Store JSON text yourself if you need structured data, and keep it small.
CSS can select elements by attribute, which makes data-* attributes a clean way to expose state to your stylesheet.
The attr() function can also print an attribute value through generated content:
Today attr() is reliable only inside the content property. The newer typed form of attr() for other properties is not supported everywhere, so do not depend on it in production without checking browser support.
Event delegation combined with data-* attributes avoids adding a listener to every row.
One listener handles every button, and the id travels with the element that was clicked.
Set one attribute from JavaScript and let CSS do the visual work:
Teams often add data-testid or data-analytics attributes so that automated tests and trackers find elements without depending on class names that designers may rename.
Data attributes are not a place for everything. Keep these limits in mind:
data-* attribute. Search engines and assistive technologies generally do not treat it as page content. Use real elements and text.disabled, hidden, aria-expanded and similar built-ins when they describe the state.data- prefix such as price="19.99". They are invalid and may clash with future standards.Yes. The HTML living standard reserves the data- prefix for authors, so these attributes are valid on any element and will never be given a built-in meaning.
Attributes are always text. Convert with Number() or parseInt(), and compare booleans with === "true" rather than testing truthiness.
No. They are not treated as visible content. Put text that matters in elements, and use data-* only for behaviour and styling hooks.
Convert each hyphenated part to camelCase. data-user-first-name becomes dataset.userFirstName.
Custom data attributes use the data- prefix, hold strings, and are valid on any element. Read and write them with element.dataset, where kebab-case names become camelCase, and select on them in CSS with attribute selectors. Use them for ids, state flags and test hooks, convert types yourself, and never rely on them for secrets or for content that visitors and search engines need to see.