Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 122 ยท Images and Media

Embedding Videos, Maps and Other Content

Embed videos, maps and other third-party content with iframes. Make embeds responsive, protect privacy, and see where embed and object fit today.

Hosting video yourself works for short clips, but for long videos most sites use a video platform that handles encoding, streaming and bandwidth. Maps, calendars, slide decks, music players and social posts are similar: someone else runs the service, and you place a small window onto it in your page.

Almost all of these embeds are iframes under the hood. In this lesson you will look at typical embed codes, make embeds resize gracefully, cut down the privacy and performance cost of third-party content, and learn what happened to the old <embed> and <object> elements and the plug-ins they were made for.

Anatomy of an embed code

Video and map services offer a Share โ†’ Embed option that gives you a snippet to paste. A typical video embed looks like this (VIDEO_ID stands for the ID of a real video):

HTML
<iframe width="560" height="315"
        src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
        title="How to repot a cactus (video)"
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
        referrerpolicy="strict-origin-when-cross-origin"
        allowfullscreen></iframe>

A map embed follows the same pattern. Here, YOUR_EMBED_CODE is the long parameter the map service generates for your chosen place:

HTML
<iframe src="https://www.google.com/maps/embed?pb=YOUR_EMBED_CODE"
        width="600" height="450"
        title="Map showing our shop on Harbor Street"
        style="border: 0;"
        loading="lazy"
        referrerpolicy="no-referrer-when-downgrade"></iframe>

Open-data maps such as OpenStreetMap offer the same thing from their Share panel, with the map area given as coordinates in the URL instead of an ID.

Before pasting any embed code, review it with what you learned in the previous lesson:

  • Replace the generic title. Generated codes often use a title like "YouTube video player". Describe the actual content instead.
  • Keep loading="lazy" (or add it) for embeds below the fold.
  • Trim allow to the features you need.
  • Do not strip referrerpolicy. Some providers check the referrer to make sure the embed is allowed on your site, and their player may refuse to load without it.
  • Remove obsolete attributes such as frameborder and use CSS for borders.

Use the embed URL, not the page URL. Pasting a normal watch page or map page address into src usually fails, because those pages refuse to be framed. Services provide a separate embed URL (often containing /embed/) that is built for iframes.

Responsive embeds with aspect-ratio

Embed codes come with fixed width and height, which overflow on a narrow phone. An iframe does not keep its proportions automatically the way an image does, so you have to tell it its shape. Modern CSS makes that a one-liner with aspect-ratio:

CSS
.embed {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  border: 0;
}

The example below uses a small page of our own with a <video> inside it as a stand-in for a video platform's player. Resize the browser window or change the ratio to 4 / 3 to see the frame keep its shape.

A responsive embed

You may still meet an older technique in existing code: a wrapper with padding-bottom: 56.25% (9 รท 16) and an absolutely positioned iframe inside. It worked before aspect-ratio existed, but you no longer need it. You will learn more about fluid layouts in Responsive Web Design.

Privacy and performance

An embed is not just a picture of someone else's content: it is someone else's web page running inside yours. As soon as it loads, the provider receives your visitor's IP address and browser details and may set cookies or run tracking scripts โ€” even if the visitor never presses play. A single video embed can also download more JavaScript than the rest of your page combined.

You have three tools to reduce that cost:

  1. Privacy-enhanced modes. Many providers offer one. YouTube serves embeds from youtube-nocookie.com, and Vimeo accepts a dnt=1 parameter in the embed URL. These reduce tracking, but they do not eliminate contact with the provider.
  2. Lazy loading. loading="lazy" delays the iframe until the visitor scrolls near it, so embeds far down the page cost nothing for people who never reach them.
  3. Click to load (a "facade"). Show a lightweight preview image with a play button, and create the real iframe only when the visitor clicks. Nothing from the third party loads until the visitor actually asks for it. In regions with strict privacy laws, this is also a common way to collect consent before contacting the provider.

Here is a facade. The button contains just an image; the iframe is created on click:

Click-to-load embed

A few details make the facade accessible: it is a real <button>, so it works with the keyboard; its text says what will play; and the image inside has alt="" because the button text already describes it. In a real page, the srcdoc would be replaced by the provider's embed URL. Most providers support an autoplay=1 URL parameter so the video starts right after the click instead of needing a second one.

Always offer a way out. Add a plain link to the content on the provider's site next to the embed ("Watch on the platform", "Open in maps"). It helps when the embed is blocked by an extension, a firewall or a privacy setting, and it gives everyone a choice.

Script-based embeds

Some services, especially social networks, give you a snippet that is not an iframe: a <blockquote> with the post's text and a link, followed by a <script> from the provider. The script replaces the blockquote with a rich, interactive version:

HTML
<blockquote class="social-post">
  <p>Our community garden harvested 40 kg of tomatoes this weekend!</p>
  <a href="https://social.example/posts/POST_ID">View the post</a>
</blockquote>
<script async src="https://social.example/embed.js"></script>

The good part: if the script fails or is blocked, readers still see the quote and the link. The bad part: that script runs with full access to your page, unlike an iframe, which stays in its own document. Only use script-based embeds from providers you trust, and load them with async so they do not block your page. The Adding JavaScript lesson explains async.

Many sites can generate embeds automatically from a plain URL using oEmbed, a protocol in which you send the content's URL to the provider and receive the embed HTML back. Content management systems use it when you paste a link into an editor and it turns into a player.

<embed>, <object> and the end of plug-ins

Before HTML had <video> and <audio>, browsers displayed rich content through plug-ins: separate programs such as Flash, Java applets, Silverlight and RealPlayer that drew inside a rectangle on the page. The <embed> and <object> elements existed to load them.

Plug-ins were a constant source of crashes and security holes, and they did not work on phones. Browsers removed support step by step, and Adobe ended Flash for good on December 31, 2020. Today's browsers run no plug-ins at all. Anything you once needed them for is now done with native HTML: <video>, <audio>, <canvas>, SVG and JavaScript.

Both elements still exist in HTML, and browsers use them to show content they can display natively, such as images, SVG files, PDFs and HTML pages:

  • <embed src="..." type="..."> is a void element with no fallback content.
  • <object data="..." type="..."> can contain fallback content that appears when the resource cannot be shown. Its old companion <param> is obsolete.
object with fallback

The most common remaining use is embedding a PDF:

HTML
<object data="/files/menu.pdf" type="application/pdf" width="100%" height="600">
  <p>Your browser cannot show the PDF here. <a href="/files/menu.pdf">Download the menu (PDF, 240 KB)</a>.</p>
</object>

Desktop browsers display PDFs with their built-in viewer, but many mobile browsers show the fallback or a download prompt instead. That is why the fallback link matters โ€” and often a plain link to the PDF is the better choice from the start.

Do not use <embed> or <object> for images, video or audio. Use <img>, <video> and <audio>, which are accessible, responsive and well supported. For other web pages, use <iframe>, which has title, sandbox, allow and lazy loading.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You paste a video embed into a narrow mobile layout and it overflows. What is the modern fix?

    Iframes do not keep their proportions on their own. aspect-ratio lets the height follow the width.
  2. 2. What is the main privacy benefit of a click-to-load facade?

    The page shows only your own preview image. The provider's iframe, and any tracking that comes with it, loads only after the click.
  3. 3. How does a script-based social embed differ from an iframe embed?

    Provider scripts run in your page's context. The blockquote fallback still shows the content if the script fails.
  4. 4. Which element offers fallback content when a resource cannot be displayed?

    The content inside object is shown when its data cannot be displayed. embed is a void element with no fallback.

Summary

  • Most video, map and widget embeds are iframes; use the provider's embed URL, not the normal page URL.
  • Review pasted embed codes: give a specific title, add loading="lazy", trim allow, keep the provider's referrerpolicy, drop obsolete attributes.
  • Make embeds responsive with width: 100%; height: auto; aspect-ratio: 16 / 9.
  • Embeds share visitor data with the provider: use privacy-enhanced modes, lazy loading or a click-to-load facade, and add a plain link as a fallback.
  • Script-based embeds run with full access to your page; use only trusted providers.
  • Plug-ins are gone; <embed> and <object> remain for things like PDFs, and <object> supports fallback content.

You have finished the Images and Media chapter. Next, you will look at how elements flow on the page in Block and Inline Elements.