HTML Media Get Certified
What is HTML Media?
HTML media refers to embedding multimedia content such as video and audio into web pages using the <video> and <audio> elements. HTML5 makes it easy to add rich media experiences without plugins.
Types of HTML Media
- HTML Video: Use the
<video>tag to embed video files with controls. - HTML Audio: Use the
<audio>tag to play sound files. - Embedded Video: Integrate videos from platforms like YouTube using
<iframe>. - Responsive Media: Make media elements adapt to different screen sizes using CSS.
HTML Media Code Examples
1. Basic HTML Video Element
A simple video player using the <video> tag with controls.
What this code does: Displays a video player with play/pause controls using a publicly available sample video.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML Video Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
video { max-width: 100%; background: #000; }
</style>
</head>
<body>
<h2>Basic HTML Video</h2>
<video width="480" height="270" controls>
<source src="https://www.youtube.com/watch?v=-ZIqYS5dWfQ" type="video/mp4">
Your browser does not support the video tag.
</video>
<p>This is a basic HTML5 video element with controls.</p>
</body>
</html>
2. Embedding a YouTube Video
Embed a YouTube video using an <iframe>.
What this code does: Shows how to embed a YouTube video player inside an HTML page.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>YouTube Embed Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
iframe { max-width: 100%; }
</style>
</head>
<body>
<h2>YouTube Video Embed</h2>
<iframe width="560" height="315"
src="https://www.youtube.com/watch?v=-ZIqYS5dWfQ"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
<p>This example embeds a YouTube video using an iframe.</p>
</body>
</html>
3. HTML Audio Element
A simple audio player using the <audio> tag.
What this code does: Creates an audio player with controls that plays an MP3 file.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML Audio Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
</style>
</head>
<body>
<h2>HTML Audio Player</h2>
<audio controls>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<p>This is a basic HTML5 audio element with playback controls.</p>
</body>
</html>
4. Responsive Video
Make a video responsive so it scales on smaller screens.
What this code does: Uses CSS so the video always fits the container width.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Responsive Video</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
.video-container {
max-width: 640px;
}
video {
width: 100%;
height: auto;
display: block;
background: #000;
}
</style>
</head>
<body>
<h2>Responsive HTML Video</h2>
<div class="video-container">
<video controls>
<source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<p>This video scales automatically to fit different screen sizes.</p>
</body>
</html>
Additional Tips
Always provide multiple <source> elements for different formats (MP4, WebM) for better browser support. Include the controls attribute so users can play, pause, and adjust volume. For accessibility, consider adding captions using the <track> element.
Web Resources on HTML Media
1. HTML.com : Embedding Modern Media in HTML
2. MDN : HTML Multimedia and Embedding
3. MDN : The HTML video element
Questions and Answers related to HTML Media
HTML media elements such as <video> and <audio> allow you to embed multimedia content. Use the controls attribute for playback controls and provide one or more <source> tags for different file formats.
Use the <video> element with the controls attribute. Add one or more <source> elements pointing to different formats (MP4, WebM) for better browser compatibility.
Use an <iframe> with the YouTube embed URL (found under the Share → Embed option on YouTube). Set appropriate width and height, and add allowfullscreen for full-screen support.
With the <video> tag you host the file yourself and control everything. YouTube embeds are easier (no hosting or bandwidth concerns) but rely on YouTube’s player and policies.
Set the video’s CSS width: 100% and height: auto, or wrap it in a container that maintains aspect ratio. This ensures the video scales properly on different screen sizes.
Use the <audio controls> element and provide a <source> with the audio file (e.g. MP3). Always include fallback text for browsers that do not support the audio element.
🧪 Test Your HTML Code
Edit the HTML code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.