Skip to content

Instantly share code, notes, and snippets.

@kodie
Created April 12, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodie/2e977ca86357a15ab4f8fd7cf75ae70e to your computer and use it in GitHub Desktop.
Save kodie/2e977ca86357a15ab4f8fd7cf75ae70e to your computer and use it in GitHub Desktop.
Make YouTube video embeds responsive
var responsiveVideos = $('iframe[src*="//www.youtube.com"]');
responsiveVideos.each(function(){
$(this)
.data('aspectRatio', $(this).height() / $(this).width())
.width('100%')
.removeAttr('height');
});
$(window).resize(function(){
responsiveVideos.each(function(){
$(this)
.height($(this).width() * $(this).data('aspectRatio'));
});
}).resize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment