HTML Tutorial / Embed Audio, Video, and External Content
HTML can play media files with audio and video, or display another document inside a page with iframe. Successful embedding depends on real file paths and supported formats. Useful controls, captions or transcripts, and the trustworthiness of external content matter too.
Play audio and video files
The example assumes sample.mp3, sample.mp4, and sample-poster.webp are next to the HTML file. To run it, supply files you have the right to use and adjust the names as needed. Media also needs correct filenames, paths, and dimensions, following the principles in Images: img, Alt Text, and Dimensions.
<audio controls>
<source src="sample.mp3" type="audio/mpeg">
Your browser does not support audio playback.
</audio>
<video controls width="640" height="360" poster="sample-poster.webp">
<source src="sample.mp4" type="video/mp4">
<track src="sample-en.vtt" kind="captions" srclang="en"
label="English captions" default>
Your browser does not support video playback.
</video>
controls provides the browser's playback controls, and poster is shown before a video plays. For speech or meaningful sounds, provide accurate captions. The referenced sample-en.vtt is not automatically created. Without that file, the captions cannot appear.
A WebVTT caption file is UTF-8 text. This minimal example shows one cue for the first three seconds; real captions must cover the video's speech and important sounds at the correct times.
WEBVTT
00:00:00.000 --> 00:00:03.000
Welcome to Small Garden.
When important information is conveyed by audio, also provide a text transcript. Let users start playback themselves rather than unexpectedly autoplaying sound. If you offer multiple source formats, the underlying files must actually use those formats; renaming an extension does not convert media.
Embed external content with iframe
An iframe displays another document within the current page. The URL below is a structural placeholder, not a working media embed. Replace it with an official embed URL supplied by the provider.
<iframe
src="https://example.com/embed/example"
title="Introduction to the example content"
width="640" height="360"
loading="lazy"></iframe>
The title describes the frame's purpose. A provider may forbid embedding even if its ordinary page URL works. External content can affect privacy, cookies, and performance, so use a trusted provider's official embed code only when it adds value. State the embedded item's title or context in nearby text so the surrounding article still makes sense when it does not load.
Make media fit and test a local frame
An HTML width of 640 does not automatically fit a narrow viewport. Put this CSS in a style element in head or in a linked stylesheet. Video keeps its intrinsic ratio; the iframe exercise uses a 16:9 display box. Adjust that ratio when the embedded content requires a different one.
video, iframe {
display: block;
width: 100%;
max-width: 640px;
}
video { height: auto; }
iframe {
height: auto;
aspect-ratio: 16 / 9;
}
You can test frame paths, layout, and alternative navigation without a video file. Save these two UTF-8 files together. This exercise does not depend on an external service or personal data.
embed.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Practice instructions</title>
</head>
<body>
<h1>Small Garden instructions</h1>
<p>This is a local example displayed inside an iframe.</p>
</body>
</html>
frame.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Frame practice</title>
<style>
video, iframe {
display: block;
width: 100%;
max-width: 640px;
}
video { height: auto; }
iframe {
height: auto;
aspect-ratio: 16 / 9;
}
</style>
</head>
<body>
<h1>Read the instructions</h1>
<iframe src="embed.html" title="Small Garden instructions" width="640" height="360"></iframe>
<p><a href="embed.html">Open the instructions as a separate page</a></p>
</body>
</html>
Open frame.html and check the embedded heading and paragraph, then test that the frame fits a 390px viewport. Follow the separate-page link too. If you later use an external embed, check that provider's official URL and embedding policy.
WebVTT is a text format that pairs time intervals with captions. If captions fail from a local file, first check existence, extension, UTF-8, and cue timing, then compare by serving the HTML, video, and VTT from the same-origin HTTP development server. File-URL restrictions and server media types can affect loading. Renaming an extension is not media conversion.
<p><a href="sample.mp4">Open the example video file</a></p>
<p><a href="transcript.html">Read the video transcript</a></p>
Point the alternatives to files and a transcript you actually prepared. Fallback text inside audio/video is for browsers that do not support the element; it is not an error message guaranteed to appear when a supporting browser cannot load a file. Keep necessary alternatives and transcripts available outside the player as well.
If media will not play or fit
- Verify that the file exists at the specified path and the server sends the appropriate media type.
- Check whether the browser supports the file's actual encoding, not only its filename extension.
- Use CSS to limit the width on small screens without distorting the video's aspect ratio.
- For external content, check the provider's embedding policy and terms.
See the MDN video reference and iframe reference for element-specific attributes. Apply the manual-testing scope in Accessibility Basics: Headings, Links, Images, and Keyboard to playback controls, alternative links, and keyboard use.




