Compass Images
Provides mixins that work across many modern browsers with the latest CSS3 image rendering primitives.
These mixins provide intelligent cross-browser access to properties that accept images or image-like values (e.g. gradients). The syntax is very straightforward, it is exactly like the css syntax that you would use for the corresponding CSS3 properties: Values are comma and space delimited, just as they would be for a property. Vendor prefixes are used only when necessary.
Example (more examples are available by following the links below):
.in-css3 {
background: url(foo.png),
linear-gradient(to bottom right, #333, #0c0),
radial-gradient(#c00, #fff 100px);
}
.with-compass {
@include background(image-url("foo.png"),
linear-gradient(to bottom right, #333, #0c0),
radial-gradient(#c00, #fff 100px));
}
To enable SVG gradient support in Opera and IE9, lower the SVG shim threshold
$svg-gradient-shim-threshold
to 0.1
(or to whatever
threshold you feel is best) in your stylesheet.
NOTE: At this time, Opera renders incorrectly an SVG background on a element
with a border, repeating the gradient towards the end. You can set
background-repeat: no-repeat
to avoid this, but the gradient
will not fill the area completely.
This file can be imported using:
@import "compass/css3/images"
Examples
- Background Gradients
- css3 mixin for css gradients
Imports
- Browser Support – Provides configuration options for the Compass Browser Support Matrix.
- Hacks – Mixins for hacking specific browsers.
Configurable Variables help
$gradient-support-threshold
$graceful-usage-threshold
$svg-gradient-shim-threshold
$graceful-usage-threshold
$border-image-support-threshold
$graceful-usage-threshold
$owg-threshold
$graceful-usage-threshold
$use-legacy-gradient-syntax
false
Compass assumes you will use the official gradient syntax, unless otherwise instructed.
Constants
$browsers-supporting-svg-but-not-gradients
(ie: "9", opera: "9.5-9.6")
These browsers support svg but not gradients so we can shim the gradient with an inline svg file.
$browsers-supporting-old-webkit-gradients
(android: ("2.1", "3"))
These browsers require the old webkit gradient syntax
Functions
view sourcelinear-gradient($angle)
@function linear-gradient($angle, $details...) $legacy-syntax: $use-legacy-gradient-syntax @if type-of($angle) != "number" $angle: compact($angle) $legacy-syntax: if(index($angle, "to"), false, true) @if $legacy-syntax @return _linear-gradient_legacy($angle, $details...) @else @return _linear-gradient($angle, $details...)
@function linear-gradient($angle, $details...) { $legacy-syntax: $use-legacy-gradient-syntax; @if type-of($angle) != "number" { $angle: compact($angle); $legacy-syntax: if(index($angle, "to"), false, true); } @if $legacy-syntax { @return _linear-gradient_legacy($angle, $details...); } @else { @return _linear-gradient($angle, $details...); } }
Create a linear gradient using standard official or legacy syntax. This function must be included in one of the following image module mixins to work properly.
Mixins
view sourceeach-gradient-prefix($values)
=each-gradient-prefix($values) @if prefixed(-svg, $values) +for-legacy-browsers($browsers-supporting-svg-but-not-gradients, $svg-gradient-shim-threshold) +with-prefix(-svg) @content @if prefixed(-owg, $values) +for-legacy-browsers($browsers-supporting-old-webkit-gradients, $owg-threshold) +with-prefix(-owg) @content +with-each-prefix(css-gradients, $gradient-support-threshold) @if $current-prefix @if prefixed($current-prefix, $values) @content @else if $debug-browser-support /* There is not a value that needs to be prefixed with #{$current-prefix} in: #{$values} @else @content
@mixin each-gradient-prefix($values) { @if prefixed(-svg, $values) { @include for-legacy-browsers($browsers-supporting-svg-but-not-gradients, $svg-gradient-shim-threshold) { @include with-prefix(-svg) { @content; } } } @if prefixed(-owg, $values) { @include for-legacy-browsers($browsers-supporting-old-webkit-gradients, $owg-threshold) { @include with-prefix(-owg) { @content; } } } @include with-each-prefix(css-gradients, $gradient-support-threshold) { @if $current-prefix { @if prefixed($current-prefix, $values) { @content; } @else if $debug-browser-support { /* There is not a value that needs to be prefixed with #{$current-prefix} in: #{$values} */ } } @else { @content; } } }
image-property($property)
=image-property($property, $values...) +each-gradient-prefix($values) @if $current-prefix #{$property}: prefix($current-prefix, $values) @else #{$property}: $values @content
@mixin image-property($property, $values...) { @include each-gradient-prefix($values) { @if $current-prefix { #{$property}: prefix($current-prefix, $values); } @else { #{$property}: $values; } @content; } }
background
=background($backgrounds...) +image-property(background, $backgrounds...)
@mixin background($backgrounds...) { @include image-property(background, $backgrounds...); }
Background property support for vendor prefixing within values.
background-with-css2-fallback
=background-with-css2-fallback($backgrounds...) @if length($backgrounds) > 1 or prefixed(-css2, $backgrounds) background: -css2(nth($backgrounds, -1)) +background($backgrounds...)
@mixin background-with-css2-fallback($backgrounds...) { @if length($backgrounds) > 1 or prefixed(-css2, $backgrounds) { background: -css2(nth($backgrounds, -1)); } @include background($backgrounds...); }
Set any number of background layers, along with a fallback. The final argument will be output separately, first, as a css2 fallback.
background-image
=background-image($images...) +image-property(background-image, $images...) @if $current-prefix == -svg background-size: 100%
@mixin background-image($images...) { @include image-property(background-image, $images...) { @if $current-prefix == -svg { background-size: 100%; } } }
Background image property support for vendor prefixing within values.
filter-gradient($start-color, $end-color, $orientation)
=filter-gradient($start-color, $end-color, $orientation: vertical) +for-legacy-browsers((ie: "8"), $gradient-support-threshold) +has-layout $gradient-type: if($orientation == vertical, 0, 1) filter: progid:DXImageTransform.Microsoft.gradient(gradientType=#{$gradient-type}, startColorstr='#{ie-hex-str($start-color)}', endColorstr='#{ie-hex-str($end-color)}')
@mixin filter-gradient($start-color, $end-color, $orientation: vertical) { @include for-legacy-browsers((ie: "8"), $gradient-support-threshold) { @include has-layout; $gradient-type: if($orientation == vertical, 0, 1); filter: progid:DXImageTransform.Microsoft.gradient(gradientType=#{$gradient-type}, startColorstr='#{ie-hex-str($start-color)}', endColorstr='#{ie-hex-str($end-color)}'); } }
Emit a IE-Specific filters that renders a simple linear gradient. For use in IE 6 - 8. Best practice would have you apply this via a conditional IE stylesheet, but if you must, you should place this before any background-image properties that you have specified.
For the $orientation
parameter, you can pass vertical
or horizontal
.
border-image($value)
=border-image($value) +with-each-prefix(border-image, $border-image-support-threshold) $border-prefix: $current-prefix +each-gradient-prefix($value) @if $current-prefix and prefixed($current-prefix, $value) $legacy-value: reject($value, fill) +prefix-prop(border-image, prefix($current-prefix, $legacy-value), $prefix: $border-prefix) @else +prefix-prop(border-image, $value, $prefix: $border-prefix)
@mixin border-image($value) { @include with-each-prefix(border-image, $border-image-support-threshold) { $border-prefix: $current-prefix; @include each-gradient-prefix($value) { @if $current-prefix and prefixed($current-prefix, $value) { $legacy-value: reject($value, fill); @include prefix-prop(border-image, prefix($current-prefix, $legacy-value), $prefix: $border-prefix); } @else { @include prefix-prop(border-image, $value, $prefix: $border-prefix); } } } }
Border image property support for vendor prefixing properties and values.
list-style-image($image)
=list-style-image($image) +image-property(list-style-image, $image)
@mixin list-style-image($image) { @include image-property(list-style-image, $image); }
List style image property support for vendor prefixing within values.
list-style($value)
=list-style($value) +image-property(list-style, $value)
@mixin list-style($value) { @include image-property(list-style, $value); }
List style property support for vendor prefixing within values.
content($value)
=content($value) +image-property(content, $value)
@mixin content($value) { @include image-property(content, $value); }
content property support for vendor prefixing within values.