Set youtube video on background

I found good solution here.

.video-background {
  background: #000;
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: -99;
}
.video-foreground,
.video-background iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

Using Youtube API + CSS, we can build full screen youtube video background.

HTML Markup:

<div class="page">
  <div class="video-bg">
    <div id="videoPlayer" data-videoid="b0r_dH7jfOM"></div>
  </div>
  <div class="content">
    <h1>Video Player background</h1>
    <h3>Full screen youtube video player in background.</h3>
  </div>
</div>

CSS:

.page {
 position: relative;
 overflow: hidden;
}

.page .video-bg {
 width: 100%;
 height: 0;
 padding-bottom: 56.25%;/* Aspect ratio */
 position: absolute;
}

.page .video-bg iframe {
 border: 0;
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}

.page .content {
 position: relative;
 z-index: 1;
}

Add Youtube API: https://www.youtube.com/iframe_api

JS:

jQuery(function () {

// Youtube player
window.videoPlayer;

window.onYouTubeIframeAPIReady = function () {
 var videoPlayerId = $('#videoPlayer').attr('data-videoid');
 window.videoPlayer = new YT.Player('videoPlayer', {
  height: '1080',
  width: '1920',
  videoId: videoPlayerId,
  playerVars: {
   'controls': 0,
   'autoplay': 1,
   'mute': 1,
   'loop': 1,
   'showinfo': 0,
   'modestbranding': 1
  },
  events: {
   'onReady': onVideoPlayerReady,
   'onStateChange': onVideoPlayerReady
  }
 });
}

function onVideoPlayerReady(event) {
 videoPlayer.playVideo();
}
});

OH HI EVERYONE, here is a reusable simple jQuery plugin I made based on YouTube Embed API. It's also super simple to use. You can see it in action here.

<div id="ytbg" data-youtube="https://www.youtube.com/watch?v=eEpEeyqGlxA"></div>

<script type="text/javascript">
    jQuery(document).ready(function() {
        $('[data-youtube]').youtube_background();
    });
</script>

Code on GitHub


In your body just add those lines.Change the id number.

 <div style="position: fixed; z-index: -99; width: 100%; height: 100%">
      <iframe frameborder="0" height="100%" width="100%" 
        src="https://youtube.com/embed/ID?autoplay=1&controls=0&showinfo=0&autohide=1">
      </iframe>
 </div>