转载请注明出处: http://qiudeqing.com/css3/2015/06/16/sass.html

sass 官网

安装

mac: 可能需要设置ruby本地镜像:https://ruby.taobao.org/

  1. sudo gem install sass
  2. sass -v

gulp集成

安装插件:

npm install gulp-sass --save-dev

gulpfile配置:

var gulp = require('gulp');
var sass = require('gulp-sass');

gulp.task('sass', function () {
  gulp.src('./style/**/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./style'));
});

gulp.task('sass:watch', function () {
  gulp.watch('./style/**/*.scss', ['sass']);
});

map

键值对, 类似js对象, 通过map-functions维护, 也可以用@each迭代

$colors: (
    brand: #f03,
    brand-hover: #f00,
    border: #eaeaea,
    primary: #333
);

.nav {
    color: map-get($colors, brand);
}

@each $key, $value in $colors {
    .color-#{$key} {
        color: $value;
    }
}

for循环

$grids = 12;

@for $i from 1 through $grids {
    .col-md-#{$i} {
        width: ($i / $grids) * 100%;
    }
}

@for $i from 1 to $grids {
    .col-sm-#{$i} {
        width: ($i / $grids) * 100%;
    }
}

from a through b遍历[a, b]

from a to b遍历[a, b)

mixin

mobile first的mini栅格系统

// breakpoints map
$breakpoints: (
  xs: 32rem,
  sm: 48rem,
  md: 72rem,
  lg: 96rem,
  xl: 102rem,
  xx: 120rem
);

// media query mixin
@mixin break($size) {
  @media (min-width: map-get($breakpoints, $size)) {
    @content;
  }
}

// number of columns variable
$items: 12;

// grid container
.grid {
  display: flex;
  flex-flow: row wrap;
  margin-bottom: 1rem;
}

// loop over the breakpoints
@each $key, $value in $breakpoints {
  @for $i from 1 through $items {
    .grid__item--#{$key}-span-#{$i} {
      flex: 0 0 100%;
      @include break($key) {
        flex: 0 0 #{$i / $items * 100%};
      }
    }
  }
}

Node Sass does not yet support your current environment: OS X 64-bit with Unsupported runtime (57)

重新编译npm rebuild node-sass