HTML data Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The data attribute on an <object> is the URL of the resource to embed — a PDF, image, SVG, or similar file. Pair it with type (MIME type) and optional width / height. At least one of data or type must be present.

Put fallback HTML inside <object> for when the resource cannot be shown. This attribute is not the same as data-* custom attributes.

Baseline · Widely available.

Not deprecated and not experimental. Standard <object> attribute across modern browsers.

Not data-*.

data-id, data-price, and similar names are custom data attributes on any element. The standalone data attribute is only the resource URL on <object>.

HTML Syntax

syntax.html
<object data=" URL" type="MIME">…</object>

URL may be relative or absolute. Child content is the fallback when the resource fails.

Attribute Values

A valid URL string pointing at the embedded resource:

ValueDescriptionTry it
Relative pathResolved from the current page, e.g. report.pdfTry it
Root-relative pathFrom the site root, e.g. /images/logo.pngTry it
Absolute URLFull URL to another host (subject to CSP / network rules)Try it
object.data (JS)Read or set the URL string at runtimeTry it

Live Preview

Change the data URL on a real <object>. Fallback text shows if the resource cannot be displayed.

PDF display depends on the browser’s built-in viewer. Images should render inside the object box.

object

Cannot display this resource. Open the file.

data="/images/javascript.png"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — PDF with data

Point data at a PDF and declare type. Keep a download link in the fallback.

HTML
<object
  data="/files/camel.pdf"
  type="application/pdf"
  width="320"
  height="240">
  <p>PDF unavailable. <a href="/files/camel.pdf">Download</a>.</p>
</object>
Try It Yourself

How It Works

  1. data is the resource URL the browser should load.
  2. type="application/pdf" hints the MIME type for the viewer.
  3. width / height size the embed box in CSS pixels.
  4. Fallback inside the tag is shown if the PDF cannot be displayed.

Example 2 — Image with nested fallback

Embed an image via data; nest an <img> as backup. Prefer <img> alone for simple pictures.

HTML
<object data="/images/javascript.png" type="image/png" width="128" height="128">
  <img src="/images/logo.png" alt="Fallback logo" width="128" height="128">
</object>
Try It Yourself

How It Works

  1. data loads the primary image into the object.
  2. type="image/png" matches the file format.
  3. Nested img is fallback content if the object resource fails.
  4. For images only, a plain <img> is usually clearer and more accessible.

Example 3 — Change data with JavaScript

Assign a new URL to the data property to swap the embedded resource.

HTML
<button type="button" id="swapBtn">Switch image</button>
<object id="demo" data="/images/javascript.png" type="image/png" width="128" height="128">
  <p>Resource unavailable.</p>
</object>
<script>
  var obj = document.getElementById("demo");
  var logo = false;
  document.getElementById("swapBtn").addEventListener("click", function () {
    logo = !logo;
    obj.data = logo
      ? "/images/logo.png"
      : "/images/javascript.png";
  });
</script>
Try It Yourself

How It Works

  1. object.data mirrors the HTML data attribute.
  2. Assigning a URL tells the browser to load a different resource.
  3. type can stay the same when both files share a MIME type.
  4. Keep fallback content for cases where the new URL fails.

Edge Cases & Pitfalls

Use the right embed element, and do not confuse data with data-*.

vs data-*

Different features

data = object resource URL. data-* = custom metadata via dataset. See the data-* tutorial next.

prefer

Prefer dedicated tags

Use <img>, <video>, <audio>, or <iframe> when they fit. Reserve <object> for PDFs and cases that need nested fallback.

required

data or type

The HTML spec requires at least one of data or type. Providing both is the usual, clearer pattern.

CSP

Content Security Policy

Object embeds are governed by object-src (not img-src). A strict CSP can block the resource even when the URL is valid.

obsolete

Old object attributes

archive, classid, codebase, and similar plugin-era attributes are obsolete. Stick to data, type, size, and fallback content.

Browser Support

data is an HTML attribute on <object> in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

data

Widely available on object. PDF rendering quality depends on the browser’s built-in viewer.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer All versions
Supported
data Widely supported

Bottom line: Safe for embedding with object. Always include fallback content for unsupported viewers or blocked resources.

Key Takeaways

  • Role: URL of the resource embedded by <object>.
  • Pair with: type, size attributes, and nested fallback HTML.
  • JS: read/write via objectElement.data.
  • Do not confuse with data-* custom attributes.

One line: data tells <object> which file to load — nest fallback content for when it cannot.

Frequently Asked Questions

On an object element, data is the URL of the external resource to embed (PDF, image, SVG, and similar). At least one of data or type must be set.
Only object. It is not the same as data-* custom attributes used on any element for script metadata.
No. data is a single attribute for an object resource URL. data-* attributes (data-id, data-price, …) store custom values and map to element.dataset in JavaScript.
Prefer img for images and video/audio for media. Use object with data when you need fallback content inside the tag (common for PDFs) or a generic embed.
Yes. Assign a URL string to objectElement.data to point the embed at another resource.
Older attributes like archive, classid, and codebase are obsolete. The data attribute itself is current and Baseline widely available.

Next: data-*

Store custom metadata on any element with data-* attributes.

data-* tutorial →

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.

8 people found this page helpful