The HTML <object> type attribute
The HTML type attribute labels the file in an <object> with its MIME type, such as application/pdf. A browser that can't display that type may skip the file and show the fallback. The type the server sends with the file usually decides what appears.
Overview
The HTML type attribute tells an <object> which MIME type to expect from the URL in its data attribute. Its value has to be a valid MIME type, a type and a subtype joined by a slash, as in image/svg+xml, with optional parameters.
Once the file arrives, the type sent with it usually wins. A page served as text/html still displays as a page under type="image/png", and a text file stays text.
With no type on the response at all, the HTML standard lets the attribute choose between an image and a nested document. A text or HTML file marked as an image then gets the fallback in Firefox, and Chrome displays it anyway.
Generic binary is another exception. Many servers send files they don't recognize as application/octet-stream, and some send no type at all. For such a file, or binary data labeled text/plain, an image type other than SVG, such as image/png, makes the object display it as an image.
Editing type on an object that already has a data attribute doesn't reload it, while changing data does.
Syntax
<object data="floor-plan.svg" type="image/svg+xml"
width="400" height="300">
<img src="floor-plan.png" alt="Floor plan of the second floor">
</object>
Values
| Value |
|---|
A MIME type, such as application/pdf. |
Example
<p>All three objects say type="image/png", but each data: URL carries its own type, as a server would. Compare them, then remove type.</p>
<object data="data:text/plain,Packing%20list" type="image/png" width="110" height="40"></object>
<object data="data:text/html,%3Cb%3EOrder%20page%3C/b%3E" type="image/png" width="110" height="40"></object>
<object data="data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAZ0lEQVR4AcyQUQrAIAxDi/fYfneb7ay7jf56EaV/jVgJKKgQqCQ+UsP1xjKjIJPnMED+H2Fkt167wv0lYeQ2YOprxgVYg53hD5j6mrFwAGi9nuyDdgZAazJ3AGi9nkYgAIyCnrcfUAEAAP//pXlIggAAAAZJREFUAwDPGXJBl2lnswAAAABJRU5ErkJggg==" type="image/png" width="40" height="40"></object>
<p><button type="button" id="drop">Remove type from all three</button></p>
<p id="msg" role="status">Text, a page and an icon. Only the octet-stream file became an image.</p>
<script>
let loads = 0;
const objects = document.querySelectorAll('object');
for (const obj of objects) {
obj.addEventListener('load', function () {
loads++;
});
}
document.getElementById('drop').addEventListener('click', function () {
const before = loads;
for (const obj of objects) obj.removeAttribute('type');
setTimeout(function () {
document.getElementById('msg').textContent = 'type removed. New load events: ' + (loads - before) + (loads === before ? ', so nothing reloaded.' : '.');
}, 1000);
});
</script>
Best practices
- Set
typewhenever you know the format. A browser that can't display that type can then go straight to the fallback content without requesting the file. - Write the full MIME type, with both parts. A value such as
pdforhtmlisn't valid, so Chrome and Firefox skip the file and fall back. - Keep the value in lowercase. MIME types are case-insensitive, yet Firefox shows the fallback content for
TEXT/HTML. - Use the standard media type for each format, such as
image/svg+xmlfor SVG,video/mp4for MP4 andtext/htmlfor a web page.























