Created
February 21, 2013 13:38
-
-
Save nicooprat/5004783 to your computer and use it in GitHub Desktop.
Automatic responsive iframes with JS + CSS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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