data-id
Stores a public identifier for JavaScript or analytics.
Learn data-* as part of the HTML attribute system: what they configure, where they belong and which mistakes to avoid.
Data
Custom data attributes start with data-. They are useful when HTML needs to carry a small piece of state or configuration for JavaScript, analytics, filtering or interaction.
They are not a database and they should not hold secrets. Think of them as simple, readable notes attached to elements.
data-*. Custom data stored in HTML for JavaScript and small interface state. What belongs here
data-idStores a public identifier for JavaScript or analytics.
data-stateStores a small interface state such as open, active or loading.
data-categoryStores grouping information for filtering or tracking.
datasetJavaScript reads data-* values through element.dataset.
Syntax in context
Use lowercase names separated with hyphens. JavaScript converts data-user-id to dataset.userId.
<button type="button" data-action="save" data-item-id="42"> Save lesson </button>
Good versus weak
<button type="button" data-action="save" data-item-id="42"> Save lesson </button>
<button data-password="secret123" data-big-json="{...lots of data...}">
Save
</button>
Rules that matter
Anything in HTML can be viewed by the user. Never store secrets in data attributes.
Large JSON blobs make markup noisy and harder to maintain.
Use data-action or data-state instead of vague names such as data-thing.
Data attributes are strongest when they connect semantic HTML to behavior.
Production thinking
Data attributes exist for small public values that connect HTML to JavaScript, filtering, analytics or interface state without pretending those values are visible content.
Data attributes do not describe meaning to screen readers. Use semantic HTML and ARIA when users need the information.
Data attributes are excellent for behavior hooks because they avoid overloading CSS class names with JavaScript meaning.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.