HTML

Introduction to Web and HTML

How the Web Works What is HTML Why Learn HTML

Embedding Video (<video>)

Embedding Video using <video>

HTML provides the <video> element for embedding video content directly into web pages. Whether you want to showcase videos, film trailers, or any other video content, this element allows you to enhance your website with multimedia.

Basic Usage

To embed video in your web page, use the <video> element and specify the video source file using the src attribute. Here's an example:

<video controls width="640" height="360">
  <source src="video.mp4" type="video/mp4">
  Your browser does not support the video element.
</video>

In this example, the controls attribute adds video player controls such as play, pause, and volume, allowing users to interact with the video.

The <source> element within <video> is used to define the video file source. Like with audio, you can specify multiple <source> elements with different file formats (e.g., MP4, WebM) to ensure compatibility with various browsers.

Adding Text Content

The text content within the <video> element, in this case, "Your browser does not support the video element," serves as a fallback for browsers that don't support video playback. Users of those browsers will see this message.

Styling the Video Player

You can style the video player using CSS to match your website's design. For example, you can change the player's color, size, or position on the page.

/* Example CSS for styling the video player */
video {
  width: 100%;
  background-color: #f2f2f2;
  border: 1px solid #ccc;
}

Controlling Playback

With JavaScript, you can further control the video playback. For instance, you can automatically start playing the video when the page loads or create custom play/pause buttons.

<video id="myVideo" controls width="640" height="360">
  <source src="video.mp4" type="video/mp4">
  Your browser does not support the video element.
</video>

<button onclick="playVideo()">Play</button>
<button onclick="pauseVideo()">Pause</button>

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

  function playVideo() {
    video.play();
  }

  function pauseVideo() {
    video.pause();
  }
</script>

In this example, JavaScript functions control video playback with play and pause buttons.

Accessibility

To ensure accessibility, provide text descriptions or transcriptions for video content, especially if it contains important information. Screen readers can convey this information to users who are visually impaired.

Multiple Video Tracks

The <video> element can handle multiple video tracks, such as different video quality options. Use the track element to specify additional tracks.

<video controls width="640" height="360">
  <source src="video.mp4" type="video/mp4">
  <track src="video-en.vtt" kind="subtitles" srclang="en" label="English">
  <track src="video-es.vtt" kind="subtitles" srclang="es" label="Spanish">
  Your browser does not support the video element.
</video>

In this example, we include English and Spanish subtitles as tracks.

In summary, the <video> element allows you to embed video content in your web pages with ease. You can control video playback, style the player, and ensure accessibility for a rich multimedia experience.