Skip to main content

HTML Images and Media

HTML Embed & Object

Written by Updated HTML Embed & Object exercises

Want to show a PDF inside your page? embed and object both do it. The difference is what happens when the browser cannot display the file: embed shows nothing, object lets you show a download link instead.

Both elements put an outside file into the page. Today that is almost always a PDF. Images, audio, video and other web pages have their own better tags: <img>, <audio>, <video> and <iframe>.

HTML <embed> Element

<embed> is a single tag with no closing tag and nothing inside it. You point it at a file and give it a size.

Syntax

html

<embed src="example.pdf" type="application/pdf" width="600" height="400">
  • src is the address of the file.
  • type tells the browser what kind of file it is, such as application/pdf.
  • width and height set the size of the box, in pixels.

HTML Embed Example

html

<embed src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf" type="application/pdf" width="600" height="800">

That shows the PDF in a box inside the page, using the browser's own PDF viewer.

HTML <embed> Attributes

AttributeWhat it does
srcThe address of the file to show.
typeThe kind of file (MIME type), like application/pdf.
widthWidth of the box in pixels.
heightHeight of the box in pixels.

Older tutorials list autoplay, loop and hidden for embed. Those were never standard HTML; they were passed to browser plugins, which no longer exist.

HTML <object> Element

<object> does the same job, but it has a closing tag, and whatever you put between the tags is shown if the file cannot be displayed. That backup content is the reason to choose it.

HTML Object Syntax

html

<object data="example.pdf" type="application/pdf" width="600" height="400">
  Your browser does not support embedded objects.
</object>
  • data is the address of the file (note: data, not src).
  • type is the kind of file.
  • width and height set the size.
  • The content inside is the fallback.

HTML Object Example

html

<object data="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf" type="application/pdf" width="600" height="800">
  <p>It appears your browser does not support PDF viewing. <a href="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf">Download the PDF</a>.</p>
</object>

If the browser can show PDFs, you see the document. If not, for example on many phones, the reader gets a "Download the PDF" link instead of an empty box.

HTML <object> Attributes

AttributeDescription
dataURL or path of the external file.
typeMIME type of the content.
widthWidth of the embedded object.
heightHeight of the embedded object.
formAssociates the object with a form.
nameAssigns a name to the object for scripting purposes.

embed or object?

Questionembedobject
Fallback if the file cannot be shownNoYes, the content inside
Closing tagNoYes
File address attributesrcdata
Best forQuick embeds where you know it will workAnything readers on phones might open

In practice, choose object for PDFs, so phone users still get a way to open the file.

Hide the PDF toolbar

You can add settings to the end of the PDF address after a #:

PDF Toolbar Removing Example with <embed>

html

<embed src="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf#toolbar=0&navpanes=0&scrollbar=0" type="application/pdf" width="600" height="500">

PDF Toolbar Removing with <object>

PDF Toolbar Removing Example with <object>

html

<object data="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf#toolbar=0&navpanes=0&scrollbar=0" type="application/pdf" width="600" height="500">
  <p>Your browser cannot display this PDF. <a href="https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf">Download PDF</a></p>
</object>

toolbar=0 hides the toolbar, navpanes=0 hides the side panel, and scrollbar=0 hides the scrollbar. These are hints for the browser's PDF viewer, not HTML. Chrome and Edge follow most of them; other browsers may ignore them, so do not rely on them to stop people downloading the file.

Tips

  • Always set width and height, so the page does not jump while the file loads.
  • Many phone browsers cannot show PDFs inside a page at all. Always offer a plain download link too.
  • For media, use <audio> and <video>. For another web page, use an iframe.
  • Do not add <param> tags inside <object>. They were for old plugins and are obsolete.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Embed & Object exercises

Embed a document

Embed the PDF https://ik.imagekit.io/html5andcss3/lesson-samples/sample.pdf with an embed element and say its type, application/pdf.

Result

Check your answer to see it here.