Skip to content

Instantly share code, notes, and snippets.

@nicooprat
Created February 21, 2013 13:38
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 nicooprat/5004783 to your computer and use it in GitHub Desktop.
Save nicooprat/5004783 to your computer and use it in GitHub Desktop.
Automatic responsive iframes with JS + CSS.
/* CSS code */
.responsive-iframe {
position: relative;
padding-bottom: 0; // Will be overriden by Javascript
height: 0;
overflow: hidden;
margin-bottom: 20px;
}
.responsive-iframe iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
// jQuery code
$('iframe').each(function()
{
var w = $(this).width(),
h = $(this).height(),
ratio = h/w*100;
$(this)
.wrap('<div class="responsive-iframe">')
.parent()
.css('padding-bottom',ratio+'%');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment