Data Attributes — HTML

A data-* attribute is an attribute you create yourself. It attaches information to an element without showing it on the page. Write it in the opening tag…

A member card on your page only shows a name: Sam. But the JavaScript code you write later needs to know more: the member's id, the card's colour, whether the card is active. Information like that doesn't need to show on the page. It only needs to be attached to the card's element.

That's what data-* attributes are for: attributes you create yourself to attach extra information to an HTML element. You replace the star with a name you choose, for example data-color or data-user-id. The browser doesn't show this information on the page. Your CSS and JavaScript read it and use it.

Attach information to an element

Here is a member card with three data attributes:

<div class="card" data-color="blue" data-active="true" data-user-id="42">
  Sam
</div>

On the page you only see the word "Sam". The values blue, true and 42 don't show, but they stay attached to the element.

You can put data attributes on any tag, including buttons. Remember that a button's text is not an attribute. It still goes between the opening and closing tags:

<button data-action="delete" data-id="7">Delete</button>

Why must the name start with data-? An attribute you invent without this prefix, such as shade="blue", is not valid (it breaks the rules of HTML). HTML could also add an official attribute with the same name one day, and the two would conflict. The data- prefix is reserved for your own information, so it is safe.

Read them from JavaScript with dataset

This is JavaScript code, and you don't need to know how to write it yet. The first line gets Sam's card element. The next two lines read its data attributes. The comment at the end of each line shows the result:

const card = document.querySelector(".card");
card.dataset.color;  // "blue"
card.dataset.userId; // "42"

Let CSS pick elements by them

Here is one CSS rule. It gives a light blue background to every element whose data-color is blue:

[data-color="blue"] { background: lightblue; }

The square brackets mean "pick the elements that have this attribute with this value". Sam's card matches, so its background is light blue. If you change data-color="blue" in the HTML to "green", your CSS rule for green is used instead. You don't need a new class.

⚠️ Common mix-ups with data attributes

TL;DR: A data-* attribute is an attribute you create yourself. It attaches information to an element without showing it on the page. Write it in the opening tag, with a lowercase name after data- and the value in quotes. One element can have many data attributes. JavaScript reads them through dataset with camelCase names: the hyphen is removed and the next letter becomes a capital (data-user-id becomes dataset.userId). The values are always text (strings). CSS picks elements by them with [data-color="blue"].

⚡ Information that sticks but doesn't show

Write:

<div class="card" data-color="blue" data-active="true" data-user-id="42">
  Sam
</div>

Meaning: An attribute that starts with data- is called a data attribute. The page only shows "Sam". The values blue, true and 42 don't show, but they stay attached to the element, for CSS and JavaScript to read. You choose the name after data- yourself: lowercase, with hyphens between words.

⚡ Why the data- prefix

Meaning: An attribute you invent without the prefix, such as shade="blue", isn't valid (it breaks the rules of HTML). HTML could also add an official attribute with the same name one day, and the two would conflict. The data- prefix is reserved for your own information, so it is safe.

⚡ Read by JavaScript through dataset

Write:

const card = document.querySelector(".card");
card.dataset.color;  // "blue"
card.dataset.userId; // "42"
Guess first: is card.dataset.userId the number 42?

No, it's the text "42". A data attribute's value is always text (a string). Need a number? Convert it first with Number().

Meaning: The first line gets the card element. In dataset, each attribute's name is written without the data- prefix. A name with hyphens changes to camelCase (the hyphen is removed, the next letter becomes a capital): data-user-id becomes dataset.userId.

What you will learn