SCSS相关介绍
SCSS官方文档
#main p {
color: #00ff00;
width: 97%;
.redbox {
background-color: #ff0000;
color: #000000;
}
}
编译为:
#main p {
color: #00ff00;
width: 97%;
}
#main p .redbox {
background-color: #ff0000;
color: #000000;
}
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}
编译为:
a {
font-weight: bold;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
body.firefox a {
font-weight: normal;
}
.funky {
font: {
family: fantasy;
size: 30em;
weight: bold;
}
}
编译为:
.funky {
font-family: fantasy;
font-size: 30em;
font-weight: bold;
}
$width: 5em;
// 使用
#main {
width: $width;
}
编译为:
#main {
width: 5em
}
p {
width: 10px + 20px;
}
编译为:
p {
width: 30px;
}
$content: "First content";
$content: "Second content?" !default;
$new_content: "First time reference" !default;
#main {
content: $content;
new-content: $new_content;
}
编译为:
#main {
content: "First content";
new-content: "First time reference";
}
@import "foo.scss";
// 或
@import "foo";
@import "rounded-corners", "text-shadow";
@media screen {
.sidebar {
@media (orientation: landscape) {
width: 500px;
}
}
}
编译为:
@media screen and (orientation: landscape) {
.sidebar {
width: 500px; }
}
.father{
color: yellow;
font-size: 18px;
}
.son{
@extend .father;
font-weight: bold;
}
编译为:
.father{
color: yellow;
font-size: 18px;
}
.son{
color: yellow;
font-size: 18px;
font-weight: bold;
}
p {
@if 1 + 1 == 2 { border: 1px solid; }
@if 5 < 3 { border: 2px dotted; }
@if null { border: 3px double; }
}
编译为:
p {
border: 1px solid;
}
$type: monster;
p {
@if $type == ocean {
color: blue;
} @else if $type == matador {
color: red;
} @else if $type == monster {
color: green;
} @else {
color: black;
}
}
编译为:
p {
color: green;
}
@for 指令可以在限制的范围内重复输出格式,每次按要求(变量的值)对输出结果做
出变动。这个指令包含两种格式:@for $var from through ,或者
@for $var from to ,区别在于 through 与 to 的含义:当使用 through 时,条
件范围包含 与 的值,而使用 to 时条件范围只包含 的值不包含
的值。另外,$var 可以是任何变量,比如 $i; 和 必须是整数
值。即through前闭后闭 to是前闭后开
@for $i from 1 through 3 {
.item-#{$i} { width: 2em * $i; }
}
编译为:
.item-1 {
width: 2em;
}
.item-2 {
width: 4em;
}
.item-3 {
width: 6em;
}
@each 指令的格式是 $var in , $var 可以是任何变量名,比如 $length 或者
$name,而 是一连串的值,也就是值列表。
@each 将变量 $var 作用于值列表 中的每一个项目,然后输出结果,例如:
@each $animal in puma, sea-slug, egret, salamander {
.#{$animal}-icon {
background-image: url('/images/#{$animal}.png');
}
}
编译为:
.puma-icon {
background-image: url('/images/puma.png');
}
.sea-slug-icon {
background-image: url('/images/sea-slug.png');
}
.egret-icon {
background-image: url('/images/egret.png');
}
.salamander-icon {
background-image: url('/images/salamander.png');
}
$i: 6;
@while $i > 0 {
.item-#{$i} { width: 2em * $i; }
$i: $i - 2;
}
编译为:
.item-6 {
width: 12em;
}
.item-4 {
width: 8em;
}
.item-2 {
width: 4em;
}
@mixin large-text {
font: {
family: Arial;
size: 20px;
weight: bold;
}
color: #ff0000;
}
@mixin clearfix {
display: inline-block;
&:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
* html & { height: 1px }
}
.page-title {
@include large-text;
padding: 4px;
margin-top: 10px;
}
编译为:
.page-title {
font-family: Arial;
font-size: 20px;
font-weight: bold;
color: #ff0000;
padding: 4px;
margin-top: 10px;
}
@mixin sexy-border($color, $width) {
border: {
color: $color;
width: $width;
style: dashed;
}
}
p { @include sexy-border(blue, 1rem); }
编译为:
p {
border-color: blue;
border-width: 1rem;
border-style: dashed;
}
$grid-width: 40px;
$gutter-width: 10px;
@function grid-width($n) {
@return $n * $grid-width + ($n - 1) * $gutter-width;
}
#sidebar { width: grid-width(5); }
编译为:
#sidebar {
width: 240px;
}