Skip to content

Instantly share code, notes, and snippets.

@iftee
Last active January 23, 2017 12:26
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 iftee/a788d2c837d952744902f651d18e6051 to your computer and use it in GitHub Desktop.
Save iftee/a788d2c837d952744902f651d18e6051 to your computer and use it in GitHub Desktop.
Responsive YouTube embed for WordPress
( function( $ ) {
'use strict';
// We'll assume that the post content (and the video) will be inside a wrapper with class .post-content
// and the video aspect ratio is 16:9
$( window ).on( 'load resize', function() {
// Take the width of the wrapper
var contentWidth = $( '.post-content' ).width();
// Resize all YouTube iframes according to the wrapper size with 16:9 aspect ratio
$( '.post-content iframe[src*="https://www.youtube.com"]' ).each( function() {
$( this ).width( contentWidth );
$( this ).height( contentWidth * 9 / 16 );
} );
} );
} ) ( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment