Skip to content

Instantly share code, notes, and snippets.

@stiplady
Last active February 21, 2020 12:09
Show Gist options
  • Select an option

  • Save stiplady/8c38f0c4e6ff128bd32bf170bce2a6b6 to your computer and use it in GitHub Desktop.

Select an option

Save stiplady/8c38f0c4e6ff128bd32bf170bce2a6b6 to your computer and use it in GitHub Desktop.
Responsive Youtube video iframes
.videoWrapper {
position: relative;
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
// Find all YouTube videos
jQuery("iframe[src*='//www.youtube.com']")
.wrap('<div class="videoWrapper" />')
.each(function() {
// Figure out and save aspect ratio for each video
jQuery(this)
.parent()
.css('padding-bottom', (this.height / this.width * 100)+'%');
jQuery(this)
// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment