HTML global attributeGlobal attributes

data-*

Custom attributes for storing data for scripts and styles.

Applies to
All HTML elements
On this page

Example

#
HTML
<style>
  [data-level="expert"] { color: #f472b6; }
  li::after { content: " (" attr(data-points) " pts)"; color: #737373; }
</style>

<ul>
  <li data-level="beginner" data-points="120">Anna</li>
  <li data-level="expert" data-points="980">Jake</li>
</ul>
Preview

Definition and usage

#

Attributes starting with data- let you attach your own information to an element, for example a product identifier, a button state, or a category. The browser does not interpret them; they are for your code.

In JavaScript you read them through the dataset object. The name after data- is converted to camelCase: data-product-id becomes element.dataset.productId. Values are always strings.

You can also style based on them in CSS, for example [data-state="active"], or display the value with content: attr(data-label).

Syntax

#
Syntax
<element data-name="value">…</element>

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Do not store anything secret in data-*. Anyone can inspect the page source.
  • Write names in lowercase with hyphens, for example data-user-id.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.