Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Last active October 13, 2015 00:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2ndkauboy/4110612 to your computer and use it in GitHub Desktop.
Save 2ndkauboy/4110612 to your computer and use it in GitHub Desktop.
A simple mixin that takes a target size and adds a min-width media query and a IE fallback given by a variable for the content.
@mixin respond-to-ie($size){
@if $old-ie {
@content;
} @else {
@media all and (min-width: $size) {
@content;
}
}
}
@2ndkauboy
Copy link
Author

Usage

First you have to declare some variables. They might look like this:

$breakTiny: 320px;
$breakSmall: 480px;
$breakMedium: 768px;
$breakLarge: 960px;

Main file styles.scss

Than after importing the mixin to your scss file, you can use it like in this example:

$old-ie: false !default;
@import "respond-to-ie";

@include respond-to-ie($breakMedium){
    body {
        margin: 0;
    }
}

This will produce the following CSS code in your styles.css file:

@media only screen and (min-width: 768px) {
    body {
        margin: 0;
    }
}

Secondary file for old IE old_ie.scss

$old-ie: true;
@import styles.scss

This will create a second file old_ie.css with the following content:

body {
    margin: 0;
}

So it's basically the same content but without the media query.

Including the files into your HTML

You simply a conditional comment to include those two files and every browser get's only one of the files with only those properties, that the browser supports:

<!--[if lte IE 8]>
    <link rel="stylesheet" href="old_ie.css" media="all" />
<![endif]-->
<!--[if gt IE 8]><!-->
    <link rel="stylesheet" href="styles.css" media="all" />
<!--<![endif]-->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment