HTML Video Elements

HTML Video Elements

Master HTML5 video elements with comprehensive examples, custom controls, responsive techniques, and accessibility features for modern web applications.

How do you embed a video in HTML?

Use the <video> element with controls and one or more <source> files: <video controls src="clip.mp4"></video>. Add a poster image, set width and height (or an aspect-ratio) to avoid layout shift, and include captions with <track>. For silent background videos, use autoplay muted loop playsinline.

<video controls playsinline poster="poster.jpg" width="1280" height="720">
  <source src="demo.webm" type="video/webm">
  <source src="demo.mp4" type="video/mp4">
</video>

<!-- Background video -->
<video autoplay muted loop playsinline src="bg.mp4"></video>

HTML Video — step by step

1 / 4
1

Embed video with the video element

Add video with the video element and a src attribute or nested source elements. Use controls to show playback controls. Set width and height to prevent layout shift while the video loads. Add a poster attribute for the thumbnail image.

1 of 4
💡Use playsinline for Mobile Video
On iOS Safari, video elements default to opening in a native fullscreen player. Add the playsinline attribute to keep the video inline within the page layout — essential for background videos, video overlays, and autoplaying banner videos. Without it, mobile users get a jarring fullscreen takeover.

🎯 Quick Check

Which combination of attributes is typically required for autoplay video in modern browsers?

HTML5 Video Revolution

HTML5 video elements transformed web multimedia by providing native video playback without plugins. Modern video implementation requires understanding formats, responsive design, accessibility, and performance optimization.

Format Support

  • MP4: Universal browser support
  • WebM: Modern, efficient compression
  • OGG: Open source alternative

Advanced Features

  • Custom controls: Full player control
  • Subtitles: Track element support
  • Picture-in-Picture: Floating video

Responsive Design

  • Adaptive: Multiple resolutions
  • Mobile-optimized: Touch controls
  • Bandwidth aware: Smart loading

Video Element Basics

Essential Video Attributes

Basic Structure

<video controls width="400" height="300">
  <source src="video.mp4" type="video/mp4">
  Fallback text
</video>

With Poster

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

Essential Attributes

  • controls - Show video controls
  • autoplay - Auto-start playback
  • muted - Start without sound
  • loop - Repeat when finished
  • poster - Thumbnail image
  • preload - Loading strategy
  • width/height - Dimensions
  • playsinline - iOS inline play

Preload Strategies

none

Don't preload anything

preload="none"

metadata

Load duration, dimensions

preload="metadata"

auto

Load entire video

preload="auto"

Complete Video Implementation

Explore comprehensive video examples including custom controls, playlists, subtitles, responsive design, and Picture-in-Picture functionality.

Code Editor

<video controls playsinline width="400" preload="metadata"
       poster="https://picsum.photos/id/1067/400/225">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
  Your browser does not support video.
</video>
<p><button id="speed">Play at 2×</button></p>

Preview

Accessibility & Performance

Accessibility Features

  • Captions: Text for dialogue and sound effects
  • Subtitles: Translations for different languages
  • Audio descriptions: Narration of visual content
  • Keyboard navigation: Full control via keyboard
  • Screen reader support: Proper ARIA labels

WebVTT Subtitles

WEBVTT

00:00:00.000 --> 00:00:03.000
Welcome to our video tutorial

00:00:03.000 --> 00:00:06.000
Let's learn about HTML video

Performance Optimization

  • Format optimization: Use modern codecs (AV1, VP9)
  • Compression: Balance quality and file size
  • CDN delivery: Fast global distribution
  • Adaptive streaming: Quality based on connection
  • Lazy loading: Load videos when needed

Mobile Considerations

  • Avoid autoplay on mobile devices
  • Use playsinline for iOS Safari
  • Optimize for touch interactions
  • Consider data usage costs

Advanced Video Features

JavaScript Video API

  • video.play() - Start playback
  • video.pause() - Pause video
  • video.currentTime - Get/set position
  • video.duration - Video length
  • video.volume - Audio level (0-1)
  • video.playbackRate - Playback speed

Modern Web APIs

  • Picture-in-Picture: Floating video window
  • Fullscreen API: Immersive viewing
  • Media Session: OS media controls
  • Web Codecs API: Direct codec access
  • Media Streams: Real-time video

Video Events

  • loadstart: Loading begins
  • loadedmetadata: Metadata loaded
  • loadeddata: First frame loaded
  • canplay: Enough data to play
  • canplaythrough: Can play without buffering
  • play/pause/ended: Playback events

Responsive Techniques

  • Aspect ratio: Maintain proportions
  • Adaptive quality: Multiple resolutions
  • Bandwidth detection: Connection-aware loading
  • Device optimization: Mobile-specific settings

Video Mastery Complete!

Outstanding! You've mastered HTML5 video elements with comprehensive knowledge of formats, controls, accessibility, and performance optimization. Ready to explore favicon implementation?

HTML Video: summary

Key points

  • playsinline stops iOS from forcing the video into full screen.
  • MP4 (H.264) plays everywhere; WebM (VP9 or AV1) is smaller in browsers that support it.
  • Use video { max-width: 100%; height: auto; } so videos fit small screens.

Frequently asked questions

›How do I make a video responsive?

Give it width: 100%; height: auto;. For iframes, also add aspect-ratio: 16 / 9.