Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="cover">
  <h2>This is a fully responsive & mobile friendly YouTube video background scaled with 16/9 aspect ratio.</h2>
  <p>To make this compatible with a CMS, you can pass data attributes into the video wrapper. This version only supports one video. If you'd like to see how this works with an array of videos, check out the <a href="https://codepen.io/ccrch/pen/GgPLVW/" target="_blank">version I originally forked</a>.</p>
  <p>It's important to note that this will not work on mobile devices, and there is no way to make it work. Why? Because mobile devices do not permit background video that is controlled by JS. I'm assuming this has something to do with bandwidth usage that cannot be controlled by the mobile device owner?</p>
</div>

<div class="video-wrapper" data-video-id="ORuz_i59Dfs" data-video-youtube-link="y" data-video-start="75" data-video-end="140" data-video-width-add="100" data-video-height-add="100" style="height: 800px;">
  <div class="video-overlay" style="background-color: #000; opacity: .70"></div>
					  <div class="tv">
						    <div class="screen mute" id="tv"></div>
					  </div>
				</div>
              
            
!

CSS

              
                @import "bourbon";

a {
  color: #cadecb;
}
.video-wrapper {
	position: relative;
	width: 100%;
	margin: rem-calc(0 0 54.5);
	min-height: rem(500);
	overflow: hidden;
}
.video-overlay {
	height: 100%;
	opacity: .8;
	position: absolute;
	width: 100%;
	z-index: 2;
}
.video-expand {
	color: white;
	font-size: rem(18);
	line-height: 1em;
	opacity: .4;
	position: absolute;
	top: rem(15);
	right: rem(15);
	z-index: 3;
	&:hover {
		@include transition(all 400ms ease-in-out);
	}
}
.cover {
  font-family: 'Helvetica Neue', Helvetica, sans-serif;
	position: absolute;
  margin: auto;
	top: 10%;
	left: 0;
  right: 0;
  bottom: 0;
	z-index: 100;
	width: 50%;
  text-align: center;
  h2 {
    font-size: 36px;
    font-weight: 100;
    color: #fff;
    line-height: 1.3em;
  }
  p {
    font-size: 18px;
    font-weight: 300;
    color: #fff;
  }
}
.tv {
	position: absolute;
	top: 0;
	left: 0;
	z-index: 1;
	width: 100%;
	height: 100%;
	overflow: hidden;
	.screen {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 1;
		margin: auto;
		opacity: 0;
		transition: opacity .5s;
		&.active {
			opacity: 1;
		}
	}
}
              
            
!

JS

              
                // Grab data attributes from video-wrapper
var videoID = $(".video-wrapper").data("video-id");
var videoYouTubeLink = $(".video-wrapper").data("video-youtube-link");
var videoStart = $(".video-wrapper").data("video-start");
var videoEnd = $(".video-wrapper").data("video-end");
var videoWidthAdd = $(".video-wrapper").data("video-width-add");
var videoHeightAdd = $(".video-wrapper").data("video-height-add");

// Create video script element
var tag = document.createElement('script');
  tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Prepend expander if data attr is yes
if (videoYouTubeLink === 'y') {
  $(".video-wrapper").prepend('<a href="https://www.youtube.com/watch?v=' + videoID + '" class="video-expand" target="_blank">View on Youtube</a>');
}

// Setup the Youtube TV with player defaults
var tv,
  playerDefaults = {autoplay: 0, autohide: 1, modestbranding: 1, rel: 0, showinfo: 0, controls: 0, disablekb: 1, enablejsapi: 0, iv_load_policy: 3};
var vid = {'videoId': videoID, 'startSeconds': videoStart, 'endSeconds': videoEnd, 'suggestedQuality': 'hd720'};

function onYouTubePlayerAPIReady(){
  tv = new YT.Player('tv', {events: {'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange}, playerVars: playerDefaults});
}

function onPlayerReady(){
  tv.loadVideoById(vid);
  tv.mute();
}

function onPlayerStateChange(e) {
  if (e.data === 1){
    $('#tv').addClass('active');
  } else if (e.data === 0){
    tv.seekTo(vid.startSeconds)
  }
}

function vidRescale(){
  var w = $(window).width() + videoWidthAdd,
    h = $(window).height() + videoHeightAdd;
  if (w/h > 16/9){
    tv.setSize(w, w/16*9);
    $('.tv .screen').css({'left': '0px'});
  } else {
    tv.setSize(h/9*16, h);
    $('.tv .screen').css({'left': -($('.tv .screen').outerWidth()-w)/2});
  }
}

$(window).on('load resize', function(){
  vidRescale();
});
              
            
!
999px

Console