HTML5 Video Player Best Practices

By  on  

Let's all be honest:  when it comes to media and the early days of the internet, we definitely did it all wrong.  We started with embedded video players like RealPlayer and Windows Media Player which required custom codecs and browser plugins, then moved on to Flash and Quicktime -- all of which made our browsers slow and sometimes even at risk from a security perspective.  It took more than a decade to create a <video> tag and actually get browser support for it -- far too long of a wait, so much so that many sites still use Flash to serve their videos.

All that said, the majority of the web is now using <video> and you can tell -- less browser crashes, less memory uxsed, and smoother playback.  Let's have a look at basic and more advanced <video> usage as well as some best practice tips.  We'll take advantage of Cloudinary's HTML Video Player tools and Cloudinary's awesome API for help along the way too!

Basic <video> Usage

Let's look at an example of <video> page usage and follow what's going on:

<video width="640" height="480" poster="sample-video.jpg" controls autoplay preload>
 <source src="sample-video.webm" type="video/webm">
 <source src="sample-video.ogv" type="video/ogg">
 <source src="sample-video.mp4" type="video/mp4">
 <track src="subtitles-en.vtt" kind="subtitles" srclang="en" 
        label="English" default>
 <track src="subtitles-fr.vtt" kind="subtitles" srclang="fr"

Most <video> elements will use the following attributes:

  • width: The width of the <video> element
  • height: The height of the <video> element
  • poster: The representative image of the video, shown while the video loads
  • controls: Whether or not the browser's native controls should be shown (in case you prefer to create your own)
  • preload: Whether or not to preload the video
  • autoplay: Whether or not to autoplay the video

Within the <video> element will typically be:

  • source: Elements that provide the source and source type of video files, ordered by preference of support
  • track: Subtitles for said video (optional, of course)

<video> Methods & Properties:

On the JavaScripts side, we have a few methods to manage the <video> element:

  • load: Reloads the video
  • play: Plays the video
  • pause: Pauses the video

And while there are many events and properties, these are the most used:

  • duration
  • paused
  • currentTime
  • volume

Using Cloudinary for <video>

Cloudinary provides a full upload to deliver API for videos.  Let's start by uploading the video with the Node.js API:

cloudinary.uploader.upload('sample-video.mp4', function(result) {
  // Callback
  console.log('result: ', result);
}, {
  public_id: 'sample-video',
  resource_type: 'video'

Once the video has been uploaded, Cloudinary generates WEBM, MP4, and OGV versions of your video for optimal delivery, as well as a poster image. With the video up on Cloudinary's server, you can use the following to get the video HTML:'sample-video');

<video poster=''>
    <source src='' type='video/webm'>
    <source src='' type='video/mp4'>
    <source src='' type='video/ogg'>

You can use a second argument to customize the video and poster:'sample-video', {
	width: 640,
	height: 480,
	autoplay: true,
	poster: {
		transformation: [
			{ width: 1850, crop: "scale" },
			{ overlay: "movie_curtain_overlay_new"},
			{ overlay: "text:Courier_80_bold:Once%20upon%20a%20time...", 
			gravity:"north", y: 60 },
			{ start_offset: 36 }

Cloudinary makes implementing a HTML5 video player very easily!

Best Practices and Tips

  • Be careful with the autoplay attribute -- a page mainly created for a video makes sense, but an advertisement or sidebar item?  You'll make visitors upset quickly!
  • Use a CDN -- load speed matters!
  • Offer both standard (MP4) and edge source file types (WEBM) -- you reward users who have a client with advanced compression and quality capabilities (WEBM), but provide a fallback to widely supported formats (MP4s).
  • If you create custom control elements, be sure to make them large enough that they're reasonable tap targets for mobile!

HTML5 video players started out simple but have grown quite advanced (think YouTube), and since it's all just HTML, CSS, and JavaScript, any front-end developer can create an impressive HTML5 video player.  There are probably many more people out there who've pushed HTML5 video to its limits, so please post any further tips or examples of your work!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    I&#8217;m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Fixing sIFR Printing with CSS and MooTools

    While I'm not a huge sIFR advocate I can understand its allure. A customer recently asked us to implement sIFR on their website but I ran into a problem: the sIFR headings wouldn't print because they were Flash objects. Here's how to fix...

  • By
    FileReader API

    As broadband speed continues to get faster, the web continues to be more media-centric.  Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it).  And every social service does...


  1. DJ

    David… thanks. A novice at this, I admit to being quite confused at all the changes for this and “best practices” I’ve read over the past year or so. And, I might be doing it wrong; but, I don’t do anything anymore with “fixed” dimensions. Every time I’ve done it, it breaks something somewhere.

    How can this be used when I’m not building something for some specialized, set in stone display and don’t have the luxury of ignoring the bizarre matrix of size and shapes the industry has boxed us in with? (i.e. responsive) – is it just an easy tweak?

    • Hi DJ,

      The good news is you can make s visually shrink to fit the layout space available to them the exact same way that you do this with elements:

      video { max-width: 100%; }

      The bad news is, there’s no video equivalent of the the responsive img srcset attribute, to help you load scaled-down versions of your video appropriate for the size of your viewers’ screens. However, there is a nice video (and Cloudinary!) feature which allows you to adapt the resolution and bitrate of a video in response to each user’s *connection speed* – something that can’t yet do. It’s a bit of an advanced topic, but check it out:

  2. DJ

    Sorry Dave… I’ve been at this for years and years but only avocationally and the rate the industry has exploded these last two years has me reeling trying to keep up.


  3. Hi David, I want to know if is possible load videos according to media queries, like the tag.


  4. Hi, I want custom features for video player, does anyone recommended me the library or tools so the way I can build a custom plyer with my own features list?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!