本文主要包含CSS,sass等相关知识,TomDong 希望在学习及工作中可以帮助到您
sass结尾的文件有着更严格的格式要求,scss文件更贴近原生css
比如sass文件
- #main
- color: #fff
- &-sidebar
- border: 1px solid
等同于scss的
- #main {
- color: black;
- &-sidebar { border: 1px solid; }
- }
编译后为
其中代表父级元素的& 只可出现在头部,否则解析不出来。
嵌套属性
- .funky {
- font: {
- family: fantasy;
- size: 30em;
- weight: bold;
- }
- }
编译为
- .funky {
- font-family: fantasy;
- font-size: 30em;
- font-weight: bold; }
这个在一些复杂的css3属性上用处比较大,比如animate
字符串内插
使用#{}作为选择器变量
- $name: foo;
- $attr: border;
- p.#{$name} {
- #{$attr}-color: blue;
- }
- p {
- $font-size: 12px;
- $line-height: 30px;
- font: #{$font-size}/#{$line-height};
- }
第二个例子如果不用字面量则12除30
运算符
- p {
- width: 1in + 8pt;
- }
单位如果可以转换,会被自动转换
编译为:
- p {
- width: 1.111in; }
- 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