Full-screen Responsive HTML5 Video Background for WordPress using jQuery and CoverVid


HTML5 full-screen video isn't nearly groundbreaking, nor challenging, especially with the help of the CoverVid library from Stefan Erickson. What made my particular situation unique was the need to load completely different videos for each device resolution breakpoint and page template combination in WordPress. With this requirement in mind, I ran through some development options and disregarded a couple approaches mainly because of performance or configurability reasons.

Ultimately, I decided to that Javascript would be the solution to dynamically load the correct video based on the page and device.

Determine your breakpoints

The first piece of puzzle is pulling your media query breakpoints from your theme, so you can target each device resolution. It's up to you how granular you need your breakpoints rules to be. In the following example, I'm only using the window width to sniff out which video I should be loading, but you could easily use the window's height or user agent to target specific platforms like iOS and Android.

    var getDevice = function() {
      if($(window).width() <= 480) {
        return {
          name: 'mobile-portrait',
          videoHeight: 800,
          videoWidth: 400
      if($(window).width() <= 900) {
        return {
          name: 'tablet-portrait',
          videoHeight: 1100,
          videoWidth: 800
      if($(window).width() <= 1100) {
        return {
          name: 'tablet-landscape',
          videoHeight: 800,
          videoWidth: 1100

      return {
        name: 'desktop',
        videoHeight: 1150,
        videoWidth: 1650

In the snippet above, I'm also returning the native height and width of the video produced for each resolution. CoverVid requires those dimensions when configuring the full-screen layout.

Now for the guts of it

When the page loads, we need to figure out what video we need to load into the background. To do this, I chose to use target the page template CSS class WordPress automatically adds to the body tag.

        if($('body').hasClass('home')) {
          switch(device.name) {
            case "mobile-portrait":
              videoSrc = {
                'poster': '/wp-content/themes/your-theme/images/mobile-portrait-home-video-poster.jpg',
                'ogv': '/wp-content/themes/your-theme/videos/400x800/home-video.oggtheora.ogv',
                'webm': '/wp-content/themes/your-theme/videos/400x800/home-video.webmhd.webm',
                'mp4': '/wp-content/themes/your-theme/videos/400x800/home-video.mp4'
            case "tablet-portrait":
              // video paths
            case "tablet-landscape":
              // video paths
            case "desktop":
              // video paths

The code block above looks for a particular page (or pages) and then based on the type of the device, loads videos designed and compiled for that resolution. I also added a path to the "poster" video attribute, which provides the UI a placeholder image as the video downloads.

Build the video HTML and inject it into the DOM

The following snippet builds the HTML5 video element with our dynamically targeted video paths. It appends to a hook in WordPress, which will be explained in the next section.

            '<video id="background-video" class="covervid-video" autoplay loop poster="' + videoSrc.poster + '">' +
              '<source src="' + videoSrc.webm + '" type="video/webm">' +
              '<source src="' + videoSrc.ogv + '" type="video/ogv">' +
              '<source src="' + videoSrc.mp4 + '" type="video/mp4">' +

Finally, once the video is placed, we'll fire up CoverVid to handle the full-screen background layout.

          setTimeout(function() {
            $('.covervid-video').coverVid(device.videoWidth, device.videoHeight);
          }, 1);

Finally, configure your WordPress theme

Add the necessary HTML element hook to your page template:

<div class="covervid-wrapper"></div>

And, last of all, give the CoverVid hook element its necessary styling in your theme's styles.css:

.covervid-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;


Pull down a copy of this demo script wrapped up in a closure.

Stay Connected

Join us on our social networks for all the latest updates, announcements and more.


> Follow Us


> Connect