HTML - Media Tags

Code SubtleCode Subtle
4 min read

1. Understanding Audio and Video Tags

HTML5 introduced native support for audio and video content through the <audio> and <video> elements. These tags eliminate the need for third-party plugins like Flash and provide built-in browser support for multimedia content.

Audio Tag

The <audio> element is used to embed sound content in documents.

<!-- Basic Audio Player -->
<audio src="music.mp3" controls>
    Your browser does not support the audio element.
</audio>

<!-- Audio with Multiple Sources -->
<audio controls>
    <source src="music.ogg" type="audio/ogg">
    <source src="music.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
</audio>

<!-- Background Music (Autoplay & Loop) -->
<audio autoplay loop muted>
    <source src="background.mp3" type="audio/mpeg">
</audio>

Video Tag

The <video> element is used to embed video content in documents.

<!-- Basic Video Player -->
<video src="movie.mp4" width="640" height="360" controls>
    Your browser does not support the video element.
</video>

<!-- Video with Multiple Sources -->
<video width="640" height="360" controls>
    <source src="movie.mp4" type="video/mp4">
    <source src="movie.webm" type="video/webm">
    Your browser does not support the video element.
</video>

<!-- Background Video -->
<video autoplay loop muted playsinline>
    <source src="background.mp4" type="video/mp4">
</video>

Tips for Audio/Video Tags:

  • Always provide fallback content within the tags

  • Use multiple source formats for broader browser support

  • Consider bandwidth and loading times

  • Implement proper controls for user interaction

  • Be mindful of autoplay policies in different browsers

2. Attributes of Media Tags

Media elements support various attributes that control their behavior and appearance.

Common Media Attributes:

<!-- Essential Attributes -->
src="media.mp4"          <!-- Source file path -->
width="640"             <!-- Width in pixels -->
height="360"            <!-- Height in pixels -->
alt="Description"       <!-- Alternative text (for images) -->
controls               <!-- Show player controls -->
autoplay               <!-- Start playing automatically -->
muted                  <!-- Start with no sound -->
loop                   <!-- Repeat media -->
preload="auto"         <!-- Loading behavior -->
poster="thumb.jpg"     <!-- Thumbnail for video -->

Examples with Different Attribute Combinations:

  1. Professional Video Player:
<video
    width="1280"
    height="720"
    controls
    preload="metadata"
    poster="thumbnail.jpg">
    <source src="video.mp4" type="video/mp4">
    <source src="video.webm" type="video/webm">
    <p>Your browser doesn't support HTML5 video.</p>
</video>
  1. Background Audio Player:
<audio
    autoplay
    loop
    muted
    preload="auto">
    <source src="background.mp3" type="audio/mpeg">
    <source src="background.ogg" type="audio/ogg">
</audio>
  1. Responsive Video Player:
<video
    width="100%"
    height="auto"
    controls
    controlsList="nodownload"
    playsinline>
    <source src="video.mp4" type="video/mp4">
</video>

Tips for Media Attributes:

  • Use controls for user interaction

  • Combine autoplay with muted for better browser support

  • Set appropriate dimensions for videos

  • Use preload wisely to manage bandwidth

  • Include poster for better user experience

3. Using Source Element for Alternative Media Files

The <source> element allows you to specify multiple media resources for audio and video elements, providing fallback options for different browser support.

Source Element Structure:

<video controls>
    <source src="video.webm" type="video/webm">
    <source src="video.mp4" type="video/mp4">
    <source src="video.ogv" type="video/ogg">
    <p>Your browser doesn't support HTML5 video.</p>
</video>

Examples:

  1. Comprehensive Audio Support:
<audio controls>
    <source src="audio.opus" type="audio/opus">
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    <source src="audio.wav" type="audio/wav">
    <p>Your browser doesn't support HTML5 audio.</p>
</audio>
  1. Video with Multiple Resolutions:
<video controls>
    <source src="video-hd.mp4" type="video/mp4" media="(min-width: 1080px)">
    <source src="video-sd.mp4" type="video/mp4" media="(min-width: 720px)">
    <source src="video-mobile.mp4" type="video/mp4">
</video>

Tips for Source Elements:

  • Order sources by preference (most preferred first)

  • Include all major formats for maximum compatibility

  • Specify correct MIME types

  • Provide appropriate fallback content

  • Consider using media queries for responsive video

4. Understanding Concept of Using iframe

The <iframe> (Inline Frame) element allows you to embed another document within the current HTML document. It's commonly used for embedding videos, maps, and other external content.

Basic iframe Structure:

<iframe
    src="URL"
    width="width"
    height="height"
    frameborder="0"
    allowfullscreen>
</iframe>

Common Use Cases:

  1. YouTube Video Embedding:
<iframe
    width="560"
    height="315"
    src="https://www.youtube.com/embed/VIDEO_ID"
    frameborder="0"
    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
    allowfullscreen>
</iframe>
  1. Google Maps Embedding:
<iframe
    width="600"
    height="450"
    src="https://www.google.com/maps/embed?pb=MAP_URL"
    style="border:0"
    allowfullscreen=""
    loading="lazy">
</iframe>
  1. Responsive iframe:
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
    <iframe
        style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
        src="https://www.youtube.com/embed/VIDEO_ID"
        frameborder="0"
        allowfullscreen>
    </iframe>
</div>

Security Considerations:

<iframe
    src="trusted-source.com"
    sandbox="allow-same-origin allow-scripts"
    referrerpolicy="no-referrer"
    loading="lazy">
</iframe>

Tips for Using iframes:

  • Always use HTTPS sources when available

  • Implement proper security attributes

  • Consider using the sandbox attribute

  • Make iframes responsive for different screen sizes

  • Use loading="lazy" for better performance

  • Be cautious with third-party content

  • Consider accessibility implications

  • Use appropriate dimensions for the content

14
Subscribe to my newsletter

Read articles from Code Subtle directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Code Subtle
Code Subtle

At Code Subtle, we empower aspiring web developers through personalized mentorship and engaging learning resources. Our community bridges the gap between theory and practice, guiding students from basics to advanced concepts. We offer expert mentorship and write interactive, user-friendly articles on all aspects of web development. Join us to learn, grow, and build your future in tech!