1、安裝Sass
npm install sass -g
2趴酣、scss轉(zhuǎn)css
sass demo1.scss demo1.css
3梨树、監(jiān)視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的嵌套規(guī)則和屬性
7.1指巡、規(guī)則
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;
}
混入中傳遞變量
/* 混入接收兩個(gè)參數(shù) */
@mixin bordered($color, $width) {
border: $width solid $color;
}
.myArticle {
@include bordered(blue, 1px); // 調(diào)用混入藻雪,并傳遞兩個(gè)參數(shù)
}
.myNotes {
@include bordered(red, 2px); // 調(diào)用混入,并傳遞兩個(gè)參數(shù)
}
css代碼
.myArticle {
border: 1px solid blue;
}
.myNotes {
border: 2px solid red;
}
混入中變量默認(rèn)值
@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;
}
可變參數(shù)
@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