範例對比:
/* Sass */
#main
color: #fff
&-sidebar
border: 1px solid
/* SCSS 等價 */
#main {
color: black;
&-sidebar { border: 1px solid; }
}
編譯後 & 代表父選擇器,只能出現在選擇器開頭,否則無法解析。
同一命名空間屬性可群組:
.funky {
font: {
family: fantasy;
size: 30em;
weight: bold;
}
}
/* 編譯為 */
.funky {
font-family: fantasy;
font-size: 30em;
font-weight: bold;
}
適用於 animation、transition 等複雜 CSS3 屬性群組。
#{}動態構建選擇器、屬性名、值:
$name: foo;
$attr: border;
p.#{$name} { #{$attr}-color: blue; }
$font-size: 12px;
$line-height: 30px;
p { font: #{$font-size}/#{$line-height}; } /* 避免 12/30 被當除法 */
p { width: 1in + 8pt; } /* 編譯為 1.111in */
除法規則:
10px/8px → 不除,保留斜線(字體簡寫慣例)。$width/2、函數 round(1.5)/2、括號 (500px/2)、加法上下文 5px + 8px/2px → 執行除法。color: #010203 + #040506; // RGB 分通道相加
color: #010203 * 2; // 相乘
color: rgba(255,0,0,0.75) + rgba(0,255,0,0.75); // Alpha 疊加
color: opacify($c, 0.3); // 降低透明度(加強不透明)
background: transparentize($c, 0.25); // 增加透明度
!default$content: "First content";
$content: "Second content?" !default; // 已賦值,保留 "First content"
$new_content: "First time reference" !default; // 未賦值,使用此值
!default:若變數已賦值則保留舊值,否則賦予新值,利於框架預設值覆寫。