Skip to content

Instantly share code, notes, and snippets.

@Matt-Jensen
Last active December 22, 2015 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Matt-Jensen/6395680 to your computer and use it in GitHub Desktop.
Save Matt-Jensen/6395680 to your computer and use it in GitHub Desktop.
A sass function to generate retina media queries.
/* Update: 5/3/14 - supports IE9+, FF3.5+, Opera9.5+ */
@mixin retina($query: null, $density: 2) {
$dpi: $density * 96;
/* Accepts optional @media query conditions */
@if $query {
$query: "and (#{$query})";
}
@else {
$query: "";
}
@media
#{"only screen " + $query + " and (-webkit-min-device-pixel-ratio: " + $density + ")"},
#{"only screen " + $query + " and (min-resolution: " + $dpi + "dpi)"},
#{"only screen " + $query + " and (min-resolution: " + $density + "dppx)"} {
@content;
}
}
/* " */
/*** Basic Usage for targeting only 2dppx / 192dpi devices: ***/
@include retina() {
.your-nifty-selector {
opacity: 1;
background: red
}
}
/*** Including additional @media query conditions with each density condition ***/
@include retina("min-width: 640em", 1.5) {
.more-selector-fun {
color: red;
}
}
/*** Just changing device density query: ***/
@include retina(null, 1.5) {
.more-selector-fun {
color: red;
}
}
/*** Using string interpolation to do cool stuff with Zurb's Foundation Framework: ***/
@include retina("min-width: #{lower-bound($medium-range)}") {
.that-parameter-is-interpolated {
content: 'Holy Smokes!'
}
}
/* Based on code by Brett Jankord */
/* http://www.brettjankord.com/2012/11/28/cross-browser-retinahigh-resolution-media-queries */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment