An <iframe> (inline frame) is a window in your page that shows another whole web page inside it. It's how you drop a YouTube video, a Google Map, or a payment widget into your site without rebuilding it. Powerful — and worth handling with a little care.
1Embedding a page
Point an iframe at a URL with src, and size it with width and height:
<iframe src="https://example.com"
width="100%" height="300"
title="Example site"></iframe>title. Screen-reader users hear the title to know what the frame contains — without it they just hear "frame", which is useless. It's the most-skipped iframe accessibility step.Most embeds (YouTube, Google Maps) give you a ready-made iframe snippet to copy. You usually only tweak its width, height and title.
<iframe src="…">embeds another page as a window inside yours.- Size it with
widthandheight. - Always add a
titleso screen-reader users know what the frame contains.
2Keep iframes safe and fast
Because an iframe runs someone else's page inside yours, two attributes matter:
loading="lazy"— don't load the frame until the user scrolls near it. Embeds are heavy; this is a big speed win, especially on mobile data.sandbox— restrict what the embedded page can do (run scripts, submit forms, open pop-ups). You then opt back in only what's needed, e.g.sandbox="allow-scripts". An emptysandboxis the most locked-down.
Only embed sources you trust, and prefer https:// URLs so the connection is encrypted.
loading="lazy"defers heavy embeds until needed — a real speed win.sandboxrestricts what the embedded page can do; opt back in only what's necessary.- Only embed trusted sources, and prefer
https://URLs.
★ Practical: a safe embed
Write the markup (in any playground) for:
- An
<iframe>with asrc, a percentagewidthand a fixedheight. - A descriptive
titleattribute. - Add
loading="lazy"so it loads only when scrolled into view. - Add a
sandboxattribute to lock it down.
Ready to test yourself?
Take the short quiz. Score 60% or more to mark this lesson complete.
Start the quiz →💡 Log in to save your progress and earn the certificate.