HTML - poster Attribute



HTML poster attribute is used to specify an image/poster(thumbnail) for the video to be displayed until the user clicks the play button.

If this attribute is not specified then nothing is displayed until the first frame is available, then the first frame is shown as a poster frame for the video.

Syntax

<video poster = "posterlocation/url"></video>

Where the poster location can be the location of your local image/poster or any URL.

Applies On

Below listed elements allow using of the HTML placeholder attribute

Element Description
<video> HTML <video> tag is used to render video on webpage.

Examples of HTML poster Attribute

Below examples will illustrate the HTML poster attribute, where and how we should use this attribute!

Set poster for a video

In the following example, we are using the HTML 'poster' attribute within the video element to specify an image/poster to the video until the user does not click on the play button.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'poster' Attribute</title>
   <style>
      video {
         border: 3px solid rgb(86, 3, 3);
      }
   </style>
</head>
<body>
   <h3>Example of the HTML 'poster' Attribute</h3>
   <video poster=
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRF50DmcEaJHWoz35nvXG7nirVaCNVqymt6Xw&s" 
          width="400" 
          height="250" 
          controls>
      <source src=
"https://static.videezy.com/system/resources/previews/000/019/695/original/pointing-pink.mp4"
      >
   </video>
   <p>You can notice that a black logo is displayed before playing video</p>
</body>
</html>

Set poster using script

Let's look at the following example, where we are going to run the script using the poster attribute and also using the separate function to set and remove the poster.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'poster' Attribute</title>
   <style>
      video {
         border: 5px solid rgb(123, 9, 200);
      }

      button {
         padding: 7px;
      }
   </style>
</head>
<body>
   <h3>Example of the HTML 'poster' Attribute</h3>
   <video width="400" height="250" controls id='demo'>
      <source type="video/mp4" 
              src="bgmi.mp4">
   </video>

   <br>
   <button onclick="Add()">
         Set poster
   </button>
   <button onclick="Remove()">
         Remove poster 
   </button>
   <script>
   function Add() {
      document.getElementById('demo').poster = "download.png";
   }

   function Remove() {
      document.getElementById('demo').poster = "";
   }
   </script>
</body>
</html>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
poster Yes 4.0 Yes 9.0 Yes 3.5 Yes 4.0 Yes 10.5
html_attributes_reference.htm
Advertisements