LEARNING OBJECTIVES ⌵
- Understand the semantic role of the
<data>element in linking human-readable text with machine-readable values. - Contrast
<data>(non-temporal values) with<time>(temporal timestamps) anddata-*(custom scripting attributes). - Implement the mandatory
valueattribute to embed unambiguous catalog SKUs, inventory counts, and scientific IDs. - Integrate
<data>with Microdata and Schema.org for automated e-commerce and scraper ingestion.
📖 The Mental Model & Story (Intuitive Foundation)
Imagine browsing a retail inventory dashboard:
- A shoe size is displayed to the user as: "Size 10.5 (US Men's)"
- The database catalog identifies this item as:
SKU: 849204-US105M - The inventory level is displayed as: "In Stock (Few Left)"
- The database quantity count is:
QTY: 4
Human beings prefer friendly, localized, contextual phrases like "A few left in stock".
Automated inventory scrapers, point-of-sale systems, and search engine bots need exact, immutable, standardized identifiers like 4 and 849204-US105M.
+----------------------------------------------------------------------------------------------------+
| THE DUAL HUMAN/MACHINE ARCHITECTURE OF <data> |
+----------------------------------------------------------------------------------------------------+
| |
| <data value="849204-US105M"> Size 10.5 (US Men's) </data> |
| | | |
| +---------------+ +---------------+ |
| v v |
| [ MACHINE DATA VALUE ] [ HUMAN VISUAL UI ] |
| - Scrapers, APIs, Bots - Human Shopper |
| - Immutable Primary Key - Localized, Formatted Text |
| - "849204-US105M" - "Size 10.5 (US Men's)" |
| |
+----------------------------------------------------------------------------------------------------+
The <data> element bridges this gap perfectly. It allows you to display friendly copy to human eyes while embedding clean machine-readable tokens in the value attribute.
Technical Deep Dive & Specifications
WHATWG HTML Living Standard Specification
According to the official WHATWG specification:
"The
<data>element links a given piece of content with a machine-readable translation. Thevalueattribute must be specified. The value of this attribute is the machine-readable value of the element's contents."
Element Comparison: <data> vs. <time> vs. data-*
Frontend engineers frequently confuse these three distinct concepts:
+---------------------------+
| MACHINE DATA TRIAD |
+---------------------------+
|
+-------------------+-----------------+-------------------+
| | | |
v v v v
[ <data> ] [ <time> ] [ data-* ]
Non-temporal data values Dates, times, durations, Custom JS attributes on
(SKUs, ISBNs, IDs, metrics) time zones (ISO-8601) any DOM element node
e.g., <data value="98.6"> e.g., <time datetime="..."> e.g., <div data-user="12">
| Syntax | Category | Mandatory Attribute | Valid Use Case |
|---|---|---|---|
<data value="..."> |
Semantic HTML Element | value |
Catalog SKUs, ISBNs, stock ticker symbols, numeric metrics |
<time datetime="..."> |
Semantic HTML Element | datetime |
Publication dates, event times, durations, calendar stamps |
<tag data-key="..."> |
HTML Attribute (Dataset) | None (User defined) | Client-side JavaScript state hooks and DOM dataset storage |
The Critical Rule: When to Use <time> vs. <data>
The WHATWG specification strictly mandates:
- If the content represents a date, time, or duration, you MUST use
<time datetime="...">. - If the content represents any other machine-readable value (e.g., numbers, identifiers, coordinates), you MUST use
<data value="...">.
<!-- INVALID: Do not use <data> for dates! -->
<data value="2026-08-21">August 21, 2026</data> ❌
<!-- VALID: Use <time> for dates -->
<time datetime="2026-08-21">August 21, 2026</time> ✅
<!-- VALID: Use <data> for non-temporal data -->
<data value="978-0-13-110362-7">The C Programming Language (2nd Ed)</data> ✅
Microdata & Schema.org Integration
The <data> element is heavily used in Schema.org e-commerce markup to provide clean machine values without invisible <meta> hacks:
<div itemscope itemtype="https://schema.org/Product">
<h2 itemprop="name">Wireless Mechanical Keyboard</h2>
<p>Product Code: <data itemprop="sku" value="KB-9920-RGB">KB-9920-RGB</data></p>
<p>Inventory: <data itemprop="inventoryLevel" value="18">18 units available</data></p>
</div>
💻 Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 47:
<data class="sku-badge" value="CHR-ERG-091">— Links the visible SKU text with the machine-readable SKU token. - Line 48:
<data value="42">42 units (Optimal)</data>— Human reads friendly status "(Optimal)"; scraper extracts exact integer42. - Line 49:
<data value="14.8">14.8 kg</data>— Machine value standardizes metric units to float14.8. - Line 60:
<data value="0">Out of Stock</data>— Human reads "Out of Stock"; database scraper parses integer0. - Line 72:
node.value— JavaScript property directly accesses the element'svalueattribute.
Expected Browser Render Output
- A clean, responsive data table renders the human-readable product names and stock labels.
- The browser console outputs the parsed machine tokens (
"42","14.8","3","0"), demonstrating frictionless data extraction.
🏋️ Hands-On Exercise
🎯 The Challenge: Library Book Catalog Refactoring
You are upgrading a university digital library catalog. The existing markup stores ISBNs and availability in unsemantic spans and mistakenly uses <data> for publication dates.
Instructions:
- Fix the date violation: Replace
<data>with<time datetime="...">on all publication dates. - Refactor book ISBN numbers into semantic
<data value="...">elements. - Refactor book checkout status into
<data value="...">(e.g.value="available"orvalue="checked-out"). - Extract the data values programmatically via the
.valueDOM property.
🏁 Starter Code Sandbox
⚠️ Common Pitfalls
- Using
<data>for Dates and Times: This is a direct violation of the WHATWG specification. Dates, calendar months, and durations MUST use<time datetime="...">. - Omitting the
valueAttribute:<data>without avalueattribute has no machine meaning and fails HTML validation. - Confusing
<data>withdata-*Attributes:<data>is an inline HTML element node (<data value="10">Ten</data>).data-*is a custom HTML attribute placed on any tag (<div data-product-id="10">).
💡 Pro Tips
- Client-Side Framework Data Binding: When rendering tables in React, Svelte, or Vue, binding
<data value={item.id}>{item.formattedName}</data>eliminates the need to maintain parallel lookup arrays for clipboard copy actions or analytics click handlers. - Web Scraper & ETL Efficiency: By wrapping catalog identifiers in
<data value="...">, data engineering web crawlers can extract structured data viadocument.querySelectorAll('data').map(el => el.value)in one line of JavaScript without fragile regex parsing.
📌 Key Takeaways
<data>links human-readable text with a machine-readable translation via thevalueattribute.- The
valueattribute is mandatory on<data>. - Never use
<data>for dates or times; always use<time datetime="...">for temporal content. <data>is a semantic element;data-*are custom dataset attributes on any element.<data>pairs seamlessly with Schema.org Microdata for search engine product indexing.- --