H.dev

Video & Audio Tags

Hero

HTML Video & Audio Tags Learn how to natively embed rich media directly into your web pages without relying on third-party plugins, using the incredible HTML5 <video> and <audio> tags. Learning Outcomes: Master embedding media, configuring playback controls, and providing fallback sources. Prerequisites: Understanding of HTML attributes and basic tags. Reading Time: 4 min read Difficulty: Beginner

Introduction

In the dark ages of the web, if you wanted to play a video or audio file on your website, you had to force users to download clunky, insecure plugins like Adobe Flash. Thankfully, HTML5 changed the internet forever by introducing native <video> and <audio> tags. Now, playing media is as easy as placing an image on your site!

Why This Matters

As you scale Learn with Himanshu, you might want to start uploading your own coding tutorial videos or hosting a developer podcast directly on your platform. By mastering these tags, you can host your own media and keep full control over the user experience, rather than sending your audience away to YouTube or Spotify.

Core Concepts

Both the <video> and <audio> tags work very similarly. They act as containers for media files.

Key attributes you need to know:

  • src: The path to your media file.
  • controls: A boolean attribute (meaning you just write the word controls). It tells the browser to display play, pause, and volume buttons.
  • autoplay: Automatically starts playing the media when the page loads (Note: browsers usually block this unless the media is also muted).
  • loop: Makes the media start over automatically when it finishes.
  • muted: Starts the media with the volume turned off.

Syntax

You can provide the source directly in the main tag, or use nested <source> tags to offer multiple formats, letting the browser choose the best one!

<!-- Simple Audio -->
<audio src="podcast.mp3" controls></audio>

<!-- Robust Video with Fallbacks -->
<video width="600" controls>
  <source src="tutorial.mp4" type="video/mp4">
  <source src="tutorial.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

Real World Example

Let's build a clean video player for Himanshu Dubey's latest Next.js masterclass.

<section class="video-lesson">
  <h2>Next.js Routing Masterclass</h2>
  
  <video width="800" height="450" controls poster="/images/nextjs-thumbnail.jpg">
    <source src="/media/nextjs-routing.mp4" type="video/mp4">
    <!-- The text below ONLY shows if the user has an incredibly old browser -->
    <p>Sorry, your browser doesn't support HTML5 video. Please upgrade your browser to watch Himubey's tutorials!</p>
  </video>
</section>

(Notice the poster attribute? It acts like a YouTube thumbnail—showing an image before the user hits play!)

Best Practices

  • Always Include controls: Unless you are building a custom JavaScript video player, always include the controls attribute so the user can actually pause your video!
  • Never Autoplay Sound: Autoplaying a loud video when someone opens your website is the fastest way to make them close the tab. If you must autoplay a background video, always add the muted attribute.

Common Mistakes

Mistake: Forgetting the controls attribute on an audio tag.

<!-- WRONG: The audio is playing, but the user can't see anything to pause it! -->
<audio src="song.mp3" autoplay></audio>

Correct Approach:

<!-- CORRECT: The browser will render a nice audio player UI -->
<audio src="song.mp3" controls></audio>

💡 Himubey Tip

When offering multiple <source> files, always put the most modern/efficient format (like .webm) first, and older formats (like .mp4) second. The browser reads them from top to bottom and plays the very first one it supports, saving you precious bandwidth!

Interview Questions

  1. Why were the <video> and <audio> tags considered revolutionary when HTML5 was released?
  2. What does the poster attribute do on a <video> tag?
  3. If you want a video to play automatically in the background of a landing page, which two attributes must you combine to ensure modern browsers allow it?

Mini Challenge

Task: Build a Podcast Player.

  1. Create an <audio> tag.
  2. Ensure the browser's default play buttons are visible.
  3. Use the nested <source> tag to link to an audio file (you can use a fake path like my-podcast.mp3).
  4. Add a fallback message for older browsers!

Summary

HTML5 made embedding rich media incredibly simple and secure. By using the <video> and <audio> tags alongside attributes like controls, poster, and muted, you can deliver a premium, native media experience directly on your own website.

Next Lesson

Next Lesson: SVG in HTML

Design & Developed by Himanshu Dubey © 2026