Full answer
A data-* attribute lets you store your own small piece of information on an HTML element. The name starts with data-, followed by a name in lowercase, for example data-course-id="42" or data-status="open". They are valid HTML, so they do not break validators, and they do not appear on the page.
In JavaScript, the dataset property gives access. el.dataset.courseId reads data-course-id. The dashes become camel case. Values are always strings, so convert numbers yourself with Number(). Writing el.dataset.status = "closed" changes the attribute.
In CSS you can use the value in two ways:
- Attribute selectors:
[data-status="open"] { ... }styles elements by value. This is useful for state that JavaScript sets, and it avoids long class names. attr()insidecontent:a::after { content: attr(data-hint); }shows the text from the attribute in a pseudo-element.
Keep these rules in mind. Do not put secrets or important content in data attributes, because anyone can read them in the page source. Do not use them for text that screen readers must read: text inside content from attr() may not be read the same way in every case, and search engines do not treat it as page content. Use real text for real content.
The script reads 42 as a number and changes the status to closed. The border turns red because the attribute selector matches the new value. The text "(beginner)" is added after the title from the data-level attribute.
In the example the course title is real text in the element, and only the extra label comes from the attribute.
<style>
.course { padding: 8px; border: 1px solid #999; }
.course[data-status="open"] { border-color: green; }
.course[data-status="closed"] { border-color: #c00; color: #666; }
.course::after { content: " (" attr(data-level) ")"; font-size: 0.8em; }
</style>
<div class="course" id="c" data-course-id="42" data-status="open" data-level="beginner">HTML basics</div>
<script>
const el = document.getElementById("c");
window.idValue = Number(el.dataset.courseId);
el.dataset.status = "closed";
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Names start with data- and use lowercase letters and dashes.
- dataset converts data-course-id to courseId.
- Values are always strings.
- CSS can use [data-x="y"] selectors and attr().
- Do not store secrets or important text in them.
Common mistakes
- Expecting dataset values to be numbers or booleans. They are always strings.
- Storing secrets or large data in data attributes.
- Putting meaningful text only in attr() content, which is not reliable for assistive technology or search.
Follow-ups
- How do you remove a data attribute with JavaScript?
- Why should important text not come only from attr()?
Tips
- Show the camel case rule: data-course-id becomes dataset.courseId.
- Say that values are strings, with a one-line example.