Created
June 21, 2016 08:27
-
-
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
This file contains hidden or 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
| @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