How to Make Cross Browser Gradient Mixin in Compass, with Ie9, Ie8, Ie7 and Opera

CSS (perhaps with Compass): Cross-browser gradient

I just noticed that the current Compass beta (0.11.beta.6) has support for generating IE gradients in the compass/css3/images module (which supersedes the previous gradient module), so you can generate your gradients with a total of two short commands:

@import "compass/css3/images";
@import "compass/utilities/general/hacks"; /* filter-gradient needs this */

.whatever {
/* IE; docs say this should go first (or better, placed in separate IE-only stylesheet): */
@include filter-gradient(#aaaaaa, #eeeeee);
/* Fallback: */
background: #cccccc;
/* CSS 3 plus vendor prefixes: */
@include background(linear-gradient(top, #aaaaaa, #eeeeee));
}

This generates the following slew of CSS:

.whatever {
*zoom: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFAAAAAA', endColorstr='#FFEEEEEE')";
filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFAAAAAA', endColorstr='#FFEEEEEE');
background: #cccccc;
background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #aaaaaa), color-stop(100%, #eeeeee));
background: -moz-linear-gradient(top, #aaaaaa, #eeeeee);
background: -o-linear-gradient(top, #aaaaaa, #eeeeee);
background: linear-gradient(top, #aaaaaa, #eeeeee);
}

I guess I would have preferred to have the IE and non-IE gradient code in one call, but since IE's DXImageTransform gradient function is pretty limited, that is probably not possible.

SASS Compass - gradients compile, but create invalid CSS?

I'm running the SASS plugin under NetBeans and it has its own Sass-watch like feature upon compile, so it is bypassing the compass watch feature. So SASS is generating SASS code within the css file, rather than compass generating CSS code.

Is it possible to use a mixin for browser-specific CSS

The absolute simplest mixin would be like so:

@mixin legacy-ie($ver: 7) {
.ie#{$ver} & {
@content;
}
}

Output:

.baz {
background: #CCC;

@include legacy-ie {
background: black;
}
}

If you wanted to emit styles that work for multiple IE versions at once without duplication, then this would be one way to do it:

$default-legacy-ie: 7 8 9 !default;

@mixin legacy-ie($versions: $default-legacy-ie) {
$sel: ();
@each $v in $versions {
$sel: append($sel, unquote('.ie#{$v} &'), comma);
}

#{$sel} {
@content;
}
}

.foo {
background: red;

@include legacy-ie {
background: green;
}
}

.bar {
background: yellow;

@include legacy-ie(7 8) {
background: orange;
}
}

Output:

.foo {
background: red;
}
.ie7 .foo, .ie8 .foo, .ie9 .foo {
background: green;
}

.bar {
background: yellow;
}
.ie7 .bar, .ie8 .bar {
background: orange;
}

If you want to be able to suppress all of the IE kludges all you need to add is one variable and an @if block:

$enable-legacy-ie: true !default;

@mixin legacy-ie($ver: 7) {
@if $enable-legacy-ie {
.ie#{$ver} & {
@content;
}
}
}

Set $enable-legacy-ie to false at the top of the file you don't want to have the IE specific styles, set it to true if you do want the styles included. You could easily write a reverse mixin to hide styles that old IE can't make use of so that the IE specific file stays nice and small.

Adding !important using a Compass Mixin

UPDATE: new versions of sass support this syntax now:

@include border-radius(5px !important);

Just do this (as noted in @naoufal answer).

--- old answer ---

You can not use !important with compass mixings, but the culprit is not compass, you should blame sass for this.

@include border-radius(5px) !important; #=> SASS Syntax Error


Related Topics



Leave a reply



Submit