简介
sass是css3的扩展,增加了嵌套、变量、混合、选择器继承等。
它使用scss语法。
安装
安装Ruby
windows下载:
http://rubyinstaller.org/
安装sass
国内先更换gem安装源
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
命令行输入:
gem install sass
gem install compass
使用命令
sass 转换为 scss
sass-convert style.sass style.scss
scss 转换为sass
sass-convert style.scss style.sass
sass 输出css
sass input.scss output.css
监控自动编译
sass –watch input.scss:output.css
SCSS语法
变量
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
编译后
body {
font: 100% Helvetica, sans-serif;
color: #333;
}
SASS语法
nested语法
示例:
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
编译后:
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
引用
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}
编译后:
a {
font-weight: bold;
text-decoration: none; }
a:hover {
text-decoration: underline; }
body.firefox a {
font-weight: normal; }
Nested属性
.funky {
font: {
family: fantasy;
size: 30em;
weight: bold;
}
}
编译后:
.funky {
font-family: fantasy;
font-size: 30em;
font-weight: bold; }
缩进
#sidebar
width: 30%
background-color: #faa
编译成css会是:
#sidebar {
width: 30%;
background-color: #faa;
}
注释:
/* This comment is
* several lines long.
* since it uses the CSS comment syntax,
* it will appear in the CSS output. */
body { color: black; }
// These comments are only one line long each.
// They won't appear in the CSS output,
// since they use the single-line comment syntax.
a { color: green; }
包含
子scss文件格式:_partial.scss
@import ‘reset’;
Mixins
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
border-radius: $radius;
}
.box { @include border-radius(10px); }
编译后:
.box {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
继承
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
@extend .message;
border-color: green;
}
.error {
@extend .message;
border-color: red;
}
.warning {
@extend .message;
border-color: yellow;
}
编译后:
.message, .success, .error, .warning {
border: 1px solid #cccccc;
padding: 10px;
color: #333;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
运算
.container { width: 100%; }
article[role="main"] {
float: left;
width: 600px / 960px * 100%;
}
aside[role="complimentary"] {
float: right;
width: 300px / 960px * 100%;
}
编译后:
.container {
width: 100%;
}
article[role="main"] {
float: left;
width: 62.5%;
}
aside[role="complimentary"] {
float: right;
width: 31.25%;
}
SassScript
控制台测试
$ sass -i
>> "Hello, Sassy World!"
"Hello, Sassy World!"
>> 1px + 1px + 1px
3px
>> #777 + #777
#eeeeee
>> #777 + #888
white
变量
$width: 5em;
#main {
width: $width;
}
变量有作用域限制,默认是在自己的nested生效。如果要全局生效,使用
$width: 5em ~global;
数据类型
- numbers (e.g. 1.2, 13, 10px)
- strings of text, with and without quotes (e.g. “foo”, ‘bar’, baz)
- colors (e.g. blue, #04a3f9, rgba(255, 0, 0, 0.5))
- booleans (e.g. true, false)
- nulls (e.g. null)
- lists of values, separated by spaces or commas (e.g. 1.5em 1em 0 2em, Helvetica, Arial, sans-serif)
- maps from one value to another (e.g. (key1: value1, key2: value2))
字符串
列表
map键值对
参考文章:
http://sass-lang.com/guide