← Learning Hub
Intermediate

Audio, Video and Iframes

HTML can play sound and video by itself, with no extra player. Add controls to show play, pause and volume buttons.

Before HTML5, playing video on a website needed plug-ins like Flash. Now the browser plays audio and video itself, on phones and computers, with standard controls. You give it the file and a few settings, and it handles buffering, full screen and volume.

Example
<video src="intro.mp4" width="480" controls poster="cover.jpg">
  Sorry, your browser cannot play this video.
</video>

<audio src="song.mp3" controls></audio>

Several formats

Not every browser plays every file type. Use <source> tags; the browser picks the first one it can play.

Example
<video width="480" controls muted autoplay loop playsinline>
  <source src="clip.webm" type="video/webm">
  <source src="clip.mp4" type="video/mp4">
</video>
  • autoplay - starts by itself. Browsers allow this only when the video is also muted.
  • loop - plays again from the start
  • playsinline - plays inside the page on iPhones instead of going full screen
  • poster - a picture shown before the video starts

Iframes: a page inside a page

An <iframe> shows another web page inside yours. YouTube and Google Maps give you iframe code to embed their videos and maps.

An iframe is a window into another website. It's the standard way to embed content you don't host yourself - a YouTube video, a Google Map with your shop's location, or a payment form. The embedded page runs separately from yours, which keeps both safe.

Example
<iframe src="https://www.youtube.com/embed/VIDEO_ID"
        width="560" height="315"
        title="Product demo video"
        loading="lazy"
        allowfullscreen></iframe>
Give every iframe a title so screen-reader users know what it is, and use loading="lazy" so it loads only when the user scrolls near it.
Only play music and videos you are allowed to use. Embedding from YouTube is fine; copying someone's video file to your site usually is not.
Key points
  • <video> and <audio> play media; add controls.
  • Offer several formats with <source>.
  • Autoplay works only when muted.
  • Iframes embed other pages; give them a title and loading="lazy".

📝 Test yourself

1. Which attribute shows the play and pause buttons?
2. When do browsers allow a video to autoplay?
3. Fill in the tag that shows another web page inside yours:
< src="https://..."></iframe>