References

Beginner-friendly references for web development, with live, editable examples.

The HTML <object> type attribute

Attribute All modern browsers
In short

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

Live 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 type whenever 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 pdf or html isn'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+xml for SVG, video/mp4 for MP4 and text/html for a web page.

Frequently asked questions

What does the type attribute tell the browser about an object?
The type attribute tells the browser the MIME type of the file named in data, such as video/mp4. Scripts can read and change it through the object's type property, which returns the value exactly as written.
What type should I use to embed a PDF in an object?
Use application/pdf, the MIME type for PDF files, as the type. Browsers with a built-in PDF viewer then show the document, and others can show the fallback content.
What happens if an object has no type attribute?
The type attribute is optional, so an object is valid without it. The browser then goes by the Content-Type sent with the file, and a generic application/octet-stream file usually won't display, though Chrome shows an image whose URL ends in .png.
How does type on an object differ from type on input or script?
On an object, type holds the MIME type of the embedded file. On input, type picks the kind of control, such as checkbox, and on a script element, type set to module loads a JavaScript module.
Does an SVG with type image/svg+xml behave like an image?
No, image/svg+xml is an XML type, so an SVG file loads into a nested document instead of an image.