—HTML Series · Post 15: HTML5 Media

HTML5 Media — Video, Audio & Embeds | CodeHerWay
Post 15 · HTML Fundamentals

HTML5 Media:
Video, Audio & Embeds

No plugins. No Flash. HTML5 gives you native video, audio, and the ability to embed anything from YouTube to Google Maps — all with a few tags.

#HTML #Media #HTML5 10 min read

There was a time when adding video to a website required Flash Player, three browser plugins, and a prayer. HTML5 changed all of that. Now you can embed video, audio, and content from other sites using native HTML elements — no plugins, no hacks, no dependencies.

This post covers the four media-related elements you need to know: <video>, <audio>, <source>, and <iframe>. Plus how to embed content from YouTube, Google Maps, and other platforms.

The Four Media Elements

🎬
<video>
Self-hosted video
Plays video files you host on your own server. Supports controls, autoplay, looping, poster images, and multiple formats via <source>.
🎵
<audio>
Self-hosted audio
Plays audio files — podcasts, music, sound effects. Same attribute pattern as video but renders as a compact player bar.
🔄
<source>
Format fallback
Goes inside <video> or <audio>. Lets you provide multiple file formats so the browser picks the one it supports.
🪟
<iframe>
External embed
Embeds another webpage inside yours — YouTube videos, Google Maps, CodePen demos, Spotify players, and more.

<video> — Self-Hosted Video

The <video> element plays video files that you host on your own server or file system. It gives you full control over the player — controls, autoplay behavior, poster images, looping, and more.

HTML
<!-- Basic video with controls --> <video controls width="640"> <source src="videos/tutorial.mp4" type="video/mp4"> <source src="videos/tutorial.webm" type="video/webm"> Your browser doesn't support video. </video> <!-- Video with poster image (thumbnail) --> <video controls poster="images/video-thumb.jpg" width="640"> <source src="videos/demo.mp4" type="video/mp4"> </video> <!-- Muted autoplay background video --> <video autoplay muted loop playsinline> <source src="videos/hero-bg.mp4" type="video/mp4"> </video>

Key attributes for <video>:

controls width height poster autoplay muted loop playsinline preload
⚠️ Autoplay Rules

Modern browsers block autoplay with sound. If you use autoplay, you must also use muted — otherwise the browser silently ignores it. This is a user experience decision by browsers, and it's a good one. Nobody wants a website screaming at them on page load.

Autoplay + muted is perfect for background hero videos. For anything the user should actively watch, use controls instead and let them press play.

Why <source> Matters

Different browsers support different video formats. By providing multiple <source> elements, the browser picks the first format it understands. MP4 has the widest support, but WebM offers better compression. Include both to cover all bases.

The text between the opening and closing <video> tags ("Your browser doesn't support video.") is the fallback content — it only shows if the browser doesn't support the video element at all. Extremely rare today, but good practice.

<audio> — Self-Hosted Audio

The <audio> element works almost identically to <video> — same pattern, same <source> fallbacks, same attributes. The difference is that it renders as a compact audio player bar instead of a video rectangle.

HTML
<!-- Basic audio player --> <audio controls> <source src="audio/podcast-ep1.mp3" type="audio/mpeg"> <source src="audio/podcast-ep1.ogg" type="audio/ogg"> Your browser doesn't support audio. </audio> <!-- With figure/figcaption for context --> <figure> <figcaption>Episode 1: Why I Started Coding</figcaption> <audio controls src="audio/ep1.mp3"></audio> </figure>
💡 When to Self-Host vs Embed

Self-host (using <video> and <audio>) when you need full control over the player, the files are small, or you're building something like a background video or sound effect. Embed (using <iframe>) when the content lives on another platform — YouTube, Spotify, SoundCloud. Embedding offloads bandwidth and gives you the platform's player for free.

<iframe> — Embedding External Content

The <iframe> (inline frame) is how you embed content from other websites into your page. It creates a window into another site — YouTube videos, Google Maps, CodePen demos, Spotify players, Calendly widgets — all running inside your page.

Embedding YouTube

YouTube gives you an embed code. On any video, click Share → Embed, and it hands you an iframe. Here's what it looks like:

HTML · YouTube Embed
<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" title="CSS Flexbox Tutorial" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> </iframe>
▶️ YouTube Embed — CSS Flexbox Tutorial
▶️
CSS Flexbox Tutorial
youtube.com/embed/VIDEO_ID

Embedding Google Maps

HTML · Google Maps
<!-- Share → Embed a map from Google Maps --> <iframe src="https://www.google.com/maps/embed?pb=..." width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy" title="Office Location"> </iframe>
📍 Google Maps Embed — Office Location
🗺️
Interactive Google Map
google.com/maps/embed

Embedding Other Sites

The pattern is always the same — the platform gives you an embed URL, and you put it in an <iframe>. CodePen demos, Spotify playlists, Figma files, Calendly scheduling widgets — they all work this way. Just look for the "Embed" or "Share" option on the platform.

ℹ️ Always Add a Title

The title attribute on iframes is important for accessibility. Screen readers announce it so users know what the embedded content is without seeing it. Don't leave it blank — describe what's embedded: "CodeHerWay intro video", "Office location map", "Spotify podcast player."

Making Embeds Responsive

By default, iframes have fixed pixel dimensions — they don't resize with the screen. On mobile, a 560px-wide YouTube embed overflows the viewport. Here's the modern CSS fix:

HTML + CSS
<!-- HTML: wrap the iframe --> <div class="video-wrapper"> <iframe src="https://www.youtube.com/embed/VIDEO_ID" title="Tutorial" allowfullscreen> </iframe> </div> /* CSS: responsive 16:9 container */ .video-wrapper { position: relative; padding-bottom: 56.25%; /* 16:9 ratio */ height: 0; overflow: hidden; } .video-wrapper iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
💡 The Modern Way

If you're using modern CSS, you can skip the padding hack and use aspect-ratio: 16/9 directly on the iframe. It's cleaner, but check browser support for your audience. The padding-bottom trick works everywhere.

iframe Security — What to Know

Iframes load an entire external webpage inside your page. That comes with security considerations. Never embed untrusted content, and use these attributes to lock things down:

HTML · Security Attributes
<!-- loading="lazy" — don't load until user scrolls to it --> <iframe loading="lazy" ...></iframe> <!-- sandbox — restricts what the embedded page can do --> <iframe sandbox="allow-scripts allow-same-origin" ...></iframe> <!-- referrerpolicy — control what info you send --> <iframe referrerpolicy="no-referrer" ...></iframe>
🧠 The Embed Mindset

Think of an iframe like inviting someone else's website into your living room. If it's YouTube, Google Maps, or CodePen — trusted platforms — you're fine. But you wouldn't embed a random URL you found on the internet. Only embed content from sources you trust, and use loading="lazy" on anything below the fold to keep your page fast.

// dev_fession

I once added a YouTube embed to my portfolio and shipped it with autoplay enabled. On the YouTube embed URL. I didn't realize it was there because I copied the embed code from somewhere else without reading it.

So every time someone opened my portfolio, a tutorial video started blaring out of their speakers. A recruiter emailed me to say "cool portfolio, but the screaming video scared my cat."

I removed autoplay that same minute. Always read the embed code before you paste it. 🐱

🔨 Mini Build: Media Gallery Page

Create a media gallery page that uses all four media elements:

1
Add a self-hosted <video> with controls, a poster image, and two <source> formats (MP4 and WebM). Wrap it in a <figure> with a <figcaption>.
2
Add an <audio> player for a podcast or music track, also wrapped in a figure with a caption describing the episode.
3
Embed a YouTube video using an <iframe>. Wrap it in a responsive video-wrapper div so it scales on mobile.
4
Embed a Google Map showing your city using an iframe with loading="lazy" and a descriptive title attribute.
5
Make sure every media element has proper alt text, title attributes, and fallback content between the opening and closing tags.

The Bottom Line

<video> and <audio> give you native media players — no plugins required. <source> provides format fallbacks so every browser finds something it can play. And <iframe> lets you embed content from any platform that supports it — YouTube, Maps, CodePen, Spotify, and beyond.

Use controls for anything users should actively play. Use muted autoplay loop for background visuals. Always add a title to iframes for accessibility. And make your embeds responsive so they don't break on mobile.

Media makes your sites feel alive. Now you know how to add it properly.

Next
Next

What Is HTML & How the Web Works