sass是css3的扩展,增加了嵌套、变量、混合、选择器继承等。
它使用scss语法。
windows下载:
http://rubyinstaller.org/
国内先更换gem安装源
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
命令行输入:
gem install sass
gem install compass
sass-convert style.sass style.scss
sass-convert style.scss style.sass
sass input.scss output.css
sass –watch input.scss:output.css
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
编译后
body { font: 100% Helvetica, sans-serif; color: #333; }
示例:
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; }
.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’;
@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%; }
$ 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;
参考文章:
http://sass-lang.com/guide