Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Created November 21, 2012 02:11
Show Gist options
  • Save jakejscott/4122625 to your computer and use it in GitHub Desktop.
Save jakejscott/4122625 to your computer and use it in GitHub Desktop.
Fluid Ratio images
// credit Rolf Timmermans
// http://voormedia.com/blog/2012/11/responsive-background-images-with-fixed-or-fluid-aspect-ratios
/* Calculate fluid ratio based on two dimensions (width/height) */
@mixin fluid-ratio($large-size, $small-size) {
$width-large: nth($large-size, 1);
$width-small: nth($small-size, 1);
$height-large: nth($large-size, 2);
$height-small: nth($small-size, 2);
$slope: ($height-large - $height-small) / ($width-large - $width-small);
$height: $height-small - $width-small * $slope;
padding-top: $slope * 100%;
height: $height;
background-size: cover;
-moz-background-size: cover; /* Firefox 3.6 */
background-position: center; /* Internet Explorer 7/8 */
}
figure.fluidratio {
/* This element will have fluid ratio from 4:1 at 800px to 2:1 at 300px. */
@include fluid-ratio(800px 200px, 300px 150px);
background-image: url(http://voormedia.com/examples/amsterdam.jpg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment