SASS初体验

SASS初体验

标签(空格分隔): sass scss csscss


1. 编译环境 须要安装Ruby,以后须要打开Start Command Prompt with Ruby运行html

gem install sass

2. 命令行编译web

sass <要编译的sass文件路径>/style.scss:<要输出css文件路径>/style.css

多文件编译 (必须用--watch?反正我不加watch就会报错)api

sass --watch sass/:css/

开启watch数组

sass --watch <要编译的Sass文件路径>/style.scss:<要输出CSS文件路径>/style.css

输出方式 --style [nested(末尾花括号不换行)|expanded(彻底展开)|compact(单行)|compressed(压缩)]sass

sass --watch sass/:css/ --style compressed

3. 基本语法ruby

(1). 嵌套app

和less差很少。less

nav {
    color: blue;

    li {
        color: yellow;

        a {
            color: red;

            header & {
                color: green;
            }
        }
    }
}

编译后ide

nav {
  color: blue;
}
nav li {
  color: yellow;
}
nav li a {
  color: red;
}
header nav li a {
  color: green;
}
  • 属性嵌套(相同属性前缀), 并且前缀冒号后能够加属性
.box {
    font: 12px/24px {
        size: 12px;
        weight: bold;
    }
}

编译后

.box { font: 12px/24px; font-size: 12px; font-weight: bold; }
  • 伪类嵌套,和less一毛同样
.clearfix {
    &:before,
    &:after {
        content: "";
        display: table;
    }
    &:after {
        clear: both;
        overflow: hidden;
    }
}

编译后

.clearfix:before, .clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
  overflow: hidden;
}
  • 父选择器&能够做为选择器的第一个字符,好比
.btn {
    padding: 4px 12px;
    font-size: 16px;
    border: 1px solid #ddd;
    color: #333;

    &-primary {
        border-color: #ff5f00;
        background: #ff5f00;
        color: #fff;
    }
}

编译后

.btn, .btn-primary { padding: 4px 12px; font-size: 16px; border: 1px solid #ddd; color: #333; }
.btn-primary { border-color: #ff5f00; background: #ff5f00; color: #fff; }

(2). 注释

/**/会出如今编译后文件中 amazing! //不会

// 方向
/*方向*/
$d: "right";
.box {
    @extend %border-#{$d};
}

/*位置*/

编译后

.box {
  border-right: 2px solid #ddd;
}

/*方向*/
/*位置*/

(3). 变量

$[变量名]: [值] 块级做用域 !global声明能够将局部转变为全局变量 默认变量;普通变量会覆盖默认变量

$size: 16px;
$size: 14px !default;
p.p-1 {
    font-size: $size;
}

编译后 p.p-1{font-size:16px}

(4). 运算

+, -, *, /, % <, >, <=, >= 也可用于数字运算 ==, != 可用于全部数据类型 不一样单位不能做运算 能够进行字符串拼接;且有无引号根据左边的决定 除法须要在数学表达式中,两个普通属性须要用括号括起来,好比

.box {
    width: (100px / 2);
}

编译后

.box {
  width: 50px;
}
  • 插值语句包裹的变量不作除法运算
p {
    $font-size: 12px;
    $line-height: 30px;
    font: #{$font-size}/#{$line-height};
}

编译后

p { font: 12px/30px; }
  • 颜色计算分段(按照红绿蓝分别) 颜色函数 其中fade-in($color, $amount)等方法, color参数只能为rgba()颜色,不一样于less

颜色函数

(5). 混合

  • 用于定义可重复使用的样式 注意语法不带点,参数默认值也同less同样 @mixin [mixin-name]([$param1, $param2: default-value]) { ... } 使用: @include [mixin-name](value1, value2);
  • 对于不定参数,使用 ..., 好比
@mixin box-shadow($shadows...) { 
    -moz-box-shadow: $shadows; 
    -webkit-box-shadow: $shadows; 
    box-shadow: $shadows; 
}

(6). 继承

  • @extend .[class]
  • 还能够继承任何定义给单个元素的选择器,好比@extend a:hover;
.btn {
    border: 1px solid #999;
    padding: 4px 12px;
    font-size: 14px;
    background: #ddd;
    color: #333;
}

.btn-primary {
    background: #ff5f00;
    color: #fff;

    @extend .btn;
}

编译后

.btn, .btn-primary {
  border: 1px solid #999;
  padding: 4px 12px;
  font-size: 14px;
  background: #ddd;
  color: #333;
}

.btn-primary {
  background: #ff5f00;
  color: #fff;
}

占位符% 用占位符声明的代码,不被@extend调用就不会被编译 相一样式的会经过,合在一块儿,减小代码量

%box-padding {
    padding: 4px 12px;
}

.box {
    font-size: 14px;

    @extend %box-padding;
}

.box-2 {
    font-size: 18px;

    @extend %box-padding;
}

编译后

.box, .box-2 {
  padding: 4px 12px;
}

.box {
  font-size: 14px;
}

.box-2 {
  font-size: 18px;
}

(7). 插值

经过 #{} 插值语句能够在选择器或属性名中使用变量 #{$[param]}用法,能够用在@each@extend,多行注释

$border-properties: (border);
@mixin set-border($direction, $val) {
    @each $prop in $border-properties {
        #{$prop}-#{$direction}: $val;
    }
}

.box {
    @include set-border(left, 1px solid #ddd);
}

编译后

.box {
  border-left: 1px solid #ddd;
}
%border-right {
    border-right: 2px solid #ddd;
}

$d: "right";
.box {
    @extend %border-#{$d};
}

编译后

.box {
  border-right: 2px solid #ddd;
}

(8). 导入

  • @import能够导入多个文件,好比@import "rounded-corners", "text-shadow";
  • 导入文件能够经过url()的方式使用插值语句#{},好比@import url("http://fonts.googleapis.com/css?family=\#{$family}");
  • 若是想使一个sass文件只做为导入文件,不进行编译,在文件名前加_便可,好比文件命名为_colors.scss,使用@import "colors";导入,注意文件夹下不能再有colors.scss文件。
  • 能够用在嵌套中,做用域就只在当前嵌套中了,很赞;可是不能够在混合指令 (mixin) 或控制指令 (control directives) 中嵌套 @import。

(9). 媒体查询 @media

  • 用法同css
  • 能够写在嵌套中,编译后将会编译在最外层,且里面的选择器会是嵌套时候的选择器 好比
.sidebar {
    width: 300px;
    @media screen and (orientation: landscape) {
        width: 500px;
    }
}
.sidebar { width: 300px; }
@media screen and (orientation: landscape) { .sidebar { width: 500px; } }
  • media的查询条件可使用插值语句
  • media的查询条件能够嵌套

(10). @at-root

  • 将嵌套的选择器提高到当前文档最顶层, 好比
.parent {
    font-size: 14px;

    @at-root .child-a {
        font-size: 16px;

        @at-root .child-c {
            font-size: 18px;
        }
    }

    .child-b {
        font-size: 12px;
    }
}
.parent { font-size: 14px; }
.child-a { font-size: 16px; }
.child-c { font-size: 18px; }
.parent .child-b { font-size: 12px; }
  • @at-root (without: [directive1 directive2 ...])能够排除前面的指令
  • 括号后面不能有选择器,没有括号必须有选择器
@media .print {
    .page {
        width: 8in;
        @at-root (without: media) {
            color: red;
        }
    }
}
// 没有without
@media print {
    .page {
        width: 8in;
        @at-root .p {
            color: red;
        }
    }
}
@media .print { .page { width: 8in; } }
.page { color: red; }

@media print { .page { width: 8in; }
  .p { color: red; } }

(11). 控制指令

  • 主要与混合指令 (mixin) 配合使用, 这是less中所没有的,less经过其它方式能够实现相似的效果,好比循环,less能够经过递归配合when关键字来实现:.loop(@counter) when (@counter > 0) { .loop((@counter - 1)); }

  • @if 表达式返回值不是 false 或者 null 时,执行 {} 内的样式,一样还有@else if@else

  • @for 语法:@for $var from <start> through <end> 或者 @for $var from <start> to <end> <start><end> 必须为整数 through 包含 <start><end> 的值,而 to 只包含 <start>

  • @each 语法: $var in <list> <list> 值为列表 好比

$arr: a, b, c, d, e;
@each $img in $arr {
    .box-#{$img} {
        background: url('/img/#{$img}.png') no-repeat;
    }
}
.box-a { background: url("/img/a.png") no-repeat; }

.box-b { background: url("/img/b.png") no-repeat; }

.box-c { background: url("/img/c.png") no-repeat; }

.box-d { background: url("/img/d.png") no-repeat; }

.box-e { background: url("/img/e.png") no-repeat; }
  • 能够循环多维数组,好比
$list: (aa, pen), (bb, apple), (cc, bag);
@each $var, $img in $list {
    .box-#{$var} {
        background: url('/img/#{$img}.png') no-repeat;
    }
}
.box-aa { background: url("/img/pen.png") no-repeat; }

.box-bb { background: url("/img/apple.png") no-repeat; }

.box-cc { background: url("/img/bag.png") no-repeat; }

使用map数组或许更为明了:

$list-2: (aaa: yellow, bbb: blue, ccc: red);
@each $key, $color in $list-2 {
    .box-#{$key} {
        background: #{$color};
    }
}
.box-aaa { background: yellow; }

.box-bbb { background: blue; }

.box-ccc { background: red; }
  • @while 循环,语法:@while [conditions] { ... }

(12). 其它

  • @debug 能够输出信息到编译器
  • @warn 将SassScript表达式的值打印到标准错误输出流。
  • @error 抛出SassScript表达式的值做为致命错误
  • @function 自定义函数
@function [function-name]([params]) {
    @return [value];
}

<small style="display: inline-block;background: #1cc333;color: #fff;padding-left: 5px;">The end... <i style="display: inline-block;background: #999;font-style: normal;padding-right: 5px;">    Last updated by: Jehorn, Mar 13, 2018, 12:10 PM</i></small>

相关文章
相关标签/搜索