← Snippets - Script

Maintain YouTube Aspect Ratio

For keeping YouTube video aspect ratio as the window resizes so there are not black bars on top/bottom of the video. Not entirely sure this is the best way to do this, so feel free to edit/replace with another method if you have one. This seems to work, though.

Requires width/height attributes from the YouTube standard embed tag.

How To - script.js

$(window).on('resize load', function(){
    $('iframe[src*=youtube').each(function(){
        $(this).css('height', $(this).attr('height') / $(this).attr('width') * $(this).outerWidth());
    })
})

Tags: bs2, bs3, bs4, youtube, iframe, embed, js, jquery, javascript