1、安装Sass
npm install sass -g
2、scss转css
sass demo1.scss demo1.css
3、监视sass
sass --watch demo1.scss:demo1.css
4、取消编译生成的.map文件
sass --watch demo1.scss:demo1.css --no-source-map
5、变量的定义和使用
$variable: value;
/* 定义变量与值 */
$bgcolor: red;
$textcolor: blue;
$fontsize: 18px;
/* 使用变量 */
body {
background-color: $bgcolor;
color: $textcolor;
font-size: $fontsize;
}
6、sass的作用域
$myColor: red;
h1 {
$myColor: green; // 只在 h1 里头有用,局部作用域 green
color: $myColor;
}
p {
color: $myColor; // red
}
$myColor: red;
h1 {
$myColor: green !global; // 全局作用域 green
color: $myColor;
}
p {
color: $myColor; // green
}
7、sass的嵌套规则和属性
7.1、规则
sass代码
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline-block;
}
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
css代码
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
7.2、sass 嵌套属性
sass代码
font: {
family: Helvetica, sans-serif;
size: 18px;
weight: bold;
}
text: {
align: center;
transform: lowercase;
overflow: hidden;
}
css代码
font-family: Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
text-align: center;
text-transform: lowercase;
text-overflow: hidden;
8、sass @mixin与 @include
// 定义混入
@mixin important-text {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
}
.danger {
// 使用混入
@include important-text;
background-color: green;
}
// 生成css代码
.danger {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
background-color: green;
}
混入中也可以包含混入
@mixin special-text {
@include important-text;
@include link;
@include special-border;
}
混入中传递变量
/* 混入接收两个参数 */
@mixin bordered($color, $width) {
border: $width solid $color;
}
.myArticle {
@include bordered(blue, 1px); // 调用混入,并传递两个参数
}
.myNotes {
@include bordered(red, 2px); // 调用混入,并传递两个参数
}
css代码
.myArticle {
border: 1px solid blue;
}
.myNotes {
border: 2px solid red;
}
混入中变量默认值
@mixin sexy-border($color, $width: 1in) {
border: {
color: $color;
width: $width;
style: dashed;
}
}
p { @include sexy-border(blue); }
h1 { @include sexy-border(blue, 2in); }
css代码
p {
border-color: blue;
border-width: 1in;
border-style: dashed; }
h1 {
border-color: blue;
border-width: 2in;
border-style: dashed;
}
可变参数
@mixin box-shadow($shadows...) {
-moz-box-shadow: $shadows;
-webkit-box-shadow: $shadows;
box-shadow: $shadows;
}
.shadows {
@include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
css代码
.shadows {
-moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
-webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}
9、sass继承
.button-basic {
border: none;
padding: 15px 30px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button-report {
@extend .button-basic;
background-color: red;
}
.button-submit {
@extend .button-basic;
background-color: green;
color: white;
}
css代码
.button-basic, .button-report, .button-submit {
border: none;
padding: 15px 30px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button-report {
background-color: red;
}
.button-submit {
background-color: green;
color: white;
}
CSDN:https://blog.csdn.net/weixin_45585996/article/details/107252918