The data-* attributes in HTML provide a way to store custom data private to the page or application. They let you attach extra information to any element without affecting how the page looks, and they are especially useful for JavaScript behavior and CSS styling hooks.
What You’ll Learn
01
data- prefix
Naming pattern.
02
Any element
Global usage.
03
dataset
JS access.
04
String values
DOM storage.
05
CSS hooks
[data-state].
06
vs data
Not object URL.
Fundamentals
Purpose of data-*
The primary purpose of data-* attributes is to store custom metadata on HTML elements. This data is not shown to users by default and does not change standard rendering. It gives developers a standard, valid way to embed configuration, IDs, state flags, or other values that scripts need.
💡
Not the object data attribute
The standalone data attribute on <object> points to an external file URL. data-* custom attributes work on any element and store arbitrary string metadata.
Foundation
📝 Syntax
Prefix the name with data- followed by a descriptive identifier:
data-*.html
<divdata-example="custom-data">This element has custom data</div><buttondata-action="save"data-id="42">Save</button>
Syntax Rules
Name format: data- + your name (use lowercase and hyphens, e.g. data-user-id).
Do not use uppercase letters in the attribute name; browsers normalize them to lowercase.
Include at least one hyphen after data- (e.g. data-id) to avoid future conflicts with standard attributes.
Values are always stored as strings in the DOM.
How It Works
In the first example, data-example stores the string custom-data on the <div> element for scripts to read later.
The data-* attribute can hold different kinds of information, but the browser always treats the value as a string:
Text — data-role="admin"
Numbers as strings — data-count="5" (parse with Number() in JS)
Booleans as strings — data-active="true"
JSON as a string — data-config='{"theme":"dark"}' then JSON.parse()
data-values.html
<divdata-price="19.99"data-in-stock="true"></div><script>
var el = document.querySelector("[data-price]");
var price = parseFloat(el.dataset.price);
var inStock = el.dataset.inStock === "true";
</script>
Cheat Sheet
⚡ Quick Reference
HTML attribute
JavaScript (dataset)
Notes
data-user-id="123"
el.dataset.userId
Hyphens become camelCase
data-username="jane"
el.dataset.username = "new"
Set updates the attribute
getAttribute
el.getAttribute("data-user-id")
Alternative to dataset
CSS selector
[data-state="open"]
Style by data value
Remove
delete el.dataset.userId
Removes the attribute
Value type
Always string
Parse numbers/JSON in JS
Scope
Applicable Elements
Element
Supported?
Notes
Any HTML element
Yes
Global custom data attributes
<div>, <button>, <li>
Yes
Common for UI state and config
<object data="...">
Different feature
data is the resource URL, not data-*
Reserved names
No
Do not invent attributes that duplicate standard ones
Compare
data-* vs the object data attribute
Feature
data-*
data on object
Elements
Any element
<object> only
Purpose
Custom metadata
Embedded file URL
JavaScript
element.dataset
object.data
Example
data-price="10"
data="file.pdf"
Hands-On
Examples Gallery
User profile data, and reading or updating values with dataset.
Example
Let’s look at a simple example of how to use data-* attributes in HTML:
data-*.html
<divid="user"data-user-id="123"data-username="john_doe"data-email="john@example.com"><!-- Content for the user profile --></div>
In this example, data-* attributes store user-related information such as user ID, username, and email. JavaScript can read these values without duplicating them in visible text.
Dynamic Values with JavaScript
You can dynamically set or retrieve data-* values using the dataset property:
dataset.html
<script>// Get the value of a data attribute
var userId = document.getElementById("user").dataset.userId;
// Set a new value
document.getElementById("user").dataset.username = "new_username";
</script>
The dataset property maps data-user-id to dataset.userId. Assigning to dataset.username updates the underlying data-username attribute in the DOM.
A11y
♿ Accessibility & Security
Not a substitute for visible text — Important information for users should still appear in content or ARIA, not only in data-* attributes.
Screen readers ignore data-* — They do not announce custom data attributes by default.
No secrets — Never store passwords, tokens, or private data in data-*; users can see them in DevTools.
Prefer semantic HTML — Use native elements and ARIA when conveying meaning to assistive tech.
Validate parsed JSON — If you store JSON in data-*, wrap JSON.parse in try/catch.
🧠 How data-* Works
1
Author adds data-*
Custom name and string value on any element.
Markup
2
Browser stores in DOM
Attributes appear in HTML and DevTools.
DOM
3
JS or CSS consumes data
dataset, getAttribute, or [data-*] selectors.
Use
=
📦
Flexible metadata layer
Behavior and styling without invalid custom attributes.
Compatibility
Browser Support
Custom data-* attributes and the dataset API are supported in all modern browsers.
✓ HTML5 · Fully supported
Universal custom data attributes
All major browsers support data-* markup and dataset.
99%Browser support
Google ChromeFully supported
Full support
Mozilla FirefoxFully supported
Full support
Apple SafariFully supported
Full support
Microsoft EdgeFully supported
Full support
data-* attributes & dataset99% supported
Bottom line: Use data-* and dataset confidently in all modern projects.
Pro Tips
💡 Best Practices
✅ Do
Use descriptive names like data-user-id or data-action
Use dataset for clean JavaScript access
Parse numbers and JSON explicitly in scripts
Use data-* for UI state hooks and component config
Keep names lowercase with hyphens
❌ Don’t
Store passwords, API keys, or sensitive data
Confuse data-* with object data URL attribute
Use uppercase letters in attribute names
Rely on data-* alone for critical user-facing info
Create non-standard attributes without the data- prefix
Choose meaningful and descriptive names for your custom data-* attributes to maintain clarity in your code.
Avoid storing sensitive or critical information in data-* attributes, as they are easily accessible through browser developer tools.
Use the dataset property in JavaScript to interact with data-* attributes efficiently.
Wrap Up
Conclusion
The data-* attribute in HTML is a powerful tool for storing custom data within elements. It provides a flexible, standards-based way to pass configuration and state to JavaScript and CSS without breaking HTML validity.
Use it for non-sensitive metadata, pair it with dataset in scripts, and remember that values are always strings until you parse them. For embedding file URLs in <object>, use the separate data attribute instead.