Skip to content

Instantly share code, notes, and snippets.

@rocktronica
Created May 8, 2017 18:34
Show Gist options
  • Save rocktronica/e6ef063c3684f433f782f8cc19b961db to your computer and use it in GitHub Desktop.
Save rocktronica/e6ef063c3684f433f782f8cc19b961db to your computer and use it in GitHub Desktop.
Responsive iFrames
$.fn.ready(function(){
$("iframe").each(function() {
var $iframe = $(this),
width = $iframe.attr("width") || $iframe.width(),
height = $iframe.attr("height") || $iframe.height();
var $wrapper = $("<div></div>")
.css({
"position": "relative",
"height": "0",
"overflow": "hidden",
"padding-bottom": (height / width * 100) + "%",
});
$iframe
.removeAttr("width")
.removeAttr("height")
.css({
"position": "absolute",
"top": "0",
"left": "0",
"width": "100%",
"height": "100%",
})
.wrap($wrapper);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment