SCSS
SCSS 语法介绍与实践技巧
注释
有三种形式:
(1)//comment:该注释只是在
(2)/_! _/:重要注释,任何
(3)/* */:该注释在
Quick Start
Installation
Build
命令行下切换到代码文件夹目录
h1 {
font-size: 17px;
}
h2 {
font-size: 18px;
}
运行命令:sass –style compressed test.scss test.css,即可生成压缩版的
(1)–
如果希望某一个
sass –watch –style compressed test.scss:test.css
当
sass –watch –style compressed sass:css
当
备注:
Webpack
变量与选择器
变量
定义
变量的定义一般以
$color1: #aeaeae;
.div1 {
background-color: $color1;
}
编译后:
.div1 {
background-color: #aeaeae;
}
/*# sourceMappingURL=test.css.map */
如果希望某个在子选择器中定义的变量能够成为全局变量,可以使用
#main {
$width: 5em !global;
width: $width;
}
#sidebar {
width: $width;
}
嵌套引用
嵌套引用在其他编程语言中即是字符串插值,需要用
$left: left;
.div1 {
border-#{$left}-width: 5px;
}
变量计算
$left: 20px;
.div1 {
margin-left: $left + 12px;
}
变量可以支持计算的类型,还是比较多的:
p {
font: 10px/8px; // Plain CSS, no division
$width: 1000px;
width: $width/2; // Uses a variable, does division
width: round(1.5) / 2; // Uses a function, does division
height: (500px/2); // Uses parentheses, does division
margin-left: 5px + 8px/2px; // Uses +, does division
font: (italic bold 10px/8px); // In a list, parentheses don't count
}
选择器
嵌套
.div1 {
.span1 {
height: 12px;
}
.div2 {
width: 16px;
}
}
属性也可以嵌套,比如
p {
border: {
color: red;
}
}
注意,
父元素引用
在嵌套的子层级中,允许使用
.div1 {
&:hover {
cursor: hand;
}
}
代码重用
继承
.class1 {
font-size: 19px;
}
.class2 {
@extend .class1;
color: black;
}
注意:如果在
.class1 {
font-size: 19px;
}
.class2 {
@extend .class1;
color: black;
}
.class1 {
font-weight: bold;
}
由此可以看出
引用外部css 文件(Partials)
有时网页的不同部分会分成多个文件来写样式,或者引用通用的一些样式,那么可以使用
@import "_test1.scss";
@import "_test2.scss";
@import "_test3.scss";
Mixin&Include
使用
@mixin left {
float: left;
margin-left: 10px;
}
使用
div {
@include left;
}
参数与缺省值
- 边距设置
@mixin common($value1, $value2, $defaultValue: 12px) {
display: block;
margin-left: $value1;
margin-right: $value2;
padding: $defaultValue;
}
.class1 {
font-size: 16px;
@include common(12px, 13px, 15px);
}
.class2 {
font-size: 16px;
@include common(12px, 13px);
}
- 浏览器前缀设置设置
下面是一个
@mixin rounded($vert, $horz, $radius: 10px) {
border-#{$vert}-#{$horz}-radius: $radius;
-moz-border-radius-#{$vert}#{$horz}: $radius;
-webkit-border-#{$vert}-#{$horz}-radius: $radius;
}
使用的时候,可以像下面这样调用:
#navbar li {
@include rounded(top, left);
}
#footer {
@include rounded(top, left, 5px);
}
Mixins Collection: 一些常见的Mixins 搜集
family.scss: 使nth-child 更易用
编程式方法
流程控制
条件语句
p {
@if 1 + 1 == 2 {
border: 1px solid;
}
@if 5 < 3 {
border: 2px dotted;
}
}
配套的还有
@if lightness($color) > 30% {
background-color: #000;
} @else {
background-color: #fff;
}
循环语句
@for $i from 1 to 10 {
.border-#{$i} {
border: #{$i}px solid blue;
}
}
也支持
$i: 6;
@while $i > 0 {
.item-#{$i} {
width: 2em * $i;
}
$i: $i - 2;
}
@each $member in a, b, c, d {
.#{$member} {
background-image: url("/image/#{$member}.jpg");
}
}
函数
@function double($n) {
@return $n * 2;
}
#sidebar {
width: double(5px);
}
颜色函数
lighten(#cc3, 10%) // #d6d65c
darken(#cc3, 10%) // #a3a329
grayscale(#cc3) // #808080
complement(#cc3) // #33c
模块化
目前我们组织样式的方式为
.test-comtainer {
width: 10px;
.sub1 {
width: 10px;
.ssub2 {
width: 10px;
}
}
}
编译成的代码为:
.test-comtainer {
width: 10px;
}
.test-comtainer .sub1 {
width: 10px;
}
.test-comtainer .sub1 .ssub2 {
width: 10px;
}
可以看出,使用这种方式组织样式采用了后代选择器,如果组件比较复杂,嵌套层次过深。编译出的
因此,在参考了多个组件库的实现后,考虑使用一种更为扁平化的方式组织样式:
$prefix: .test;
$prefix {
width: 10px;
&-sub1 {
width: 10px;
}
&-sub2 {
width: 10px;
&-ssub {
width: 10px;
}
}
}
编译后的
.test {
width: 10px;
}
.test-sub1 {
width: 10px;
}
.test-sub2 {
width: 10px;
}
.test-sub2-ssub {
width: 10px;
}
可以看出,使用这种方式组织的
// 之前的写法,比较方便
<div className="test-container">
<div className="sub1"/>
</div>
// 现在的写法,需要拼接长类名
<div className="test">
<div className="test-sub1"/>
</div>
为了解决ts-classname-plugin
,此插件是&
的作用。在&
是嵌套结构中外层选择器的缩写,而在className
属性进行处理。