HTML data Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 3 Examples
Embedded Content

Introduction

In HTML, the data attribute is used with the <object> element to specify the URL of an external resource to embed — such as a PDF, image, or other file. It tells the browser where to fetch the content displayed inside the object.

What You’ll Learn

01

Resource URL

Points to file.

02

object Tag

Primary element.

03

type MIME

Pair with data.

04

Fallback

Inner content.

05

object.data

JS property.

06

vs data-*

Not the same.

Purpose of data

The main purpose of the data attribute is to specify the address of the resource used by the <object> element. That resource can be a PDF document, SVG file, image, or other embeddable content. The attribute lets you integrate external files into your page through a single tag.

💡
Not for custom element metadata

Attributes like data-id or data-price on a <div> are data-* custom attributes — a different feature covered in the next tutorial. The standalone data attribute is only for <object> resource URLs.

📝 Syntax

Set data to a valid URL on an <object> element:

data.html
<object data="document.pdf" type="application/pdf" width="480" height="320">

  <p>Cannot display PDF. <a href="document.pdf">Download</a></p>

</object>

Syntax Rules

  • Value is a URL (relative or absolute) pointing to the resource file.
  • Use with type to declare the MIME type (for example application/pdf).
  • Include fallback HTML inside <object> for browsers that cannot render the resource.
  • JavaScript property: objectElement.data returns or sets the URL string.

💎 Values

The data attribute takes a URL string as its value:

  • Relative URLdata="files/report.pdf" resolves from the current page path.
  • Absolute URLdata="https://cdn.example.com/chart.svg" loads from another host.
  • Same-origin pathdata="/images/diagram.svg" starts from the site root.
data-values.html
<!-- PDF document -->

<object data="report.pdf" type="application/pdf"></object>



<!-- PNG image -->

<object data="/images/logo.png" type="image/png" width="128" height="128"></object>



<!-- Remote SVG -->

<object data="https://example.com/icon.svg" type="image/svg+xml"></object>

⚡ Quick Reference

ItemMarkup / CodeNotes
Basic usage<object data="file.pdf">URL of embedded resource
With MIME typetype="application/pdf"Helps browser choose handler
Dimensionswidth / heightSize of embed area
JavaScriptobj.data = "new.pdf"Swap resource at runtime
FallbackHTML inside objectShown if embed fails
Not thisdata-user-id="5"That is a data-* attribute

Applicable Elements

ElementSupported?Notes
<object>YesPrimary and modern use case
<applet>ObsoleteDeprecated; do not use in new pages
<img>, <div>NoUse src or data-* instead
<video>, <audio>NoUse native media tags with src

data vs data-* attributes

Featuredata attributedata-* custom attributes
Element<object> onlyAny HTML element
PurposeResource URL to embedStore custom metadata
Exampledata="doc.pdf"data-price="19.99"
JavaScriptobject.dataelement.dataset.price

Examples Gallery

PDF embed, image with fallback, and dynamic JavaScript update.

Implementation Example

The data attribute generally takes a URL pointing to the external resource:

data.html
<object width="400" height="300" data="document.pdf" type="application/pdf">

  <p>Your browser does not support this object.</p>

</object>
Try It Yourself

How It Works

In this example, data specifies the URL of the PDF file that the <object> element embeds. The type attribute tells the browser what kind of file to expect.

Example with Fallback

Let’s explore a more comprehensive example using an image with fallback content:

data-image.html
<object data="photo.png" type="image/png" width="300" height="200">

  <!-- Fallback if object cannot render the data URL -->

  <img src="fallback-image.jpg" alt="Fallback Image">

</object>
Try It Yourself

How It Works

The data attribute points to the primary image. Fallback content inside <object> ensures something visible appears when the browser cannot render the embedded resource. Match type to the actual file format (PNG, not JPEG, for .png files).

Dynamic Values with JavaScript

You can dynamically set the data attribute using JavaScript to change embedded content based on user interactions:

dynamic-data.html
<object id="dynamicObject" data="chart-a.svg" type="image/svg+xml" width="200" height="150"></object>



<script>

  var objectElement = document.getElementById("dynamicObject");

  objectElement.data = "chart-b.svg";

</script>
Try It Yourself

How It Works

In this script, the data property of an object element with id dynamicObject is updated to point to a new resource URL. This dynamic flexibility helps build interactive pages that swap embedded files without reloading.

♿ Accessibility

  • Meaningful fallback — Provide descriptive text or download links inside <object>, not just “unsupported.”
  • Alt on fallback images — If fallback uses <img>, include proper alt text.
  • Prefer native elements — For images, video, and audio, native tags offer better accessibility than object embeds.
  • Title attribute — Consider title on object for a brief description of embedded content.
  • Keyboard access — Ensure fallback links and controls are keyboard reachable.

🧠 How data Works

1

Author sets data URL

object data points to PDF, image, or other file.

Markup
2

Browser fetches resource

type helps select the correct plugin or handler.

Load
3

Content renders or falls back

Inner HTML displays if embed is unsupported.

Fallback
=

External resource embedded

One tag loads files beyond basic HTML markup.

Browser Support

The data attribute on <object> is supported in all modern browsers. What actually renders depends on the file type and installed plugins (PDF viewers vary by browser).

HTML · Fully supported

Universal object data attribute

All major browsers parse data on object elements.

99% Attribute support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
data attribute on object 99% supported

Bottom line: The attribute works everywhere; test PDF and plugin rendering per browser. Prefer native HTML5 tags when possible.

💡 Best Practices

✅ Do

  • Use data on object for PDFs and specialized embeds
  • Set type to the correct MIME type
  • Include fallback HTML inside object
  • Prefer img, video, and audio for simple media
  • Test embedded content across browsers

❌ Don’t

  • Confuse data with data-* custom attributes
  • Use object for Flash or obsolete plugins
  • Mismatch type and file extension
  • Leave object empty with no fallback
  • Use data on div or span for JS metadata
  • Ensure the resource in data is compatible with the <object> element and the user’s browser.
  • Provide fallback content within <object> to maintain visibility when embed fails.
  • Regularly test embedded content across different browsers for a consistent experience.

Conclusion

The data attribute is a valuable tool when working with the <object> tag in HTML. It lets you point at external resources and create pages that embed PDFs, images, and other files beyond what basic tags alone provide.

Remember that this attribute is specifically for object resource URLs. To store custom values on any element for JavaScript, use data-* attributes covered in the next tutorial.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about data

Bookmark these before embedding external files.

5
Core concepts
🔗 02

URL Value

File address.

Syntax
📄 03

type MIME

Match format.

Pair
⚙️ 04

object.data

JS swap.

Script
📦 05

Not data-*

Different feature.

Compare

❓ Frequently Asked Questions

It sets the URL of the external resource embedded by an <object> element.
No. Custom metadata uses data-* attributes like data-id. The standalone data attribute is only for object resource URLs.
PDFs, images, SVG, and other embeddable formats. Simple images and media are better served by img, video, and audio.
Assign a new URL string: objectElement.data = "new-file.pdf".
Inner HTML is fallback content shown when the browser cannot render the resource from data.
No. Flash is obsolete and blocked in modern browsers. Use HTML5 video, canvas, or SVG instead.

Embed external resources with object

Practice the data attribute with PDF, image, and JavaScript swap examples in the Try It editor.

Try object data example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful