Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to embed a YouTube video, a map or another site

Short answer

Insert an <iframe> with the page address. It needs a size and a title for screen readers:

<iframe src="https://example.com" title="Example Domain" width="320" height="180" style="border: 1px solid silver;"></iframe>

Videos and maps

YouTube, map services and social networks provide ready code: the Share button → Embed. That is an <iframe> with the address of the embeddable page — on YouTube it contains /embed/ plus the video id. Copy it, then adjust the size and add title and loading="lazy" if they are missing.

Lazy loading

An embedded player or map pulls in a lot of scripts. loading="lazy" defers loading until the visitor scrolls to it:

<p style="height: 260px;">Scroll down ↓</p>
<iframe src="https://example.com" title="Example Domain" width="320" height="180" loading="lazy"></iframe>

Restricting the embedded page

The sandbox attribute without a value forbids scripts, forms and pop-ups inside the iframe. The needed abilities are listed in it as words, for example sandbox="allow-scripts".

Common mistake

src gets the regular address of a video or page, for example the link from the YouTube address bar. Most large sites forbid showing themselves in a frame, and an error appears instead of the content. Take the address from the Embed code — on YouTube it contains /embed/.

Functions used in this recipe

  • <iframe> — Embeds another page inside the current one: a map, a video, a widget.
  • title — Extra information about an element; shown as a tooltip on mouse hover.
  • loading — Lazy loading for images and frames that are not yet visible.

Thanks for reading!

Donate to the site — we work hard for you.