Sass CSS Preprocessor¶
Info
Since Version 3 SCSS is the official Syntax, which adds braces to the definition. There,fore only this format is described here.
Variables¶
To reuse the same information again, variables may be used. They need a pound sign in front.
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
Nesting¶
With nesting the CSS will become more organized:
div {
// will become: div a { .... }
a {
color: red;
// The parent selector helps to further extend the same element.
// div a:hover { .... }
&:hover {
font-weight: bold;
}
}
}
.test {
// It can also be used to style the outer selector in a certain context, such
// as a body set to use a right-to-left language.
// [dir=rtl] .test { ... }
[dir=rtl] & {
margin-left: 0;
margin-right: 10px;
}
// You can even use it as an argument to pseudo-class selectors.
// :not(.test) { ... }
:not(&) {
opacity: 0.8;
}
// Also a suffix to the existing class can be set.
// .test--dark { ... }
&--dark {
color: white;
}
Mixins¶
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.box { @include transform(rotate(30deg)); }
Extend¶
You define a block of scripts as Extend block, which may be used anywhere:
%message-shared {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.message {
@extend %message-shared;
}
Operators¶
Mathematical operators are possible, too.
article[role="main"] {
float: left;
width: 600px / 960px * 100%;
}
At-rules¶
@use,@forward,@importloads mixins, functions, and variables.@mixinand @include makes it easy to re-use chunks of styles.@functiondefines custom functions that can be used in SassScript expressions.@extendallows selectors to inherit styles from one another.@at-rootputs styles within it at the root of the CSS document.- Flow control rules like
@if,@each,@forand@whilecontrol whether or how many times styles are emitted.
Builtin Modules¶
sass:mathmodule provides functions that operate on numbers.sass:stringmodule makes it easy to combine, search, or split apart strings.sass:colormodule generates new colors based on existing ones, making it easy to build color themes.sass:listmodule lets you access and modify values in lists.sass:mapmodule makes it possible to look up the value associated with a key in a map, and much more.sass:selectormodule provides access to Sass’s powerful selector engine.sass:metamodule exposes the details of Sass’s inner workings.
More details can be found in Sass Documentation.
Last update: January 1, 2021