Tutorials › HTML Tutorial › Audio, Video and Iframes

Audio, Video and Iframes

10 min read · Lesson 18 of 22

HTML5 lets you play sound and video directly in the browser, with no plug-ins. You can also embed whole pages from other sites, such as a YouTube video or a Google Map, using an iframe.

The video element

Example HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Video</title>
</head>
<body>
  <h2>Sample video</h2>
  <video width="400" controls>
    <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
    Your browser does not support the video element.
  </video>
</body>
</html>
Try it Yourself »
  • controls shows play, pause, volume and full-screen buttons. Without it, the user has no way to start the video.
  • Each <source> offers a file. The browser plays the first one it understands, so you can list several formats.
  • The text inside <video> is shown only by very old browsers that cannot play video.

Useful video attributes

AttributeEffect
controlsShow the player controls
width, heightSize of the player in pixels
posterImage shown before the video starts
autoplayStart playing automatically (browsers only allow this if the video is also muted)
mutedStart with the sound off
loopStart again when it finishes
playsinlinePlay inside the page on iPhones instead of going full-screen
preloadHint how much to download in advance: none, metadata or auto
Tip: Video files are large. For visitors on mobile data, set preload="none" or preload="metadata" and add a poster image so nothing heavy downloads until they press play.

Captions with track

The <track> element adds subtitles or captions from a WebVTT (.vtt) file. Captions help deaf viewers, people watching in noisy matatus, and people who prefer reading along.

<video controls width="640">
  <source src="lesson-1.mp4" type="video/mp4">
  <track src="lesson-1-en.vtt" kind="captions" srclang="en" label="English">
  <track src="lesson-1-sw.vtt" kind="subtitles" srclang="sw" label="Kiswahili">
</video>

The audio element

<audio> works the same way as <video>, without the picture.

Example HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Audio</title>
</head>
<body>
  <h2>Listen to a sample</h2>
  <audio controls>
    <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>
  <p>Perfect for podcasts, radio clips or Kiswahili pronunciation guides.</p>
</body>
</html>
Try it Yourself »

Common media formats

TypeFormatMIME type
VideoMP4 (H.264)video/mp4
VideoWebMvideo/webm
AudioMP3audio/mpeg
AudioOggaudio/ogg
AudioWAVaudio/wav

MP4 video and MP3 audio play in every modern browser, so they are the safest choices.

Iframes

An <iframe> (inline frame) shows another web page inside your page. It is how YouTube videos, Google Maps and many booking widgets are embedded.

Example HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Iframe</title>
  <style>
    iframe { border: 1px solid #ccc; max-width: 100%; }
  </style>
</head>
<body>
  <h2>Find us in Nairobi</h2>
  <iframe
    src="https://www.openstreetmap.org/export/embed.html?bbox=36.80%2C-1.30%2C36.84%2C-1.27&amp;layer=mapnik"
    width="450" height="300"
    title="Map of Nairobi city centre"
    loading="lazy">
  </iframe>
</body>
</html>
Try it Yourself »

Important iframe attributes:

  • src is the address of the page to embed.
  • title describes the frame for screen readers. Always include it.
  • width and height set the size.
  • loading="lazy" delays loading until the user scrolls near it.
  • allow grants features such as full-screen or autoplay to the embedded page.
  • sandbox restricts what the embedded page can do, which is useful for content you do not fully trust.

Embedding YouTube

On YouTube, click Share, then Embed, and copy the code. It looks like this (with a real video ID in place of VIDEO_ID):

<iframe width="560" height="315"
  src="https://www.youtube.com/embed/VIDEO_ID"
  title="Video title here"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen>
</iframe>

Embedding saves you storage and bandwidth because the video streams from YouTube's servers, not yours.

Warning: Many websites (banks, Google search, most social networks) block being shown inside iframes for security reasons. If an iframe stays blank, the site you are embedding probably does not allow it.
Exercise

Show the play and pause buttons on the video.

<video src="intro.mp4" ></video>
Exercise

Embed another page and describe it for screen readers.

< src="map.html" title="Map of our Eldoret office"></iframe>
Sign in or create a free account to track progress, earn XP, badges and a certificate.