Embedded Content
HTML 4.0
The object element represents an external resource, which can be treated as an image, a nested browsing context, or a resource to be handled by a plugin. Its key advantage over <embed> is the ability to provide fallback content for progressive enhancement.
Interactive code playground requires JavaScript. Here's the code:
<!-- PDF with fallback -->
<object
data="document.pdf"
type="application/pdf"
width="600"
height="400">
<p>Unable to display PDF.
<a href="document.pdf">Download the PDF</a> instead.</p>
</object>
<!-- SVG with fallback image -->
<object
data="logo.svg"
type="image/svg+xml"
width="200"
height="100">
<img src="logo.png" alt="Company Logo">
</object>
<!-- Nested fallbacks -->
<object data="video.mp4" type="video/mp4">
<object data="video.webm" type="video/webm">
<p>Your browser doesn't support this video format.
<a href="video.mp4">Download the video</a>.</p>
</object>
</object>
< object data = " resource.ext " type = " mime/type " >
The <object> element requires both opening and closing tags. Content between the tags serves as fallback if the resource cannot be loaded or displayed.
Attribute Description Example dataURL of the resource data="document.pdf"typeMIME type of the resource type="application/pdf"widthWidth in pixels or percentage width="600" or width="100%"heightHeight in pixels height="400"nameName for form submission and scripting name="pdf-viewer"
Attribute Description Example formAssociates object with a form form="myform"typemustmatchEnforces type/data MIME type match typemustmatch
Attribute Description Status classidWindows ActiveX class ID Deprecated codebaseBase URL for relative paths Deprecated codetypeMIME type of code Deprecated declareDeclare without instantiating Deprecated standbyLoading message Deprecated archiveSpace-separated resource URLs Deprecated borderBorder width Deprecated - use CSS align, hspace, vspacePositioning Deprecated - use CSS
Interactive code playground requires JavaScript. Here's the code:
<!-- PDF with download fallback -->
<object
data="annual-report-2024.pdf"
type="application/pdf"
width="100%"
height="600">
<p>
Your browser doesn't have a PDF plugin.
<a href="annual-report-2024.pdf" download>Download the PDF</a>
or <a href="annual-report-2024.html">view HTML version</a>.
</p>
</object>
<!-- PDF with specific page -->
<object
data="manual.pdf#page=5"
type="application/pdf"
width="800"
height="600">
<p>PDF viewer not available.
<a href="manual.pdf">Download manual</a>.</p>
</object>
PDF URL Parameters:
#page=N - Open to page N
#zoom=150 - Set zoom percentage
#toolbar=0 - Hide toolbar
#navpanes=0 - Hide navigation
#view=FitH - Fit to width
#pagemode=bookmarks - Show bookmarks
Interactive code playground requires JavaScript. Here's the code:
<!-- SVG with PNG fallback -->
<object
data="chart.svg"
type="image/svg+xml"
width="600"
height="400"
aria-label="Sales chart showing 25% growth">
<img src="chart.png" alt="Sales chart showing 25% growth">
</object>
<!-- Interactive SVG with fallback -->
<object
data="interactive-map.svg"
type="image/svg+xml"
width="800"
height="600">
<p>Interactive map not available.
<a href="map.html">View static map</a>.</p>
</object>
Interactive code playground requires JavaScript. Here's the code:
<!-- WebP with JPEG fallback -->
<object
data="photo.webp"
type="image/webp"
width="800"
height="600">
<img src="photo.jpg" alt="Mountain landscape">
</object>
<!-- Better approach: use <picture> element -->
<picture>
<source srcset="photo.webp" type="image/webp">
<source srcset="photo.jpg" type="image/jpeg">
<img src="photo.jpg" alt="Mountain landscape">
</picture>
Interactive code playground requires JavaScript. Here's the code:
<!-- Multiple levels of fallback -->
<object data="content.svg" type="image/svg+xml">
<object data="content.webp" type="image/webp">
<object data="content.png" type="image/png">
<p>Unable to display image.
<a href="content.png">Download PNG version</a>.</p>
</object>
</object>
</object>
<!-- Video with nested fallbacks -->
<object data="video.mp4" type="video/mp4">
<object data="video.webm" type="video/webm">
<object data="video.ogv" type="video/ogg">
<p>Video not supported.
<a href="video.mp4">Download video</a>.</p>
</object>
</object>
</object>
Interactive code playground requires JavaScript. Here's the code:
<!-- Legacy plugin with parameters -->
<object
data="animation.swf"
type="application/x-shockwave-flash"
width="800"
height="600">
<param name="quality" value="high">
<param name="wmode" value="transparent">
<param name="allowscriptaccess" value="sameDomain">
<p>Flash plugin not available.</p>
</object>
<!-- Modern alternative: HTML5 -->
<video width="800" height="600" controls>
<source src="animation.mp4" type="video/mp4">
<source src="animation.webm" type="video/webm">
Your browser doesn't support video.
</video>
Feature <object><iframe><embed>Fallback content ✅ Yes (nested) ✅ Yes ❌ No Security sandbox ❌ No ✅ Yes ❌ No Permissions Policy ❌ No ✅ Yes ❌ No HTML documents ✅ Yes ✅ Yes ✅ Yes PDF support ✅ Yes ✅ Yes ✅ Yes SVG support ✅ Yes ✅ Yes ✅ Yes Form integration ✅ Yes ❌ No ❌ No Lazy loading ❌ No ✅ Yes ❌ No Browser support Excellent Excellent Good
Best For:
Content with fallback needs
SVG with PNG/JPEG fallback
PDF with download option
Progressive enhancement
Legacy system compatibility
Example:
< object data = " chart.svg " type = " image/svg+xml " >
< img src = " chart.png " alt = " Sales chart " >
Best For:
Embedding external websites
Security-sensitive content
Third-party widgets
Isolated browsing contexts
Modern web applications
Example:
src = " https://example.com "
title = " External content " >
Best For:
Quick PDF embedding
Legacy plugin content
Simple embeds without fallback
Example:
Note: Prefer <object> or <iframe> over <embed> for most use cases.
aria-label = " 2024 Sales Infographic " >
< img src = " infographic.png "
alt = " Bar chart showing 25% sales increase in Q4 2024 " >
title = " Annual Financial Report 2024 " >
< h2 > Annual Financial Report 2024 </ h2 >
< p > PDF viewer not available. </ p >
< li >< a href = " report.pdf " download > Download PDF (2.5 MB) </ a ></ li >
< li >< a href = " report.html " > View HTML version </ a ></ li >
< li >< a href = " report.txt " > Download text version </ a ></ li >
<!-- No fallback content -->
<!-- Unhelpful fallback -->
Interactive code playground requires JavaScript. Here's the code:
<!-- Image object -->
<object
data="diagram.svg"
type="image/svg+xml"
width="600"
height="400"
role="img"
aria-label="System architecture diagram showing three-tier design">
<img src="diagram.png" alt="System architecture diagram">
</object>
<!-- Application object -->
<object
data="calculator.html"
type="text/html"
width="400"
height="500"
role="application"
aria-label="Interactive calculator">
<p>Calculator not available.
<a href="calculator.html">Open in new window</a>.</p>
</object>
<!-- Decorative object -->
<object
data="decoration.svg"
type="image/svg+xml"
width="100"
height="100"
aria-hidden="true">
</object>
Interactive code playground requires JavaScript. Here's the code:
<!-- Interactive object (default behavior) -->
<object
data="interactive-chart.svg"
type="image/svg+xml"
width="800"
height="600"
title="Interactive sales chart">
<img src="chart.png" alt="Sales chart">
</object>
<!-- Remove from tab order if not interactive -->
<object
data="static-image.svg"
type="image/svg+xml"
width="200"
height="100"
tabindex="-1"
aria-hidden="true">
</object>
Interactive code playground requires JavaScript. Here's the code:
<style>
.styled-object {
border: 3px solid #3b82f6;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
display: block;
margin: 20px auto;
background: #f9fafb;
}
.object-wrapper {
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.object-wrapper object {
display: block;
margin: 0 auto;
}
</style>
<div class="object-wrapper">
<object
data="document.pdf"
type="application/pdf"
class="styled-object"
width="90%"
height="500">
<p style="color: white; text-align: center;">
<a href="document.pdf" style="color: white;">Download PDF</a>
</p>
</object>
</div>
Interactive code playground requires JavaScript. Here's the code:
<style>
.responsive-object-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
height: 0;
overflow: hidden;
max-width: 100%;
}
.responsive-object-container object {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
/* 4:3 aspect ratio */
.ratio-4-3 {
padding-bottom: 75%;
}
/* Square aspect ratio */
.ratio-1-1 {
padding-bottom: 100%;
}
</style>
<div class="responsive-object-container">
<object
data="video.mp4"
type="video/mp4">
<p>Video not supported. <a href="video.mp4">Download</a>.</p>
</object>
</div>
Interactive code playground requires JavaScript. Here's the code:
<style>
.object-container {
position: relative;
min-height: 400px;
}
.object-container::before {
content: 'Loading...';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #666;
font-size: 18px;
}
.object-container object {
width: 100%;
height: 400px;
}
</style>
<div class="object-container">
<object
data="large-document.pdf"
type="application/pdf">
<p>Unable to load PDF. <a href="large-document.pdf">Download</a>.</p>
</object>
</div>
Interactive code playground requires JavaScript. Here's the code:
<object
id="svg-object"
data="interactive.svg"
type="image/svg+xml"
width="400"
height="300">
<img src="fallback.png" alt="Chart">
</object>
<script>
const obj = document.getElementById('svg-object');
// Wait for object to load
obj.addEventListener('load', () => {
// Access object's document (same-origin only)
const objDoc = obj.contentDocument;
if (objDoc) {
// Manipulate SVG content
const svg = objDoc.querySelector('svg');
const circle = objDoc.querySelector('circle');
if (circle) {
circle.setAttribute('fill', '#3b82f6');
}
console.log('Object loaded successfully');
}
});
// Handle errors
obj.addEventListener('error', () => {
console.error('Failed to load object');
});
</script>
Interactive code playground requires JavaScript. Here's the code:
<div id="object-container"></div>
<script>
function embedPDF(container, url) {
const obj = document.createElement('object');
obj.data = url;
obj.type = 'application/pdf';
obj.width = '100%';
obj.height = '600';
// Create fallback
const fallback = document.createElement('div');
fallback.innerHTML = `
<p>PDF viewer not available.</p>
<a href="${url}" download>Download PDF</a>
`;
obj.appendChild(fallback);
// Test if object loads
let loaded = false;
obj.addEventListener('load', () => {
loaded = true;
});
setTimeout(() => {
if (!loaded) {
console.log('Object failed to load - fallback displayed');
}
}, 2000);
container.appendChild(obj);
}
embedPDF(document.getElementById('object-container'), 'document.pdf');
</script>
Interactive code playground requires JavaScript. Here's the code:
<button onclick="changeDocument('doc1.pdf')">Document 1</button>
<button onclick="changeDocument('doc2.pdf')">Document 2</button>
<button onclick="changeDocument('doc3.pdf')">Document 3</button>
<object
id="pdf-viewer"
data="doc1.pdf"
type="application/pdf"
width="100%"
height="500">
<p>PDF not available. <a id="download-link" href="doc1.pdf">Download</a>.</p>
</object>
<script>
function changeDocument(filename) {
const obj = document.getElementById('pdf-viewer');
const link = document.getElementById('download-link');
// Update object data
obj.data = filename;
// Update fallback link
link.href = filename;
link.textContent = `Download ${filename}`;
}
</script>
The <object> element can be associated with forms and participates in form submission:
Interactive code playground requires JavaScript. Here's the code:
<form id="myform" action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<!-- Object associated with form -->
<object
data="plugin.html"
type="text/html"
name="plugin-data"
form="myform"
width="400"
height="200">
<p>Plugin not available.</p>
</object>
<button type="submit">Submit</button>
</form>
<script>
// The object can contribute data to form submission
// if it implements appropriate interfaces
</script>
Interactive code playground requires JavaScript. Here's the code:
<!-- Restrict object sources with CSP -->
<meta http-equiv="Content-Security-Policy"
content="object-src 'self' https://trusted-cdn.com;">
<!-- Only allow specific MIME types -->
<object
data="https://trusted-cdn.com/document.pdf"
type="application/pdf"
width="600"
height="400">
<p>PDF not available.</p>
</object>
CSP Directives:
object-src: Controls which sources can be used in <object>, <embed>, and <applet>
plugin-types: Restricts MIME types (deprecated)
Always specify the type attribute to prevent content type confusion:
< img src = " image.png " alt = " Fallback " >
<!-- No type - browser guesses from content -->
< object data = " unknown-file.bin " >
The typemustmatch attribute forces the browser to verify that the type attribute matches the actual MIME type:
Interactive code playground requires JavaScript. Here's the code:
<object
data="document.pdf"
type="application/pdf"
typemustmatch
width="600"
height="400">
<p>PDF not available or type mismatch.</p>
</object>
Feature Chrome Firefox Safari Edge <object> element1+ 1+ 1+ 12+ PDF embedding 1+ 19+ 5+ 12+ SVG embedding 4+ 4+ 5+ 12+ typemustmatch❌ No 50+ ❌ No ❌ No Flash plugin ❌ Removed ❌ Removed ❌ Removed ❌ Removed Java plugin ❌ Removed ❌ Removed ❌ Removed ❌ Removed Nested objects 1+ 1+ 1+ 12+
Possible Causes:
Incorrect MIME type
Resource not found (404)
Browser doesn’t support the content type
CSP blocking the source
CORS issues for cross-origin content
Solutions:
Interactive code playground requires JavaScript. Here's the code:
<!-- Ensure correct MIME type -->
<object
data="document.pdf"
type="application/pdf"
width="600"
height="400">
<p>If you see this, the PDF didn't load.
<a href="document.pdf">Download PDF</a>.</p>
</object>
<!-- Check browser console for errors -->
<script>
const obj = document.querySelector('object');
obj.addEventListener('error', (e) => {
console.error('Object failed to load:', e);
});
</script>
Error : “Cannot read property ‘querySelector’ of null”
Cause : Trying to access cross-origin object content or accessing before load.
Solution:
Interactive code playground requires JavaScript. Here's the code:
<object
id="my-object"
data="same-origin-content.svg"
type="image/svg+xml">
</object>
<script>
const obj = document.getElementById('my-object');
// Wait for load event
obj.addEventListener('load', () => {
try {
const doc = obj.contentDocument;
if (doc) {
// Successfully accessed content
console.log('Content accessed');
} else {
console.log('Content blocked (cross-origin)');
}
} catch (e) {
console.error('Access denied:', e);
}
});
</script>