# Sass
# 文件后缀区别
- sass: 缩进语法;
- scss: 使用花括号和分号,和 css 相近。
# 使用变量($)
- $ 符号定义,旧版本使用 “!”
| $basic-border: 1px solid #ccc; |
- 有作用范围,如果在花括号中定义,則范围只能在花括号内部。
| $width: 100px; |
| .nav { |
| $width: 20px; |
| width: $width; |
| } |
- 中划线和下划线相互兼容。
| $hightlight-color: red; |
| .nav { |
| color: $hightlight_color; |
| } |
# 嵌套
- 俄罗斯套娃
| #content { |
| #article { |
| p1 { |
| font-size: 20px; |
| } |
| a { |
| color: blue; |
| } |
| } |
| |
| #aside { |
| p1 { |
| font-size: 16px; |
| } |
| } |
| } |
- $ 连接父级选择器,用于伪类
| |
| #content a { |
| color: blue; |
| &:hover { |
| color: red; |
| } |
| } |
| |
| #content a { |
| body.ie & { |
| color: red; |
| } |
| } |
| |
- 群组选择器嵌套
| .container { |
| h1, h2, h3 { |
| margin-bottom: .8em; |
| } |
| } |
| .nav1, .nav2 { |
| h1 { |
| margin-bottom: .8em; |
| } |
| } |
- 子代选择器,同级选择器: >, +, ~
| article { |
| ~ article { border-top: 1px dashed #ccc } |
| > section { background: #eee } |
| dl > { |
| dt { color: #333 } |
| dd { color: #555 } |
| } |
| nav + & { margin-top: 0 } |
| } |
| |
| |
| |
| # 编译后 |
| article ~ article { border-top: 1px dashed #ccc; } |
| article > section { background: #eee; } |
| article dl > dt { color: #333; } |
| article dl > dd { color: #555; } |
| nav + article { margin-top: 0; } |
- 嵌套属性:用 - 连接,比如 border-style
| |
| nav { |
| border: { |
| style: solid; |
| width: 1px; |
| color: #ccc; |
| } |
| } |
| |
| nav { |
| border: 1px solid #ccc { |
| left: 0px; |
| right: 0px; |
| } |
| } |
# 导入(@import)
- 与 css 的 @import 不同,直接编译成同一个文件不用再次请求一次文件;
- 可以省略后缀;
- 可以导入不同语法的文件:scss, sass
- !default 设置默认变量值
| $link-color: blue !default; |
| |
- 嵌套导入
| |
| $hightlight: red; |
| #aside { |
| color: red; |
| } |
| |
| .theme { |
| @import "file1" |
| } |
| |
- 原生导入
| |
| @import "1.css"; |
| |
| @import "http://localhost/1.scss" |
| |
| @import url("1.scss") |
# 注释
# 混合器(@mixin, @include)
- 使用 @mixin 定义 mixin
| @mixin rounded-corners { |
| -moz-border-radius: 5px; |
| -webkit-border-radius: 5px; |
| border-radius: 5px; |
| } |
- 使用 @include 引入 mixin
| .notice { |
| background-color: green; |
| border: 2px solid #00aa00; |
| @include rounded-corners; |
| } |
- 传参
| @mixin link-colors($normal, $hover, $visited) { |
| color: $normal; |
| &:hover { color: $hover; } |
| &:visited { color: $visited; } |
| } |
| |
| a { |
| @include link-colors(blue, red, green); |
| } |
| |
| a { |
| @include link-colors( |
| $normal: blue, |
| $visited: green, |
| $hover: red |
| ); |
| } |
| |
| |
| @mixin link-colors( |
| $normal, |
| $hover: $normal, |
| $visited: $normal |
| ) |
| { |
| color: $normal; |
| &:hover { color: $hover; } |
| &:visited { color: $visited; } |
| } |
# 继承选择器(@extend)
重复类,所以会比 mixin 占用体积更小!不要用在后代选择器!
| .error { |
| border: 1px red; |
| background-color: #fdd; |
| } |
| .seriousError { |
| @extend .error; |
| border-width: 3px; |
| } |
# Less
# 变量
只能定义一次,相当于常量。
| @nice-blue: #5B83AD; |
| @light-blue: @nice-blue + #111; |
| |
| #header { |
| color: @light-color; |
| } |
# 变量插值
- 选择器
| |
| @mySelector: banner; |
| |
| |
| .@{mySelector}: { |
| font-weight: bold; |
| line-height: 40px; |
| margin: 0 auto; |
| } |
- URLs
| |
| @images: "../images"; |
| |
| |
| body { |
| background: url("@{images}/bg.jpg"); |
| } |
- Import 语句
| |
| @themes: "../themes"; |
| |
| |
| @import "@{themes}/variables.less"; |
- 属性
| |
| @property: color; |
| |
| |
| .widget { |
| @{property}: blue; |
| backgroud-@{property}: white; |
| } |
- 变量名
# 懒加载
可以先使用变量,再定义!
# 默认变量
变量可以重载,加上懒加载,所以以下代码运行良好。
| |
| @base-color: green; |
| @dark-color: darken(@base-color, 10%); |
| |
| |
| @import “library.less”; |
| @base-color: blue; |
# extend 继承
| .nav ul { |
| &: extend(.inline); |
| background: blue; |
| } |
| .inline { |
| color: red; |
| } |
输出
| .nav ul { |
| background: blue; |
| } |
| .inline, |
| .nav ul { |
| color: red; |
| } |