Variables let you store information to be reused anywhere in a stylesheet.
Variables are set just like CSS properties, placing a $ symbol before the
name of the variable ($var-name). They may also be set placing a $ symbol
before two parentheses wrapping the name of the variable ($(var-name)), or by
wrapping the $ symbol and variable name in curly braces preceeded by a hash
(#{$var-name}).
*Note: To use #{$var-name} without issues, you will need to include the
PostCSS SCSS Syntax.
In that example, $font-size, $font-stack, and $primary-color are replaced
with their values.
body { font: 1.25em "Helvetica Neue", sans-serif; color: #333;}
@if and @else Rules
Conditionals like @if and @else let you use rules in a stylesheet if they
evaluate true or false.
Conditionals are set by writing @if before the expression you want to
evaluate. If the expression is true, then its contents are included in the
stylesheet. If the expression is false, then its contents are not included, but
the contents of an @else that follows it are included.
In that example, $type === ocean is false, so the @if contents are ignored
and the @else contents are used.
p { color: black;}
@for and @each Rules
Iterators like @for and @each let you repeat content in a stylesheet.
A @for statement repeats by a numerical counter defined as a variable.
It can be written as @for $counter from <start> through <end> where
$counter is the name of the iterating variable, <start> is the number to
start with, and <end> is the number to finish with.
It can also be written as @for $counter from <start> to <end> where
$counter is still the name of the counter variable, <start> is still the
number to start with, but <end> is now the number to finish
before, but not include.
When <start> is greater than <end>, the counter will decrement instead of
increment.
Either form of @for can be written as
@for $var from <start> to <end> by <increment> or
@for $var from <start> through <end> by <increment>
where <incremement> is the amount the counter variable will advance.
@for $i from 1 through 5 by 2 { .width-#{$i} { width: #{$i}0em; }}@for $j from 1 to 5 by 2 { .height-#{$j} { height: #{$j}0em; }}
In that example, $i is repeated from 1 through 5 by 2, which means it is
repeated 3 times (1, 3, and 5). Meanwhile, $j is repeated from 1 to 5 by 2,
which means it is repeated 2 times (1 and 3).
It can also be written as @each $item $counter in $list where $item is
still the name of the iterating variable and $list is still the list of values
being looped over, but now $counter is the numerical counter.
In that example, a list of 4 animals is looped over to create 4 unique
classnames.
@mixin, @include, and @content rules
Mixins let you reuse rule in a stylesheet. A @mixin defines the content you
want to reuse, while an @include rule includes it anywhere in your stylesheet.
Mixins are set by writing @mixin before the name of the mixin you define.
This can be (optionally) followed by comma-separated variables you
want to use inside of it. Mixins are then used anywhere by writing @include
before the name of the mixin you are using. This is (again, optionally)
followed by some comma-separated arguments you want to pass into the mixin as
the (aforementioned) variables.
.hero { max-width: $site-width;}/* after */.hero { max-width: 960px;}
unresolved
The unresolved option defines how unresolved variables, mixins, and imports
should be handled. The available options are throw, warn, and ignore. The
default option is to throw.
The disable option can be a string or an array, and the features that can be
disabled are @content, @each, @else, @if, @include, @import, @for,
and @mixin.
The importResolve option defines the file resolver used by imports. It is a
function given 3 arguments; the url id, the current working directory, and the
options processed by PostCSS Advanced Variables.
The importResolve function should return a Promise with an object containing
the full path (file) and the contents of the file (contents).
const resolve = require('custom-resolver');require('postcss-advanced-variables')({ // a resolver may work many ways, and this is just an example importResolve: (id, cwd, opts) => resolve({ id, cwd });});
importFilter
The importFilter option determines whether an import will be inlined.
The value can be a function or an regular expression. When
providing a function, it is called with a single string argument id
and returns true when the import should be inlined. When providing a
regular expression, if the id matches the expression, the import will
be inlined.
By default, imports are ignored if they begin with a protocol or
protocol-relative slashes (//).