data attribute

A custom HTML attribute whose name starts with data-, such as data-theme="dark", used to store extra information on an element.

In more detail

CSS can select on it with [data-theme="dark"] and JavaScript can read it through element.dataset. Setting data-theme on the root or on a section is a simple, fast way to switch themes for everything inside it.

Example

In the browser: An attribute like data-id="101" appears in JavaScript as element.dataset.id. Data attributes are handy for event delegation, testing hooks such as data-testid and theme switches such as data-theme="dark", which CSS can also select.

Related terms

Taught in

References