Skip to content

Instantly share code, notes, and snippets.

@grahamlicence
Forked from aarongustafson/responsive-iframes.css
Last active January 4, 2016 00:29
Show Gist options
  • Save grahamlicence/8542442 to your computer and use it in GitHub Desktop.
Save grahamlicence/8542442 to your computer and use it in GitHub Desktop.
iframe {
max-width: 100%;
}
<!-- All you need is a defined width & height -->
<iframe frameborder="0" height="900" width="1600" src="http://example.com/"></iframe>
var iframe = {
adjustIframes: function() {
iframe.$iframe.each(function() {
var $this = $(this),
proportion = $this.data( 'proportion' ),
w = $this.attr('width'),
actual_w = $this.width();
if ( ! proportion ) {
proportion = $this.attr('height') / w;
$this.data( 'proportion', proportion );
}
if ( actual_w != w ) {
$this.css( 'height', Math.round( actual_w * proportion ) + 'px' );
}
});
},
init: function() {
this.$iframe = $('iframe');
if (this.$iframe.length) {
this.adjustIframes();
$(window).on('resize load', iframe.adjustIframes);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment