object
The object element is a legacy mechanism for embedding external resources. Modern alternatives like img, video, audio, iframe, and svg are preferred.
Description
The <object> element embeds an external resource such as an image, PDF, or SVG. It is a legacy embedding mechanism from earlier HTML specifications, originally designed to handle plugin content (Flash, Java applets, etc.).
In modern HTML, dedicated elements handle each media type more effectively. The main remaining advantage of <object> is its fallback content pattern: any HTML placed between the opening and closing tags is displayed when the external resource cannot be loaded.
When to Use
In nearly all cases, a modern alternative is the better choice. Consider <object> only when you specifically need the nested fallback pattern for a non-HTML resource.
Prefer Modern Alternatives
Examples
SVG with Fallback
Embed an SVG file with a PNG fallback for older browsers.
<object data="diagram.svg" type="image/svg+xml" width="400" height="300"> <p>Your browser does not support SVG. <a href="diagram.png">View as PNG</a>.</p></object>
PDF Embed
Display a PDF inline with a download link fallback.
<object data="document.pdf" type="application/pdf" width="100%" height="600"> <p>This browser does not support inline PDFs. <a href="document.pdf">Download the PDF</a>.</p></object>
Nested Fallback Chain
<object> elements can be nested to create a fallback chain. The browser tries each in order, stopping at the first one it can render.
<object data="animation.svg" type="image/svg+xml"> <object data="animation.gif" type="image/gif"> <img src="animation.png" alt="Animated diagram showing data flow" /> </object></object>
The Fallback Content Pattern
Unlike void elements such as <img> and <embed>, <object> supports fallback content between its tags. This content is rendered when:
- The resource specified by
datacannot be loaded - The browser does not support the resource's MIME type
- The
typeattribute specifies an unsupported format
This makes <object> useful for progressive enhancement scenarios where you want a graceful degradation path.
Attributes
| Attribute | Description |
|---|---|
data |
URL of the resource to embed. Required for most use cases. |
type |
MIME type of the resource (e.g., image/svg+xml, application/pdf). Helps the browser decide how to handle the content. |
width |
Display width of the object in pixels. |
height |
Display height of the object in pixels. |
name |
Name for the embedded browsing context (used with forms and links targeting this object). |
form |
Associates the object with a <form> element by ID. |
This element also supports global attributes.
Accessibility
Because <object> embeds opaque external content, accessibility depends on providing good fallback content and ARIA attributes.
<object data="chart.svg" type="image/svg+xml" role="img" aria-label="Quarterly revenue chart"> <p>Quarterly revenue: Q1 $1.2M, Q2 $1.5M, Q3 $1.8M, Q4 $2.1M.</p></object>
Best Practices
- Always provide meaningful fallback content between the
<object>tags - Add
role="img"andaria-labelwhen embedding images or charts - Fallback content should convey the same information as the embedded resource
- Include download links in fallback content so users can still access the resource
- Prefer native elements (
<img>,<video>) which have built-in accessibility semantics
Related
<embed>- Void element for embedding; no fallback content support<iframe>- For embedding HTML documents with full security controls<img>- For images (raster and external SVG); usealttext instead of fallback content<video>- For video with native controls, captions, and multiple sources<audio>- For audio with native controls and multiple sources<svg>- For inline vector graphics with full CSS and DOM control