iframe
Embeds another HTML document.
Learn HTML Iframes with practical examples for structure, layout semantics, metadata, embedded content, scripts, accessibility, SEO and live practice.
HTML structure and layout
An iframe can show another page, a video, a map, a dashboard or a sandboxed demo inside the current page. It is powerful, but it also creates security, performance and accessibility decisions.
A good iframe has a clear title, sensible dimensions, loading behavior and security attributes when needed.
Do not use iframes casually. Every iframe loads another document and can affect trust, speed and privacy.
Embeds another HTML document.
The URL loaded inside the frame.
Accessible name for the embedded content.
Restricts what the embedded page may do.
Syntax and structure
The title should describe the embedded content. The sandbox attribute can reduce risk for untrusted content.
<iframe src="/demo/preview.html" title="Live code preview" loading="lazy" sandbox="allow-scripts"> </iframe>
<iframe src="https://unknown-example.com"></iframe>
HTML quick reference
Use these patterns when you need the syntax quickly. Each example has its own anchor, so search engines and readers can land directly on the exact pattern instead of only at the top of the lesson.
A clean version of the markup from this lesson. Use it when you need the correct HTML shape quickly.
<iframe src="/demo/preview.html" title="Live code preview" loading="lazy" sandbox="allow-scripts"> </iframe>
Meaningful markup that stays understandable before CSS and JavaScript are added.
The starting point from the practice lab. Change the HTML first, then use CSS only for presentation.
<main class="demo-card"> <h1>Iframe checklist</h1> <p>This lesson platform uses sandboxed previews for code labs.</p> <iframe srcdoc="<h2>Hello iframe</h2>" title="Small iframe demo" sandbox></iframe> </main> <style> body { margin: 0; min-height: 100vh; display: grid; place-items: center; font-family: Inter, system-ui, sans-serif; background: #07111f; color: white; } .demo-card { width: min(820px, calc(100% - 32px)); padding: 34px; border-radius: 24px; background: #101a2d; box-shadow: 0 30px 80px rgba(0, 0, 0, 0.3); } h1 { margin: 0 0 16px; font-size: clamp(34px, 7vw, 58px); line-height: 1; } h2 { margin: 0 0 10px; } p, li { color: #cfd8e6; line-height: 1.7; } a { color: #8cffc1; font-weight: 900; } button { border: 0; border-radius: 999px; padding: 12px 16px; background: #8cffc1; color: #07111f; font-weight: 900; } iframe { width: 100%; height: 180px; border: 1px solid rgba(140,255,193,0.35); border-radius: 16px; background: white; } </style>
A complete practice snippet that shows how the HTML behaves in context.
A weak pattern from the lesson. Use it as a warning sign when reviewing real pages.
<iframe src="https://unknown-example.com"></iframe>
A recognizable mistake you can search for and refactor.
Rules that matter
Layout-focused HTML is strongest when meaning, grouping, metadata and behavior hooks all have a clear reason to exist.
The title explains the frame to assistive technology.
Restrict embedded content unless it needs specific permissions.
loading="lazy" can reduce initial page cost.
Avoid layout jumps by defining width, height or aspect ratio.
Embedding third-party content can introduce privacy and security concerns.
Tell users what the embedded content is about.
Production thinking
This matters because iframes are boundaries. They can isolate useful content, but they can also hide complexity and risk.
A frame without a title is hard to identify. Users should know what the iframe contains before entering it.
Audit third-party iframe providers. They can affect cookies, tracking, performance and Content Security Policy.
Search engines may not treat iframe content as primary page content. Important text should exist on the page itself.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.