HTML data Tag
Learn how to use the HTML <data> tag to associate a machine-readable translation or value with human-readable content.
Introduction to the HTML data Tag
The HTML<data>
tag is used to link a given piece of content with a machine-readable translation or value. If the content is time- or date-related, the <time>
element should be used instead.
This element provides a way to make content understandable to both humans (through its visible content) and machines (through its value
attribute).
Basic Syntax
html
<ul>
<li><data value="21053">Pentium IV 3.2GHz</data></li>
<li><data value="21054">Atom N270 1.6GHz</data></li>
<li><data value="21055">Core i7 3.4GHz Extreme</data></li>
</ul>
<p>The product <data value="UPC:012345678905">SuperWidget Model X</data> is currently on sale.</p>
- The content inside the
- Use
While
<data>
tags is what the user sees (e.g., "Pentium IV 3.2GHz").
- The value
attribute contains the machine-readable equivalent (e.g., "21053" which could be a product ID).
Key Attributes
-value
: This is the primary attribute of the <data>
tag. It specifies the machine-readable translation of the content of the element.
Purpose and Use Cases
The<data>
tag is useful in scenarios where you want to present information to users in a human-friendly way, but also provide a standardized, machine-readable version for scripts, search engines, or other data processing applications.
Examples include:
- Product Names and IDs: Displaying a product name while linking it to a specific product ID or SKU.
`html
We have the Awesome Gadget Pro in stock.
`
- User-Friendly Labels with Standardized Values: Representing options that have a user-friendly display but need a specific value for processing.
`html
Color: Bright Red
`
- Linking to Internal Identifiers: Any content that has a more structured or coded representation in a backend system.
<data>
vs. <time>
- Use <data>
for generic data that has a machine-readable equivalent.
- Use <time>
specifically for dates, times, or durations. The <time>
element has a datetime
attribute that is designed for machine-readable date/time formats.
`html
The event is on .
`
<data>
vs. Microdata/RDFa/JSON-LD
While <data>
provides a simple way to attach a machine-readable value, more complex scenarios for structured data on the web often use richer systems like:
- Microdata: Uses attributes like itemscope
, itemtype
, and itemprop
to create structured data within HTML.
- RDFa (Resource Description Framework in Attributes): Another way to embed structured data.
- JSON-LD (JavaScript Object Notation for Linked Data): Often embedded in a <script>
tag, providing structured data in JSON format.
These technologies (especially JSON-LD as favored by Google) are more comprehensive for SEO and detailed data interchange. The <data>
tag is simpler and can be useful for cases where you just need to associate a single piece of machine-readable data with some content, perhaps for client-side scripting or very specific internal uses.
Styling
The<data>
tag itself does not apply any special styling by default. It behaves like a <span>
element (inline content). Any styling would be applied via CSS based on classes, IDs, or the element selector.
Conclusion
The HTML<data>
tag offers a straightforward method to embed machine-readable values alongside human-readable content. While not as extensive as full structured data vocabularies, it serves a valuable purpose for specific use cases where a simple value association is needed. Remember to use <time>
for date and time related information.