HTML Tutorial
HTML YouTube
Embed a YouTube player with an iframe. Keep it responsive so it does not overflow the page.
A player from another site
YouTube videos are not <video src="..."> files on your server. You embed YouTube’s player in an <iframe>. The iframe loads a page from YouTube inside yours.
That is the same iframe idea as maps or a live preview: another document, framed in your layout. You still owe the visitor a title, and you still have to stop the frame from stretching the page sideways.
The embed URL
Watch URLs look like https://www.youtube.com/watch?v=VIDEO_ID. Embed URLs use a different path:
https://www.youtube.com/embed/VIDEO_ID
Replace VIDEO_ID with the id from the watch link. The examples below usedQw4w9WgXcQ as a stand-in. Swap it for the clip you actually want.
Example
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube video player"
allowfullscreen>
</iframe>Never paste the /watch?v= URL into src. The embed player only loads from /embed/.
title and allow
title describes the iframe for screen readers and browser tabs inside the frame. “YouTube video player” is a start; a short name of the clip is better.
allow lists features the embedded page may use: fullscreen, autoplay, a clipboard, and similar. YouTube’s share dialog often includes a longer allowstring. You do not need to memorize every token — copy it from YouTube, keeptitle, and add allowfullscreen if you want the player’s fullscreen button to work.
Example
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="Sample clip"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>Autoplay inside YouTube still follows browser rules. A muted autoplay parameter on the embed URL may start; sound usually waits for a click.
A 16:9 wrapper
Fixed width="560" overflows a phone. Wrap the iframe in a box that keeps a 16:9 ratio and let the iframe fill that box.
Example
<style>
.video-wrap {
position: relative;
width: 100%;
max-width: 720px;
aspect-ratio: 16 / 9;
}
.video-wrap iframe {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
<div class="video-wrap">
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="Sample clip"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>aspect-ratio: 16 / 9 matches typical YouTube videos. The iframe is stretched to the wrapper, so it scales down instead of pushing the page sideways.
Try it in the HTML preview
Open the example in /html/try — StudyGrid’s live HTML preview, not the Python editor. The iframe will load YouTube if the preview allows third-party frames. If the player is blank, the sandbox may block it; the markup is still what you publish on a normal site.
Change dQw4w9WgXcQ to another id from a YouTube URL. Everything afterv= (before any extra & parameters) is the id.
Checklist
- Use
youtube.com/embed/VIDEO_ID, not the watch URL - Always set a
title - Copy
allowandallowfullscreenfrom YouTube’s embed code when unsure - Wrap the iframe so 16:9 scales to the column width
Next: web storage — saving small strings in the browser between visits, without a server.
Worked examples
The short listings above show the tag in isolation. These pages use the same markup on documents you would actually publish: a lab report, a clinic form, a timetable, a weather card.
HTML does not calculate. It names the pieces so a browser, a screen reader, and a search engine can tell a heading from a paragraph. The numbers below are classroom values — the same Ohm, pH, and pulse figures as the C track — now sitting in real page structure.
Preview them in the HTML editor at /html/try. Change a heading or a number and watch the page, not a print log.
Physics
A lecture embed that names itself
YouTube embeds are iframes. title must describe the video. allowfullscreen is the usual permission. Privacy-enhanced youtube-nocookie hosts exist when policy requires them.
Wrap the iframe in a CSS ratio box in the responsive chapter so a 16:9 player does not overflow a phone.
The sample URL is a placeholder embed. Swap in a real lecture id for class.
Example
<iframe
width="280"
height="158"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="Sample lecture embed"
allowfullscreen>
</iframe>Astronomy
Keep the player labelled
An iframe without title is an unnamed region in the accessibility tree. “YouTube video” is weak; “Mars fly-by, NASA clip” is the job.
Example
<iframe width="280" height="158" src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="Placeholder sky clip" allowfullscreen></iframe>