HTML Media Elements

HTML Media Elements allow you to embed multimedia content like audio, video, and images directly into web pages. Using tags such as <audio>, <video>, and <source>, developers can create interactive and engaging user experiences without external plugins.

3 views
5 min read
Try It Yourself

Experiment with the code in an interactive editor

What are Media Elements?

HTML5 lets you add videos and audio directly without plugins like Flash.

Video Tag:

1<!-- Basic video with controls -->
2<video width="640" height="360" controls>
3    <source src="my-video.mp4" type="video/mp4">
4    <source src="my-video.webm" type="video/webm">
5    Your browser does not support the video tag.
6</video>
7
8<!-- Advanced video with all options -->
9<video width="640" height="360" 
10       controls     <!-- Shows play/pause buttons -->
11       autoplay     <!-- Starts playing automatically (usually needs muted) -->
12       loop         <!-- Repeats forever -->
13       muted        <!-- Starts with no sound -->
14       poster="thumbnail.jpg"  <!-- Shows image before play -->
15       preload="metadata">     <!-- Loads only video info, not the whole video -->
16    <source src="video.mp4" type="video/mp4">
17</video>

Video attributes explained:

Attribute

What it does

When to use

controls

Shows play, pause, volume buttons

Always use for user control

autoplay

Starts playing as soon as page loads

Background videos (usually with muted)

loop

Plays again and again

Background videos, short clips

muted

Starts with sound off

Required for autoplay in most browsers

poster

Shows thumbnail before play

To show preview image

preload

"auto", "metadata", or "none"

"metadata" is best for performance

Audio Tag:

1<!-- Basic audio -->
2<audio controls>
3    <source src="my-song.mp3" type="audio/mpeg">
4    <source src="my-song.ogg" type="audio/ogg">
5    Your browser does not support the audio tag.
6</audio>
7
8<!-- Background music (usually muted autoplay) -->
9<audio controls autoplay loop>
10    <source src="background-music.mp3" type="audio/mpeg">
11</audio>

YouTube Embed:

1<iframe width="560" height="315" 
2    src="https://www.youtube.com/embed/VIDEO_ID_HERE" 
3    title="YouTube video" 
4    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
5    allowfullscreen>
6</iframe>

How to get YouTube embed code:

  1. Go to any YouTube video

  2. Click "Share" button

  3. Click "Embed"

  4. Copy the code