Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created June 21, 2016 08:27
Show Gist options
  • Save khoipro/d3c86aa80df8c2bbb2273089cdf712d9 to your computer and use it in GitHub Desktop.
Save khoipro/d3c86aa80df8c2bbb2273089cdf712d9 to your computer and use it in GitHub Desktop.
Mixin to create a background image with fixed ratio (like square) in all devices
@mixin image-aspect($ratio) {
position: relative;
height: auto;
display: block;
&:before {
display: block;
content: "";
width: 100%;
padding-top: percentage(1/$ratio);
}
> a,
> .bg-image {
background-size: cover;
position: absolute;
height: auto !important;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
}
// Usage: h - image height; w - image width
.bg-image {
@include image-aspect(h/w);
}
// HTML
// <div class="gallery">
// <div class="item">
// <div class="bg-image" style="background-image: url('');"></div>
// </div>
// </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment