1. sass/scss
2. 安装sass
1. 安装ruby
- sass是基于ruby环境的
- 安装ruby
- 勾选Add Ruby executables to your PATH
- 安装完成后需测试安装有没有成功,运行CMD输入以下命令:
ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [i386-mingw32]
2. 安装sass
- 因为国内网络的问题导致gem源间歇性中断因此我们需要更换gem源
gem sources --remove https:
gem sources -a https:
gem sources -l
*** CURRENT SOURCES ***
https:
gem install sass
gem install compass
- 安装完成之后,你应该通过运行下面的命令来确认应用已经正确地安装到了电脑中
sass -v
Sass 3.x.x (Selective Steve)
compass -v
Compass 1.x.x (Polaris)
Copyright (c) 2008-2015 Chris Eppstein
Released under the MIT License.
Compass is charityware.
Please make a tax deductable donation for a worthy cause: http:
3. 编译
1. 编译命令
sass input.scss output.css
sass --watch input.scss:output.css
sass --watch app/sass:public/stylesheets
2. 编译格式
sass style.scss:style.css --style nested
.box {
width: 300px;
height: 400px; }
.box-title {
height: 30px;
line-height: 30px; }
sass style.scss:style.css --style expanded
.box {
width: 300px;
height: 400px;
}
.box-title {
height: 30px;
line-height: 30px;
}
sass style.scss:style.css --style compact
.box { width: 300px; height: 400px; }
.box-title { height: 30px; line-height: 30px; }
sass style.scss:style.css --style compressed
.box{width:300px;height:400px}.box-title{height:30px;line-height:30px}
3. easysass
4. 变量
1. 普通变量
$highlight-color: #F90;
2. 默认变量
$highlight-color: #F90!default;
3. 引用变量
$color: #ff8907!default;
$bg: '../images/1.jpg';
$height_max: 'height';
div{
#{$height_max}: 200px;
color:$color;
background: url(#{$bg}) center no-repeat
}
4. 嵌套css
#content {
article {
h1 { color: #333 }
p { margin-bottom: 1.4em }
}
aside { background-color: #EEE }
}
/* 编译后 */
#content article h1 { color: #333 }
#content article p { margin-bottom: 1.4em }
#content aside { background-color: #EEE }
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; }
nav {
border: {
style: solid;
width: 1px;
color: #ccc;
}
}
5. 混合器
- 样式类似,可以通过sass的混合器实现大段样式的重用。
- 混合器使用@mixin标识符定义
- 通过@include来使用这个混合器
1. 混合器用法
@mixin rounded-corners {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
/*引用*/
notice {
background-color: green;
border: 2px solid #00aa00;
@include rounded-corners;
}
/*sass编译*/
.notice {
background-color: green;
border: 2px solid #00aa00;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
2. 混合器传参
@mixin link-colors($normal, $hover, $visited) {
color: $normal;
&:hover { color: $hover; }
&:visited { color: $visited; }
}
/*引用*/
a {
@include link-colors(blue, red, green);
}
/*sass编译*/:
a { color: blue; }
a:hover { color: red; }
a:visited { color: green; }
3. 默认参数值
@mixin link-colors($color:red) {
color: $color;
}
/*引用*/
a {
/*不传参*/
@include link-colors();
}
/*sass编译*/:
a { color: red; }
6. 继承
.error {
border: 1px solid red;
background-color: #fdd;
}
.seriousError {
@extend .error;
border-width: 3px;
}
7. 导入SASS文件
- @import 它允许在一个css文件中导入其他css文件
- 使用sass的@import规则并不需要指明被导入文件的全名,可以省略.sass或.scss文件后缀
@import './common.scss';
@import './reset.scss';