Sass使用方式和常见语法总结
创建时间:
字数:1.3k
阅读:
本文主要介绍sass的语法
Sass介绍
Sass是世界上最成熟、稳定和强大的专业级CSS扩展语言😎。
Sass是一个将脚本解析成CSS的脚本语言,即SassScript,有很多好用的语法可以帮助我们减少css代码的冗余。而且他完全兼容所有版本的css,也就是说在sass里可以直接使用css。对于使用熟练的人来说它可以极大地😍提高编程效率。
sass是一个基于ruby环境的可编程的css,不能直接用于页面,他需要编译成css以后在html文件中引入使用。
sass有两种后缀名文件:一种后缀名为sass,不使用大括号和分号;另一种就是scss文件,这种和我们平时写的css文件格式差不多,使用大括号和分号。本篇文章所有sass文件都指后缀名为scss的文件。在此也建议使用后缀名为scss的文件,以避免sass后缀名的严格格式要求报错。
Sass语法
运算
sass可进行简单的加减乘除运算等 , 当我们拿到一张需要转换成百分比布局的设计稿, 这时候我们有福了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| .container{ width: 100%; } .aside{ width:600px/960px*100%; } article{ width:300px/960px*100%; }
.container{ width: 100%; } .aside{ width:62.5%; } article{ width:31.25%; }
|
变量
必须以$开头
1 2 3 4 5 6 7 8 9 10
| $font-size:16px; div{ font-size: $font-size; }
div{ font-size: 16px; }
|
复杂变量的使用
1 2 3 4 5 6 7 8 9 10 11 12
| $linkColor:#ffffff #6fb6fd; div{ color: nth($linkColor,1); background-color:nth($linkColor,2); }
div{ color: #ffffff; background-color:#6fb6fd; }
|
一般我们都将变量当做属性值来使用, 但是也有极特殊情况下我们会将变量当做选择器或者属性名使用。 这时候, 我们必须以#{$name}的方式来使用变量名
1 2 3 4 5 6 7 8 9 10
| $name:top; .header-#{$name}{ border-#{name}:1px solid #b6b6b6; }
.header-top{ border-top:1px solid #b6b6b6; }
|
多值变量:代表的是多维数据的存储方式,换句话说,list相当于js中的数组。 list数据一般用空格分割, 但是也可以用 逗号 或者小括号分割多个值
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $list : (20px 40px)(30px 20px 10)(4px 3px 2px 5px); .main { margin: nth($list,1); padding: nth($list,2); border-radius: nth($list,3); }
.main { margin: 20px 40px; padding: 30px 20px 10; border-radius:4px 3px 2px 5px; }
|
map的数据是以键值对形式出现的,相当于js中的对象,可以结合each完成循环渲染
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $headers:(h1:20px,h2:30px,h3:40px); @each $key, $value in $headers{ #{$key}{ font-size: $value; } }
h1 { font-size: 20px; } h2 { font-size: 30px; } h3 { font-size: 40px; }
|
嵌套
sass可以进行选择器的嵌套,表示层级关系,看起来很有*格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ul{ list-style: none; li{ float: left; >a { color:#f00; } } }
ul{ list-style: none; } ul li{ float: left; } ul li>a { color:#f00; }
|
属性也可以嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| .main{ border:{ style:solid; left:none; right:1px; color:#b6b6b6; } }
.main{ border-style:solid; border-left:none; border-right:1px; border-color:#b6b6b6; }
|
嵌套的时候 & 代表上一层选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $color: #ddd #e00; a { color: nth($color,1); &:hover { color: nth($color,2); } }
a { color: #ddd; } a:hover { color: #e00; }
|
&还可以这么用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .header { height: 100px; &-top { height: 40px; } }
.header { height: 100px; } .header-top { height: 40px; }
|
mixin(混合)
sass中使用@mixin声明混合,可以传递参数,参数名以$符号开始,多个参数以逗号分开,也可以给参数设置默认值。声明的@mixin通过@include来调用。sass中可用mixin定义一些代码片段,且可传参数,方便日后根据需求调用。从此处理css3的前缀兼容轻松便捷。
无参数 mixin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @mixin marginCenter{ margin-left:auto; margin-right:auto; } .container{ width: 1000px; @include marginCenter; }
.container{ width: 1000px; margin-left:auto; margin-right:auto; }
|
有参数 minxin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@mixin marginCenter ($width:1000px){ width: $width; margin-left:auto; margin-right:auto; }
.container{ @include marginCenter; } .inner{ @include marginCenter(800px); }
.container{ width: 1000px; margin-left:auto; margin-right:auto; } .inner{ width: 800px; margin-left:auto; margin-right:auto; }
|
扩展/继承
sass可通过@extend来实现代码组合声明,使代码更加优越简洁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| .active{ border:1px solid #b6b6b6; padding:10px; color: #333; } .success{ @extend .active; width:100px; }
.active, .success{ border:1px solid #b6b6b6; padding:10px; color: #333; } .success{ width:100px; }
|
函数
sass定义了很多函数可供使用,当然你也可以自己定义函数,以@fuction开始。实际项目中我们使用最多的应该是颜色函数,而颜色函数中又以lighten减淡和darken加深为最,其调用方法为lighten($color,$amount)和darken($color,$amount),它们的第一个参数都是颜色值,第二个参数都是百分比。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| $baseFontSize:10px; $gray:#ccc; @function pxToRem($px){ @return $px/$baseFontSize*1rem; } html { font-size:$baseFontSize; } body{ color:lighten($gray,10%); } .test{ font-size:pxToRem(16px); color:darken($gray,10%); }
html { font-size:10px; } body{ color:#e6e6e6; } .test{ font-size:1.6rem; color:#b3b3b3; }
|
导入
sass中如导入其他sass文件,最后编译为一个css文件,优于纯css的@import
sass还有一些不太常用的语法请参考官网文档: https://www.sass.hk/docs/
代码不是万能的,但不写代码是万万不能的
Dary记
-
更多干货,尽在公众号
转载请注明来源,文末有原始链接。欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 dary1112@foxmail.com
文章标题:Sass使用方式和常见语法总结
文章字数:1.4k
本文作者:Dary
发布时间:2019-12-03, 13:37:00
最后更新:2020-03-14, 11:06:44
原始链接:http://www.xiongdalin.com/2019/12/03/sass/
版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。