HTML Media (Audio & Video)

Introduction

HTML media elements allow you to add audio and video content to your web pages. This helps make websites more interactive and engaging for users.

Media elements are commonly used for music players, tutorials, video streaming, and presentations.

In this lesson, you will learn how to add audio and video in HTML using built-in tags.

What You’ll Learn

  • HTML <audio> tag
  • HTML <video> tag
  • Media attributes
  • Supported formats
  • Best practices

HTML Audio Element

The <audio> tag is used to add sound or music to a webpage.

Basic Audio Example

<audio controls>
  <source src="audio.mp3" type="audio/mpeg">
  Your browser does not support audio.
</audio>

Attributes:

  • controls → Play, pause, volume
  • autoplay → Plays automatically
  • loop → Repeats audio
  • muted → Starts muted
  • Fallback text → Shown if browser doesn’t support audio

Audio Formats

Common supported formats:

  • MP3
  • WAV
  • OGG

👉 MP3 is most widely supported across browsers.

HTML Video Element

The <video> tag is used to play video files.

Basic Video Example

<video width="400" controls>
  <source src="video.mp4" type="video/mp4">
  Your browser does not support video.
</video>

Common Attributes:

<video width="400" controls poster="thumb.jpg">

Video Formats

Common video formats:

  • MP4
  • WebM
  • OGG

👉 MP4 is most commonly used.

Multiple Sources (Best Practice)

You can provide multiple formats for better browser support.

<video controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
</video>

Browser chooses the best supported format.

Embedding External Video

You can also embed videos from platforms like YouTube.

<iframe width="400" height="250"
src="https://www.youtube.com/embed/VIDEO_ID"
title="YouTube video">
</iframe>

👉 This avoids hosting large files on your server.

Important Media Attributes

Controls

Shows play/pause buttons.

<audio controls>

Autoplay

Starts media automatically.

<video autoplay>

⚠️ Use carefully (can annoy users)

Loop

Repeats media continuously.

<audio loop>

Muted

Mutes audio by default.

<video muted>

Poster Attribute

The poster attribute is used to display an image before the video starts playing.

<video width="400" controls poster="thumbnail.jpg">
  <source src="video.mp4" type="video/mp4">
</video>

👉 It works like a thumbnail for videos.

Preload Attribute

The preload attribute tells the browser how to load media files.

<audio controls preload="auto">

Values:

  • auto → Loads entire media
  • metadata → Loads only basic info
  • none → Does not preload

👉 Helps improve performance.

Controls List

You can control which buttons appear in media controls.

<video controls controlsList="nodownload">

👉 Prevents download option in some browsers.

Adding Subtitles

Use the <track> tag to add subtitles to videos.

<video controls>
  <source src="video.mp4" type="video/mp4">
  <track src="subtitles.vtt" kind="subtitles" srclang="en" label="English">
</video>

👉 Improves accessibility and user experience.

Media Optimization

Large media files can slow down your website.

👉 Tips:

  • Compress audio/video files
  • Use modern formats like MP4 and WebM
  • Avoid very large file sizes
  • Use thumbnails for videos

How Media Appears in Browser

  • Audio shows control bar
  • Video shows player with controls
  • Layout depends on browser

Complete Example

<!DOCTYPE html>
<html>
<head>
    <title>HTML Media</title>
</head>
<body><h2>Audio Example</h2>
<audio controls>
  <source src="audio.mp3" type="audio/mpeg">
</audio><h2>Video Example</h2>
<video width="400" controls>
  <source src="video.mp4" type="video/mp4">
</video></body>
</html>

Important Notes

  • Always include controls
  • Provide fallback text
  • Use supported formats
  • Optimize file size for faster loading

Common Mistakes

  • Missing <source> tag
  • Using unsupported formats
  • Overusing autoplay
  • Large file sizes

Best Practices

  • Use MP3 for audio and MP4 for video
  • Provide multiple formats
  • Keep media files optimized
  • Avoid autoplay unless necessary
  • Always add subtitles for better accessibility
  • Use poster for better UI
  • Avoid autoplay with sound
  • Use external hosting for heavy videos

Real-Life Uses

  • Music players
  • Video tutorials
  • Online courses
  • Product demos

Practice Tasks:

  • Task 1: Add an audio file to a webpage.
  • Task 2: Embed a video with controls.
  • Task 3: Use autoplay and loop attributes.
  • Task 4: Add multiple sources to a video.
  • Task 5: Create a page with both audio and video.

FAQs

What is the <audio> tag?

It is used to add sound to a webpage.

What is the <video> tag?

It is used to embed videos.

Which format is best for video?

MP4 is the most widely supported format.

Conclusion

HTML media elements make it easy to add audio and video to your web pages. By using <audio> and <video> tags along with attributes like controls, autoplay, poster, and <track>, you can create rich and interactive user experiences.

Media is widely used in modern websites such as online courses, product demos, and entertainment platforms.

👉 With this, you have completed the HTML basics. Now you are ready to move to CSS to design and style your web pages.

Related Tutorials

  • HTML Introduction

    👋 First time learning web development?We recommend starting with our Start Here guide before continuing this lesson. It will help you understand the learning path for HTML, CSS, PHP, and WordPress. Introduction HTML stands for HyperText Markup Language. It is the basic building block of every website on the internet. HTML is used to create…

  • HTML Basic Structure

    Introduction This basic HTML page structure is also known as an HTML skeleton or HTML boilerplate template. In this lesson, you will learn the basic structure of an HTML document and understand what each part does. What You’ll Learn in This Lesson Basic HTML Document Structure (HTML Boilerplate) The basic HTML structure is sometimes called…

  • HTML Headings & Paragraphs

    Introduction Headings and paragraphs are among the most commonly used elements in HTML. They help structure the content and make web pages readable and well-organized. Headings are used to define titles and sections of a webpage, while paragraphs are used to display blocks of text. By using headings and paragraphs properly, developers can organize information…

  • HTML Text Formatting

    Introduction HTML provides several tags that allow developers to format text on a webpage. Text formatting helps highlight important words, emphasize content, and improve the readability of the page. Using text formatting elements, you can make text bold, italic, underlined, highlighted, smaller, or emphasized. These formatting tags help organize information and make the content easier…

  • HTML Links

    Introduction HTML links are used to connect one page to another page. They allow users to move between different web pages, websites, or sections of the same page. Links are a fundamental part of the web. Without links, browsing the internet would not be possible. They also play an important role in SEO by helping…

  • HTML Images

    Introduction Images are an important part of any webpage. They make content more attractive, engaging, and easier to understand. In HTML, images are used to display pictures such as photos, icons, logos, and graphics on a webpage. Adding images helps improve user experience and also makes your content visually appealing. In this lesson, you will…

Leave a Reply

Your email address will not be published. Required fields are marked *