HTML data-* Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The data-* global attributes let you store custom metadata on any HTML element. Any attribute whose name starts with data- is a custom data attribute — for example data-user-id or data-columns.

Use them when scripts or CSS need extra information that should not change how the element looks by default. Prefer them over inventing non-standard attributes. They are not the same as the data attribute on <object> (a resource URL).

Baseline · Widely available.

Not deprecated and not experimental. Stable HTML5 global attributes with broad browser support, including element.dataset.

Not for visible content or secrets.

Assistive tech and search engines may ignore data values. Keep user-facing text in real content. Never store passwords or tokens in data-*.

HTML Syntax

syntax.html
<element data- name="value">

Use lowercase names with hyphens (data-user-id). Avoid names that start with xml, contain :, or use capitals. For dataset, prefer names that map cleanly to camelCase (avoid data-test-1).

Attribute Values

Any string. The browser always stores the value as text:

Value formDescriptionTry it
data-role="admin"Plain text metadataTry it
data-count="5"Number stored as a string — parse in JSTry it
data-user-id="123"Hyphenated name → dataset.userIdTry it
data-variant="note"Useful hook for CSS attribute selectorsTry it
JSON stringe.g. data-config='{"theme":"dark"}' then JSON.parse()Try it

Live Preview

Change a data-* value on the demo element, then read it with dataset.

Applies data-role on the box below and shows dataset.role.

Element
Profile card (metadata stays in attributes)

dataset.role = "guest" · dataset.userId = "42"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Store Custom Metadata

Attach private fields to an article without changing its visible meaning.

HTML
<article
  id="electric-cars"
  data-columns="3"
  data-index-number="12314"
  data-parent="cars">
  Electric cars overview
</article>
Try It Yourself

How It Works

  1. Prefix with data-. Each custom field starts with data-.
  2. Use clear names. data-index-number describes the value.
  3. Values stay invisible until a script or CSS uses them.
  4. Any element works. This pattern is valid on articles, buttons, lists, and more.

Example 2 — Read and Update with dataset

Hyphenated HTML names become camelCase properties on element.dataset.

HTML
<p id="user" data-user-id="12345" data-username="john_doe">User Info</p>
<button type="button" id="updateBtn">Update username</button>
<p id="output">Click the button.</p>

<script>
  var user = document.getElementById("user");
  var output = document.getElementById("output");

  document.getElementById("updateBtn").addEventListener("click", function () {
    var id = user.dataset.userId;
    user.dataset.username = "new_username";
    output.textContent =
      "ID: " + id + " · username: " + user.dataset.username;
  });
</script>
Try It Yourself

How It Works

  1. Drop the data- prefix. data-user-id → dataset.userId.
  2. Read like a property. user.dataset.userId returns "12345" (a string).
  3. Assign to update. Setting dataset.username updates the HTML attribute.
  4. Delete with care. delete user.dataset.username removes that attribute.

Example 3 — Style with Attribute Selectors

Because data-* are normal attributes, CSS can target them (see attribute selectors).

HTML
<div class="callout" data-variant="note">A helpful note.</div>
<div class="callout" data-variant="warning">A warning.</div>

<style>
  .callout[data-variant="note"] { border-left: 4px solid #2563eb; }
  .callout[data-variant="warning"] { border-left: 4px solid #dc2626; }
</style>
Try It Yourself

How It Works

  1. One base class. callout holds shared layout.
  2. Variant lives in data-. data-variant="note" or "warning".
  3. CSS matches the value with [data-variant="…"].
  4. JS can switch variants with el.dataset.variant = "warning" without juggling many class names.

Edge Cases & Pitfalls

Most mistakes come from treating data-* like secure storage or visible content.

a11y

Do not hide important text in data-*

Screen readers and crawlers may skip attribute values. Put real content in the element text (or use proper ARIA when needed).

security

Visible in source — no secrets

Anyone can view data-* in DevTools. Keep tokens and passwords off the page.

naming

dataset needs clean names

Avoid data-test-1 or data--x if you rely on dataset. Prefer data-test-one. Use getAttribute when names are awkward.

types

Everything is a string

dataset.count === 5 is false when the value is "5". Convert with Number(), parseInt(), or compare to "true" for flags.

compare

Not the object data attribute

data on <object> is a URL. data-* is custom metadata on any element.

Browser Support

data-* is an HTML global attribute family in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

data-*

Custom data attributes and HTMLElement.dataset are widely supported in modern browsers. Safe to use for markup metadata and progressive enhancement.

97% Global support (approx.)
Google Chrome 7+ · dataset 8+
Full support
Mozilla Firefox 6+
Full support
Apple Safari 5.1+
Full support
Microsoft Edge 12+
Full support
Opera 11.1+
Full support
Samsung Internet Modern
Full support
Internet Explorer 11 (dataset)
Supported
data-* Widely supported

Bottom line: Use data-* freely for client-side metadata. Prefer getAttribute if you must support very old browsers without dataset.

Key Takeaways

  • Role: store private string metadata on any element with a data- name.
  • JavaScript: read and write via element.dataset (kebab → camelCase).
  • CSS: select with [data-name="value"] when you need style hooks.
  • Safety: not for secrets; not a substitute for visible, accessible content.

One line: data-* is the standard way to attach custom string data to HTML for scripts and CSS.

Frequently Asked Questions

They are custom attributes whose names start with data-. You use them to store extra string metadata on any HTML element for scripts or CSS, without inventing non-standard attributes.
Use element.dataset. The part after data- is converted from kebab-case to camelCase — for example data-user-id becomes dataset.userId. You can also use getAttribute("data-user-id").
No. On <object>, data holds a resource URL. data-* custom attributes work on any element and store arbitrary metadata.
In the DOM they are always strings. Store numbers or JSON as text, then parse them in JavaScript (Number(), JSON.parse(), and so on).
No. They appear in the HTML source and DevTools. Never store passwords, tokens, or other secrets there.
data-* and HTMLElement.dataset are Baseline widely available. Modern Chrome, Firefox, Safari, and Edge all support them; Internet Explorer supports dataset from IE 11.

Next: datetime

Add machine-readable dates and times on <time>, <ins>, and <del>.

datetime tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful