Skip to content

Instantly share code, notes, and snippets.

@rbndelrio
Last active September 7, 2016 15:42
Show Gist options
  • Save rbndelrio/e20d44b80045a59606e4e0227624dfb0 to your computer and use it in GitHub Desktop.
Save rbndelrio/e20d44b80045a59606e4e0227624dfb0 to your computer and use it in GitHub Desktop.
High-dpi images
@mixin hidpi {
@media only screen and (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2), (min-resolution: 2dppx) {
@content;
}
}
@mixin image2x($name, $extension: 'jpg', $size: auto, $position: null, $repeat: null) {
@if $size != auto {
background-size: $size;
}
@if $repeat != null {
background-repeat: $repeat;
}
@if $position != null {
background-position: $position;
}
background-image: url('../images/#{$name}.#{$extension}');
@include hidpi {
background-image: url('../images/#{$name}@2x.#{$extension}');
}
}
// Usage:
.icon--play {
@include image2x('icon--play', 'png', contain);
}
<!-- Any device that could possibly have a high-density display will support srcset -->
<!-- http://caniuse.com/#feat=srcset -->
<img alt="Important image oh man"
src="/img/some_image.jpg"
srcset="/img/some_image.jpg 1x, /img/some_image@2x.jpg 2x"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment