SASS是最早、最成熟的CSS预处理,它可以通过变量、嵌套、函数、继承等使CSS变得更强大,更优雅,而且完全兼容CSS的语法。SASS可以帮助我们更好地组织大CSS文件,更加便捷地开发和运行CSS文件,在Compass等类库的帮助下,我们将更加如虎添翼。
//使用SCSS $baseFontsize:14px; $Largefontsize:16px; body{ font-size:$baseFontsize; p{ font-size:$Largefontsize; } } //使用SASS $baseFontsize: 14px $Largefontsize: 16px body font-size: $baseFontsize p font-size: $Largefontsize两种语法各有所长,也各有所爱。SCSS好在贴近CSS,SASS妙在简洁清爽,大家可以参考下这篇文章《 Sass vs. SCSS: which syntax is better?》,至于你选择那种语法,就全在你了。
# Convert Sass to SCSS $ sass-convert style.sass style.scss # Convert SCSS to Sass $ sass-convert style.scss style.sass
#main p { color: #00ff00; width: 97%; .redbox { background-color: #ff0000; color: #000000; } }上面的这段嵌套的代码编译过之后,将会是这样
#main p { color: #00ff00; width: 97%; } #main p .redbox { background-color: #ff0000; color: #000000; }
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; }
#main { color: black; a { font-weight: bold; &:hover { color: red; } } }将会被编译成
#main { color: black; } #main a { font-weight: bold; } #main a:hover { color: red; }
.funky { font: { family: fantasy; size: 30em; weight: bold; } }编译出来之后
.funky { font-family: fantasy; font-size: 30em; font-weight: bold; }并且,命名空间属性还可以有一个值,像下面的代码一样
.funky { font: 2px/3px { family: fantasy; size: 30em; weight: bold; } }编译之后
.funky { font: 2px/3px; font-family: fantasy; font-size: 30em; font-weight: bold; }
/* 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; }编译之后
/* This comment is * several lines long. * since it uses the CSS comment syntax, * it will appear in the CSS output. */ body { color: black; } a { color: green; }当注释的第一个字符为!时,注释将会插值的形式编译到css中,甚至在压缩输出时也是如此,这个特性对于在CSS中添加版权信息非常有用。
$ sass -i >> "Hello, Sassy World!" "Hello, Sassy World!" >> 1px + 1px + 1px 3px >> #777 + #777 #eeeeee >> #777 + #888 white
//声明变量 $width: 5em; //使用变量 #main{ width:$width; }在嵌套里声明的变量只在声明它的那个嵌套里可用,在嵌套外边声明的变量到处可用。
@mixin firefox-message($selector) { body.firefox #{$selector}:before { content: "Hi, Firefox users!"; } } @include firefox-message(".header");编译之后
body.firefox .header:before { content: "Hi, Firefox users!"; }需要注意的是,当我们使用不推荐的"="语法时,无论书写时有没有引号,所有的字符串都将会认作没有引号。
==
and
!=
),另外,每个数据类型都有单独支持的运算符。
p { width: 1in + 8pt; }将会被编译成
p { width: 1.111in; }Number类型也支持所有的关系运算符(<, >, <=, >=)。
p { font: 10px/8px; // Plain CSS, no division $width: 1000px; width: $width/2; // Uses a variable, does division height: (500px/2); // Uses parentheses, does division margin-left: 5px + 8px/2px; // Uses +, does division }将会编译为
p { font: 10px/8px; width: 500px; height: 250px; margin-left: 9px; }如果你想在变量中使用分隔符(而不是除),可以使用#{}插值,例如
p { $font-size: 12px; $line-height: 30px; font: #{$font-size}/#{$line-height}; }编译之后
p { font: 12px/30px; }
p { color: #010203 + #040506; }01 + 04 = 05, 02 + 05 = 07, 03 + 06 = 09,因此编译之后
p { color: #050709; }颜色和数字之间也可以运算,如下面的代码。大多数情况下,使用color函数要比color算数运算高效一些。
p { color: #010203 * 2; } //编译之后 p{ color:#020406;} }注意,具有alpha通道的颜色值要进行运算必须具有一样的alpha值,运算不对alpha值起作用。
p { color: rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75); } //编译之后 p{ color:rgba(255,255,0,0.75)}alpha通道需要进行调整时,可以使用opacify和 transparentize函数,例如
$translucent-red: rgba(255, 0, 0, 0.5); p { color: opacify($translucent-red, 0.3); background-color: transparentize($translucent-red, 0.25); } //编译之后哦 p { color: rgba(255, 0, 0, 0.9); background-color: rgba(255, 0, 0, 0.25); }IE滤镜需要所有的颜色具备alpha通道,而且严格使用#AABBCCDD的颜色格式,你可以使用ie_hex_str 函数容易的实现转换,例如
$translucent-red: rgba(255, 0, 0, 0.5); $green: #00ff00; div { filter: progid:DXImageTransform.Microsoft.gradient(enabled='false', startColorstr='#{ie-hex-str($green)}', endColorstr='#{ie-hex-str($translucent-red)}'); } //编译之后 div { filter: progid:DXImageTransform.Microsoft.gradient(enabled='false', startColorstr=#FF00FF00, endColorstr=#80FF0000); }
p { cursor: e + -resize; } //编译后 p { cursor: e-resize; }这里需要注意,如果一个引号字符串连接一个非引号字符串(+前面是引号字符串),则结果是一个引号字符串,否则+前面是非引号字符串,结果也将是非引号字符串,例如
p:before { content: "Foo " + Bar; font-family: sans- + "serif"; } //编译之后 p:before { content: "Foo Bar"; font-family: sans-serif; }我们可以在字符串内部使用#{}插值方式动态值,例如
p:before { content: "I ate #{5 + 10} pies!"; } //编译之后 p:before { content: "I ate 15 pies!"; }空的属性值在编译时会被当做空字符串处理
$value: null; p:before { content: "I ate #{$value} pies!"; } //编译后 p:before { content: "I ate pies!"; }
p { width: (1em + 2em) * 3; } //编译之后 p{ width:9em;}
p { color: hsl(0, 100%, 50%); } //编译之后 p { color: #ff0000; }关键字参数
p { color: hsl($hue: 0, $saturation: 100%, $lightness: 50%); }尽管这样看起来不大简洁,却可以提高代码的可读性,提高函数接口的灵活性,避免太多参数引起的调用困难。这些参数可以以任何顺序进行调用,如果有默认值的参数为可选属性可以省略,因为参数名称也是变量名,下划线和虚线不能交叉使用。
$name: foo; $attr: border; p.#{$name} { #{$attr}-color: blue; } //编译之后 p.foo { border-color: blue; }同时,我们也可以在属性值中 使用插值,插值周围的运算符将会被当成普通css处理
p { $font-size: 12px; $line-height: 30px; font: #{$font-size}/#{$line-height}; } //编译之后 p { font: 12px/30px; }
$content: "First content"; $content: "Second content?" !default; $new_content: "First time reference" !default; #main { content: $content; new-content: $new_content; } //编译之后 #main { content: "First content"; new-content: "First time reference"; }如果变量的值为null,SassScript则认为该变量没有被赋值
$content: null; $content: "Non-null content" !default; #main { content: $content; } //编译之后 #main { content: "Non-null content"; }
待续……
前端开发whqet,关注web前端开发技术,分享网页相关资源。
---------------------------------------------------------------