HTML Embed Element
For a long time, the web could not do everything by itself. To show an animation, a rich game, or a special document, a page needed a plug-in: a separate program that the visitor had to install. The <embed> element was invented to hand a rectangle of the page to such a plug-in.
Those plug-ins have now disappeared from modern browsers. But the <embed> element is still part of HTML, still works for some files such as PDFs and SVG, and you will meet it in older code. This lesson explains what it does, what it cannot do, and why you will usually choose something else.
What You Will Learnβ
- What
<embed>is, and where it came from - Its few attributes:
src,type,width,height - Why it has no fallback content
- How it compares with
<object>,<iframe>, and<img> - Accessibility and security limits
- Better choices for most jobs
The Basicsβ
- HTML Code
- Browser Output
<embed
src="graphic.svg"
type="image/svg+xml"
width="240"
height="96"
>
<embed
src="guide.pdf"
type="application/pdf"
width="600"
height="400"
>
<embed src="graphic.svg"
type="image/svg+xml"
width="240" height="96">
The browser recognises the type and shows the file.
Built-in PDF viewer? checkingβ¦
This is a real embed element. Results can differ a little from browser to browser, which is part of the lesson.
Drag the width, and turn the type off. The element is real, and the result shows its biggest weakness: when the browser cannot show the file, you get an empty box and no message.
<embed> is a void element: it has no closing tag and no content. There is nowhere to put fallback text or a download link inside it.
Attributesβ
| Attribute | What it does | Notes |
|---|---|---|
src | The address of the file to embed | Use https:// |
type | The MIME type of the file, such as application/pdf or image/svg+xml | Helps the browser choose how to show it |
width, height | Size in pixels, with no units | Set both, or use CSS |
That is the whole official list. Any other attributes are passed along to whatever program handles the file. In the days of plug-ins, that is how you configured them. Today, there is almost nothing to pass to.
There is also no alt, no loading, no sandbox, and no referrerpolicy. You can add a title or an aria-label as a name for assistive technology, and CSS works on the element's box, but you cannot ask the browser to restrict what is inside.
Do Not Trust type Aloneβ
type is a hint. The browser also looks at the Content-Type header the server sends for the file. If they disagree, or if the file is something the browser will not show, you get an empty box. Make sure your server sends the right type for each file.
embed vs object vs iframe vs imgβ
- HTML Code
- Which Should I Use?
<!-- A picture: use img -->
<img src="logo.svg" alt="CodeHarborHub logo" width="120" height="40">
<!-- A PDF, with a fallback: use object -->
<object data="guide.pdf" type="application/pdf" width="600" height="400">
<a href="guide.pdf">Download the guide (PDF)</a>
</object>
<!-- A web page or widget: use iframe -->
<iframe src="https://example.com/widget" title="Widget" loading="lazy"></iframe>
<!-- embed: no fallback, no sandbox, no alt -->
<embed src="guide.pdf" type="application/pdf" width="600" height="400">
Better choice: object (or an iframe), plus a visible download link
And embed? Works on desktop, but gives you no fallback if the viewer is missing
<object data="guide.pdf" type="application/pdf" width="600" height="400">
<a href="guide.pdf">Download the guide (PDF, 1.2 MB)</a>
</object>
img | object | iframe | embed | |
|---|---|---|---|---|
| Fallback content | alt text | Yes, inside the element | Text inside (rarely shown) | None |
| Text alternative | alt | Fallback content | title | Only title or aria-label |
| Can show a web page | No | Yes | Yes, built for it | Not intended |
| Lazy loading | loading="lazy" | No | loading="lazy" | No |
| Sandbox | n/a | No | Yes | No |
| Best for | Pictures, SVG used as a picture | PDFs and older embedded files | Pages, players, maps, widgets | Rarely; older code |
The honest summary: in new code, you rarely need <embed>. For a graphic, use <img> or inline SVG. For a PDF, <object> gives you a fallback link. For another page, <iframe> gives you title, lazy loading, and sandbox.
Embedding a PDFβ
A PDF is the most common use today. Desktop browsers usually have a built-in viewer, so this works:
<embed src="guide.pdf" type="application/pdf" width="600" height="400">
But think about the visitors for whom it does not:
- Many phones and tablets show nothing, or only a small preview, or just offer a download.
- Some browsers have no built-in viewer and download the file.
- A visitor with a screen reader or only a keyboard may find the viewer hard to use, and may get stuck inside it.
- You get no fallback for any of them.
You can ask the browser whether it has a viewer, on the supported browsers:
if (navigator.pdfViewerEnabled) {
// safe to show an inline viewer
} else {
// show a plain download link instead
}
The simplest, kindest pattern is always: a visible link to the PDF, with its size, next to or instead of the viewer.
<p><a href="guide.pdf">Download the guide (PDF, 1.2 MB)</a></p>
And if the content matters, offer an HTML version too. HTML is easier to search, translate, read on a phone, and use with assistive technology than most PDFs.
Embedding SVGβ
An SVG file shown through <embed> is displayed as a separate document. Scripts inside the SVG can run, and the SVG can load other things. That is rarely what you want.
- For a picture, use
<img src="logo.svg" alt="β¦">. It is safe (scripts do not run), cacheable, and hasalt. - For a graphic you want to style or animate with your page's CSS and JavaScript, use inline SVG (see SVG).
A Note on Old Codeβ
You may see older pages that wrap an <embed> inside an <object>, so that one works if the other does not, usually for Flash:
<object data="movie.swf" type="application/x-shockwave-flash">
<embed src="movie.swf" type="application/x-shockwave-flash">
</object>
Flash, Java applets, and similar plug-ins no longer run in any current browser. If you find code like this, the content must be rebuilt with video, SVG, canvas, or JavaScript. Do not try to bring plug-ins back.
Accessibility at a Glanceβ
- Prefer elements with a text alternative:
imgwithalt,objectwith fallback content,iframewithtitle. - If you do use
embed, add atitleoraria-label, and put a visible text link to the file next to it. - Give a text version of anything important: an HTML page for a PDF, a data table for a chart.
- Test with the keyboard. A viewer inside the page can trap keyboard focus. Make sure a visitor can Tab out.
- Make PDFs accessible if you must publish them: tagged, with a title, language, headings, and alternative text for images.
- Do not put essential information only in the embedded file.
- Set a size so the page does not jump.
Security and Privacy Notesβ
<embed>has nosandbox. You cannot lock down what runs inside it. For anything you do not fully trust, use an<iframe>withsandbox.- Embedded files can carry active content. An SVG can contain scripts, and a PDF can contain links and actions. Never embed a file uploaded by a visitor without checking it.
- Serve the right headers. Send the correct
Content-Type, and addX-Content-Type-Options: nosniffso the browser does not guess a file's type. - Use
https://. An insecure embedded file in a secure page is blocked, or can be swapped by someone on the network. - Do not build
srcfrom user input. A visitor could make your page show a harmful or private file. Use a fixed list of allowed files. - Remember that the file is public. Anyone who sees the page can fetch the PDF or SVG directly.
- Third-party embedded files may report back to the other site (through the referrer, for example). See Third-Party Content Security.
Global Audience Notesβ
- Do not assume every device has a PDF viewer. Many phones in many regions do not. A plain download link works everywhere.
- PDFs are heavy. Show the size ("1.2 MB") so visitors on slow or metered connections can choose.
- Offer content in HTML, which can be translated by the browser and read in right-to-left languages without special viewers.
- Embedded PDF text may not follow the page's language settings. Mark the language in the file, and name it in your link ("Guide (PDF, English)").
- Make files available in more than one language where you can, and name them clearly.
Common Mistakes to Avoidβ
- Using
embedwhereimg,object,iframe, orvideowould do. - No visible link to the file, so visitors with no viewer get an empty box.
- No
type, or a wrongtype, so the browser does not know what to do. - A wrong
Content-Typeheader on the server. - No size, so the page jumps.
- Expecting fallback content, which
embeddoes not have. - Trying to bring back Flash or Java.
- Embedding files from users, or from addresses you do not control, without checks.
- Giving the only copy of important information to a PDF.
- Forgetting a
titleoraria-label. - Trapping keyboard users inside a viewer.
- Building
srcfrom user input.
Practice Timeβ
Try these in your editor, with a small SVG and a small PDF you have:
- Embed an SVG with
<embed>, and settype. - Change the
typeto a wrong value, and see what the browser does. - Embed a PDF, and check it on your computer and on your phone.
- Add a visible download link next to it, with the file size.
- Rewrite the PDF as an
<object>with fallback text. - Replace the SVG
<embed>with an<img>that hasalt. - Try to add
sandboxto an<embed>, and note that nothing happens. - Use
navigator.pdfViewerEnabledto choose between a viewer and a link. - Set the wrong
Content-Typeon your local server for the PDF, and see what changes. - Bonus: write the plain HTML version of a short PDF, and compare how each works with a screen reader.
Quick Recapβ
Click a question to check your answer.
1. What was embed originally for?
It handed a rectangle of the page to a browser plug-in, such as Flash or a Java applet. Those plug-ins have been removed from modern browsers.
2. What attributes does embed have?
src, type, width, and height. Other attributes were once passed to plug-ins, but that is rarely useful today.
3. Why is the lack of fallback content a problem?
embed is a void element with no content. If the browser cannot show the file, the visitor sees an empty box and no message, unless you add a link beside it.
4. Which element is a better choice for a PDF, and why?
object, because it can hold fallback content such as a download link. A visible link to the file is a good idea whichever element you use.
5. Can you sandbox an embed?
No. There is no sandbox attribute on embed. For content you do not trust, use an iframe with sandbox.
6. What should you use instead of embed for a picture?
img, with alt text and lazy loading. For a graphic you need to style or script, use inline SVG.
Key Takeawaysβ
<embed>is a void element withsrc,type,width, andheight, and no fallback content.- It came from the plug-in era. Flash, Java applets, and similar plug-ins no longer run.
- Today it can still show PDFs and SVG files, but better elements usually exist.
- Use
imgfor pictures,objectfor PDFs with a fallback,iframefor pages and widgets, andvideooraudiofor media. - Always provide a visible link and a text version of important content.
embedhas no sandbox, noalt, and no lazy loading. Treat what it loads with care.- Send the right
Content-Type, use HTTPS, and never buildsrcfrom user input. - Do not assume a PDF viewer on every device.
Next, learn the element that gives embedded files a proper fallback, in Object, or return to the Embedded Content overview.