Lesson 11 +10 XP

Embedded Content

Sometimes you want to put another page, or a special thing, inside your page. HTML has tags for that.

iframe: a window into another page

<iframe> embeds another web page inside yours. Like a window showing someone else's house.

Code

html
<iframe src="https://example.com" width="600" height="400"></iframe>

iframe attributes

  • width and height: size of the window.
  • loading=&quot;lazy&quot;: only load when needed.
  • title: a description for screen readers (very important for iframes!).
Code

html
<iframe src="map.html" title="A map of the park" loading="lazy"></iframe>

The security helpers

For extra safety with iframes:

  • sandbox: limits what the embedded page can do.
  • allow: lets you allow specific features (like a camera or fullscreen).
  • allowfullscreen: lets the page go fullscreen.
Code

html
<iframe src="game.html" sandbox="allow-scripts" allowfullscreen></iframe>

embed: for plugins

&lt;embed&gt; is a quick way to embed a thing (like a PDF or a plugin).

Code

html
<embed src="brochure.pdf" width="500" height="300">

object: a flexible container

&lt;object&gt; can embed different kinds of content, including images or PDFs, and has a data attribute.

Code

html
<object data="report.pdf" type="application/pdf" width="500" height="300"></object>

param: parameters for object

&lt;param&gt; passes extra settings into an &lt;object&gt;.

New and experimental

  • &lt;fencedframe&gt;: a privacy-protected version of &lt;iframe&gt; used by some advertising tech. Very new, use it rarely!
  • &lt;geolocation&gt;: lets a page request the visitor's location permission. New and experimental.
  • &lt;selectedcontent&gt;: shows the currently selected option in a &lt;select&gt;. New and experimental.

These are cutting-edge. Keep an eye on MDN to see if they become standard!

TL;DR

  • &lt;iframe&gt; embeds another web page: always add a title.
  • &lt;embed&gt; quickly embeds things like PDFs.
  • &lt;object&gt; is a flexible container for external content.
  • &lt;param&gt; gives &lt;object&gt; extra settings.
  • Use sandbox, allow, and allowfullscreen for iframe safety.
  • &lt;fencedframe&gt;, &lt;geolocation&gt;, and &lt;selectedcontent&gt; are new and experimental.