Live Preview
Readers see the label; scripts can read value="25":
Temperature:25°C

By the end of this tutorial, you’ll link human-readable labels to machine-readable values with the <data> element.
Pair visible text with the required value attribute.
Store numbers, codes, and strings scripts can read.
Distinguish <data> from data-* custom attrs.
Read and update values with the .value property.
Know when to use time for dates instead.
Style visible labels with classes on the element.
<data> Tag?The data element (<data>) links human-readable text to a machine-readable value using the value attribute. Readers see the element’s text; scripts read .value through the HTMLDataElement interface.
<data> is an HTML element with visible content. data-* attributes are custom metadata on any element, accessed via element.dataset.
Common uses include temperatures, prices, product ratings, inventory counts, or any value where the displayed text differs from the machine-readable form scripts need.
Wrap visible text in <data> and set the machine-readable form in value:
<data value="42">forty-two</data>value attribute is required and holds the machine-readable string.<data> is an inline phrasing element—it nests inside paragraphs and lists.<time datetime> for dates and times instead of generic data.| Feature | Syntax / Rule | Notes |
|---|---|---|
| Required attr | value | Machine-readable string |
| Display | Text content | Human-readable label on page |
| Element type | Inline | Phrasing content |
| JS access | element.value | HTMLDataElement property |
| Not the same | data-* | Custom attrs on any element |
| vs time | General purpose | time is for dates/times only |
<data> Element vs data-* AttributesThese share a name but serve different purposes in HTML:
| Feature | <data> element | data-* attributes |
|---|---|---|
| What it is | HTML tag with visible text | Custom metadata on any element |
| Example | <data value="4.5">4.5 stars</data> | <div data-price="19.99"> |
| JS access | element.value | element.dataset.price |
| Visible? | Yes — text content renders | No — attribute only |
The value attribute is required. Global attributes also apply:
value RequiredMachine-readable form of the content—what scripts read via .value.
value="42"class GlobalCSS hook for styling the visible label text.
class="price-tag"id GlobalUnique identifier for JavaScript selection and updates.
id="stock-price"style GlobalInline presentation on the visible text (prefer CSS classes).
style="color:#0f766e"title GlobalTooltip on hover for extra context about the value.
title="Celsius"data-* OptionalExtra custom metadata alongside value if needed.
data-unit="celsius"Example: <data value="2024" class="highlight-year" id="year-data">Important Year</data>
Basic values, measurements, JavaScript updates, and data element vs data-* attributes with copy-ready code and live previews.
Readers see the label; scripts can read value="25":
Temperature:25°C
Pair a numeric machine value with human-friendly text.
<p>
The answer is
<data value="42">forty-two</data>.
</p>Use <data> for temperatures, prices, product ratings, inventory counts, or any value where the displayed text differs from the machine-readable form scripts need.
Store the numeric value separately from the formatted display.
<p>
The average temperature today is
<data value="25">25°C</data>.
</p>Read and update the machine-readable value with JavaScript while keeping the display in sync.
<data id="stock-price" value="150">$150</data>
<script>
var stock = document.getElementById("stock-price");
var price = Number(stock.value) + 10;
stock.value = String(price);
stock.textContent = "$" + price;
</script>Do not confuse the <data> tag with custom data-* attributes on other elements.
<!-- data ELEMENT -->
<data value="4.5">4.5 stars</data>
<!-- data-* ATTRIBUTE on a div -->
<div data-sku="BK-1024" data-price="19.99">Notebook</div>The visible label is styled like any inline text. Use classes to highlight prices, measurements, or ratings:
font-weight Emphasize valuescolor Brand accentbackground Chip-style labelsfont-variant-numeric Tabular numbers/* Price highlight on data element */
data {
font-weight: 600;
}
.price-tag {
color: #15803d;
background: #ecfdf5;
padding: 0.1rem 0.35rem;
border-radius: 4px;
}
.measurement {
color: #0f766e;
font-variant-numeric: tabular-nums;
}Live styled data values
Today:25°C— Sale:$19.99
The value attribute is primarily for scripts, not screen readers:
time may be more semantic.Human text inside the tag; machine form in value.
Users see the label, not the raw value attribute.
Scripts access the machine-readable form for logic and updates.
Friendly labels for humans, structured values for code.
The <data> element is fully supported in all modern browsers. Internet Explorer does not support it—use a span with data-* attributes as a fallback if IE support is required.
Chrome, Firefox, Safari, Edge, and Opera all support the data element. Legacy IE requires a fallback pattern.
Bottom line: Safe for all modern production environments. For legacy IE, fall back to <span data-value="..."> with equivalent JavaScript.
The <data> tag connects human-readable labels to machine-readable values through the value attribute. Use it for measurements, prices, and ratings—and remember it is not the same as data-* custom attributes on other elements.
value attribute.value in JavaScript for logictime for date/time contentdata element with data-* attrsdata with datalistvalue<data>Bookmark these before you ship — they’ll keep your machine-readable markup clean and correct.
Holds the machine-readable string scripts read via .value.
Text content is what users see on the rendered page.
EssentialElement tag ≠ custom data-* attributes on any element.
Read and update machine values through HTMLDataElement.
ScriptingInline phrasing element supported in all modern browsers.
SyntaxPrefer <time datetime> for date/time content.
value that scripts can read via the element’s .value property.<data> is an HTML element. data-* attributes are custom attributes on any element, accessed via dataset.value attribute provides the machine-readable form and is essential to the element’s purpose.element.value to get or set the machine-readable string.time is for dates and times with datetime. data is general-purpose for any machine-readable value.Try the value attribute and JavaScript .value in the interactive editor.
6 people found this page helpful