Skip to content

Instantly share code, notes, and snippets.

@andrewlaskey
Created September 30, 2013 18:14
Show Gist options
  • Save andrewlaskey/6767821 to your computer and use it in GitHub Desktop.
Save andrewlaskey/6767821 to your computer and use it in GitHub Desktop.
Dynamically set a background image to a div that will keep its aspect ratio when the div resizes.
<div class="Item" data-stretch="images/some_image.png">
$('[data-stretch]').each(function(i) {
$(this).css('background-image', 'url(' + $(this).attr('data-stretch') + ')');
});
@mixin stretch-background {
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
.Item {
@include stretch-background;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment