Media · codenexium.com

Multimedia Tags in HTML

HTML5 introduced native <audio> and <video> elements that allow embedding media directly in web pages without plugins like Flash. Modern browsers support a range of audio and video formats.

audio tag

The <audio> element embeds sound content (music, podcasts, sound effects).

Default Behavior

HTML
<audio src="song.mp3"></audio>
Live Output Window

With just the src attribute, the audio element is invisible on the page - nothing is shown and nothing plays automatically.

Adding Controls

Add the controls attribute to show play/pause, volume, and progress:

HTML
<audio src="song.mp3" controls></audio>
Live Output Window

MIME Type

Specify the audio format using the type attribute inside a <source> element:

HTML
<audio controls>
<source src="song.mp3" type="audio/mpeg">
<source src="song.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
Live Output Window

The browser plays the first format it supports. The text between <audio> and </audio> is a fallback for very old browsers.

Supported audio formats:

Format MIME Type Browser Support
MP3 audio/mpeg All browsers
OGG Vorbis audio/ogg Firefox, Chrome, Opera
AAC audio/aac Safari, Chrome
WAV audio/wav All browsers (uncompressed, large)

Autoplay

HTML
<audio src="background.mp3" autoplay></audio>
Live Output Window

Note: Most browsers block autoplay with audio unless the media is muted. See “Muted Playback” below.

Looping

HTML
<audio src="loop.mp3" loop controls></audio>
Live Output Window

Muted Playback

HTML
<audio src="intro.mp3" muted controls></audio>
Live Output Window

The muted attribute starts the audio with the volume off. Combined with autoplay, this is the only way to reliably autoplay media in modern browsers:

HTML
<audio src="background.mp3" autoplay muted loop>
<!-- Background music that starts silently -->
</audio>
Live Output Window

JavaScript Events

HTML
<audio id="player" src="song.mp3" controls></audio>

<script>
const audio = document.getElementById("player");

audio.onplay = () => console.log("Playing");
audio.onpause = () => console.log("Paused");
audio.onended = () => console.log("Finished");
audio.ontimeupdate = () => console.log(audio.currentTime);
</script>
Live Output Window

Key events: play, pause, ended, timeupdate, loadeddata, error.

video tag

The <video> element embeds video content.

Default Behavior

HTML
<video src="movie.mp4"></video>
Live Output Window

Without controls, the video is invisible (though the audio still plays).

Adding Controls

HTML
<video src="movie.mp4" controls></video>
Live Output Window

Shows play/pause, progress bar, volume, fullscreen, and (on most browsers) picture-in-picture.

MIME Type

Multiple <source> elements for format fallback:

HTML
<video controls width="640" height="360">
<source src="movie.mp4" type="video/mp4">
<source src="movie.webm" type="video/webm">
<source src="movie.ogv" type="video/ogg">
Your browser does not support the video element.
</video>
Live Output Window

Supported video formats:

Format MIME Type Notes
MP4 (H.264) video/mp4 Universal support
WebM (VP8/VP9) video/webm Smaller size, open format
OGG (Theora) video/ogg Legacy, now uncommon

Autoplay

HTML
<video src="intro.mp4" autoplay></video>
Live Output Window

Same restriction as audio - autoplay is blocked unless muted is also present.

Looping

HTML
<video src="loop.mp4" loop controls></video>
Live Output Window

Poster Image

A poster image is displayed before the video starts:

HTML
<video src="movie.mp4" controls poster="thumbnail.jpg">
</video>
Live Output Window

Dimensions

Set width and height - the video aspect ratio is maintained:

HTML
<video src="movie.mp4" controls width="640" height="360">
</video>
Live Output Window

Always set both width and height to prevent layout shifts (cumulative layout shift / CLS) when the video loads.

Muted Playback

HTML
<video src="intro.mp4" autoplay muted loop>
</video>
Live Output Window

Common for video backgrounds on landing pages.

JavaScript Events

HTML
<video id="vid" src="movie.mp4" controls></video>

<script>
const vid = document.getElementById("vid");

vid.onplay = () => console.log("Playing");
vid.onpause = () => console.log("Paused");
vid.onvolumechange = () => console.log("Volume:", vid.volume);
vid.onfullscreenchange = () => console.log("Fullscreen:", document.fullscreenElement);

// Custom controls
document.getElementById("playBtn").onclick = () => vid.play();
document.getElementById("pauseBtn").onclick = () => vid.pause();
document.getElementById("seekBar").oninput = (e) => {
vid.currentTime = (e.target.value / 100) * vid.duration;
};
</script>
Live Output Window

Key video-specific attributes:

Attribute Description
playsinline Play inline on iOS (not fullscreen)
disablePictureInPicture Prevent picture-in-picture
preload none, metadata (default), auto - how much to preload

Detailed Table with Tag, attributes and attribute values

Tag Attribute Values Description
<audio> src URL Audio file source
<audio> controls (boolean) Show browser controls
<audio> autoplay (boolean) Start playing automatically
<audio> loop (boolean) Repeat after finishing
<audio> muted (boolean) Start with volume off
<audio> preload none, metadata, auto Preload behaviour
<video> src URL Video file source
<video> controls (boolean) Show browser controls
<video> autoplay (boolean) Start playing automatically
<video> loop (boolean) Repeat after finishing
<video> muted (boolean) Start with volume off
<video> poster URL Thumbnail before play
<video> width pixels Display width
<video> height pixels Display height
<video> preload none, metadata, auto Preload behaviour
<video> playsinline (boolean) Inline playback on mobile
<video> disablePictureInPicture (boolean) Disable PiP
<source> src URL Media file source (inside audio/video)
<source> type MIME type Format hint
<track> src URL WebVTT subtitle file
<track> kind subtitles, captions, descriptions, chapters, metadata Track type
<track> srclang language code Track language
<track> label text Track label shown to user
<track> default (boolean) Default track
Courses