Sass 是对 CSS 的扩展,让 CSS 语言更强大、优雅。 它允许你使用变量、嵌套规则、mixin、导入等众多功能, 并且完全兼容 CSS 语法。 Sass 有助于保持大型样式表结构良好, 同时也让你能够快速开始小型项目, 特别是在搭配 Compass 样式库一同使用时。但是作为一名自己摸索的前端,初探sass就让我了解到他的强大,但是到现在我最常用的仅仅是嵌套?所以在这里我检讨一下自己,并对、梳理一下常用的sass。

我们公司用的是gulp编译sass,现在我个人习惯于用命令行编译,刚开始的时候也用过Koala编译软件,说实话,个人觉得命令行更简单方便。

一直监控编译,只要有保存更改就会立即编译   sass --watch xx.scss:xx.css

不同样式风格的输出方法

嵌套输出方式 nested    sass --watch test.scss:test.css --style nested

展开输出方式 expanded   sass --watch test.scss:test.css --style expanded

紧凑输出方式 compact   sass --watch test.scss:test.css --style compact

压缩输出方式 compressed  sass --watch test.scss:test.css --style compressed

sass --watch scss:css --style compressed --sourcemap=none 这个就是不生成soucemap文件

这里就拿在项目中用到的几个例子简单的介绍下吧

1.变量

$color:red;

p{

  $color:blue;

color:$color;//blue

}

a{ color:$color;//blue }

2.嵌套规则

&-1{
  left: rem-calc(45);
  top: rem-calc(50);
  &:after{
    $size:rc(290);
    width: $size;
    height: $size;
    background-size: $size $size;
    top:rc(-30);
    left: rc(10)
  }

3.mixin使用(常见css3效果)

@mixin ss-pic($width,$height,$pos-left,$pos-top){
  width: $width;
  height: $height;
  background-position: $pos-left $pos-top;
}

@mixin rounded($radius) {

   -webkit-border-radius: $radius;
   -moz-border-radius: $radius;
   border-radius: $radius;
 }
 @mixin shadow($x, $y, $blur, $color) {
   -webkit-box-shadow: $x $y $blur $color;
   -moz-box-shadow: $x $y $blur $color;
   box-shadow: $x $y $blur $color;
 }
 @mixin shadow-inset($x, $y, $blur, $color) {
  -webkit-box-shadow: inset $x $y $blur $color;
   -moz-box-shadow: inset $x $y $blur $color;
   box-shadow: inset $x $y $blur $color;
 }
@mixin transition($property) {
   -webkit-transition: $property .2s ease;
   -moz-transition: $property .2s ease;
   -o-transition: $property .2s ease;
   transition: $property .2s ease;
 }
 @mixin box-sizing {
   -webkit-box-sizing: border-box;
   -moz-box-sizing: border-box;
   box-sizing: border-box;
 }
 @mixin linear-gradient($from, $to) {
   /* Fallback for sad browsers */
  background-color: $to;
   /* Mozilla Firefox */
   background-image:-moz-linear-gradient($from, $to);
   /* Opera */
   background-image:-o-linear-gradient($from, $to);
   /* WebKit (Chrome 11+) */
   background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, $from), color-stop(1, $to));
  /* WebKit (Safari 5.1+, Chrome 10+) */
  background-image: -webkit-linear-gradient($from, $to);
   /* IE10 */
   background-image: -ms-linear-gradient($from, $to);
  /* W3C */
   background-image: linear-gradient($from, $to);
 }

@mixin gra($begin,$end){
  zoom: 1;
  background-image: -webkit-gradient(linear, left top, left bottom, from($begin), to($end));
  background-image: -webkit-linear-gradient(top, $begin, $end);
  background-image: -moz-linear-gradient(top, $begin, $end);
  background-image: -ms-linear-gradient(top, $begin, $end);
  background-image: -o-linear-gradient(top, $begin, $end);
  background-image: linear-gradient(top, $begin, $end);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr="#{ie-hex-str($begin)}", EndColorStr="#{ie-hex-str($end)}");
}

@mixin opacityColor($color,$trans){
  $rgba: rgba($color, $trans);
  background: $rgba;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr="#{ie-hex-str($rgba)}", EndColorStr="#{ie-hex-str($rgba)}");
  .ie9 &{
    filter: none;
  }
}

@mixin opacityImage($path){
  _background: none;
  _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=noscale, src="#{$path}");
}

@mixin opacityGif($path){
  _background-image: url("#{$path}");
}

@mixin stretchedImage($path){
  background-size: 100% 100%;
  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{$path}", sizingMethod="scale");
  -ms-filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{$path}", sizingMethod="scale");
}

@mixin rotate($degrees){
  zoom: 1;
  //-ms-filter: progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)});
  //filter: progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=#{cos($degrees)}, M12=-#{sin($degrees)}, M21=#{sin($degrees)}, M22=#{cos($degrees)});
  -moz-transform: rotate($degrees);
  -o-transform: rotate($degrees);
  -webkit-transform: rotate($degrees);
  -ms-transform: rotate($degrees);
  transform: rotate($degrees);
}

@mixin scale($x, $y){
  zoom: 1;
  -moz-transform: scale($x, $y);
  -o-transform: scale($x, $y);
  -webkit-transform: scale($x, $y);
  -ms-transform: scale($x, $y);
  transform: scale($x, $y);
}

@mixin flexbox(){
  display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
  display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */
  display: -ms-flexbox; /* TWEENER - IE 10 */
  display: -webkit-flex; /* NEW - Chrome */
  display: flex;
}

@mixin flexboxChild($num:1){
  -webkit-box-flex: $num; /* OLD - iOS 6-, Safari 3.1-6 */
  -moz-box-flex: $num; /* OLD - Firefox 19- */
  -webkit-flex: $num; /* Chrome */
  -ms-flex: $num; /* IE 10 */
  flex: $num;
  width:0;
  display:block; /* fix input Bug */
}

@mixin boxShadow($param){
  -moz-box-shadow: $param;
  -webkit-box-shadow: $param;
  box-shadow: $param;
}

@mixin boxShadowParameters($xNum,$yNum,$blurNum,$color,$style...){
  -webkit-box-shadow: $xNum $yNum $blurNum $color $style;
  -moz-box-shadow: $xNum $yNum $blurNum $color $style;
  -ms-box-shadow: $xNum $yNum $blurNum $color $style;
  -o-box-shadow: $xNum $yNum $blurNum $color $style;
  box-shadow: $xNum $yNum $blurNum $color $style;
}

@mixin hack($name, $value){
  -moz-#{$name}: $value;
  -webkit-#{$name}: $value;
  #{$name}: $value;
}

@mixin horizontalCenter{
  @include hack(box-align, center);
  @include hack(justify-content, center);
}

@mixin verticalCenter{
  @include hack(box-pack,center);
  @include hack(align-items,center);
}

@mixin horizontalCenterNew{
  @include hack(box-pack,center);
  @include hack(justify-content, center);
}

@mixin verticalCenterNew{
  @include hack(box-align, center);
  @include hack(align-items,center);
}

@mixin setTransition($style,$time,$function:linear,$delay:0s){
  -webkit-transition:$style $time $function $delay;
  -moz-transition:$style $time $function $delay;
  -o-transition:$style $time $function $delay;
  transition:$style $time $function $delay;
}

a,li,input,button,section,span,div{
  -webkit-tap-highlight-color: unquote('rgba(0,0,0,0)');
  -moz-tap-highlight-color: unquote('rgba(0,0,0,0)');
  -o-tap-highlight-color: unquote('rgba(0,0,0,0)');
  -ms-tap-highlight-color: unquote('rgba(0,0,0,0)');
  tap-highlight-color: unquote('rgba(0,0,0,0)');
}

.ss-pic{
  $width:rc(215);
  $height:rc(195);
  $pos-left:rc(-242);
  $pos-top:0;
  @include ss-pic($width,$height,$pos-left,$pos-top);
}

4.default

$imageVersion : true !default; // 开启图片版本号
$version : 20171206 !default; // 设置总版本号 > [单独频道可独立设置$version]
$absPath : false !default; // 开启绝对路径

$bodyBgColor : #be1008 !default; // 设置body背景色
$bodyColor : #000 !default; // 设置body字体颜色
$font-family : sans-serif; // 设置文字字体

5.extend和%

%fl {
  float: left;
  _display: inline;
}

%fr {
  float: right;
  _display: inline;
}

%dis-inb {
  display: inline-block;
  *display: inline;
  *zoom: 1;
  vertical-align: middle;
}

.icon{
  @extend %dis-inb;
  overflow: hidden;
}

6.function函数

$img:'../../../images/activity/2018newyear';
$version:'?20171206';

/// Asset URL builder
@function asset($type, $file) {
  @return url($img + $type + '/' + $file + $version);
}

/// Image asset helper
@function images($file) {
  @return asset('', $file);
}

p{background: images('shenshou.png') no-repeat;}

sass是很强大的,大家不要因为习惯与css的编写方式就不去探索它,希望自己以后可以养成良好的sass编译习惯,而不是简单的拘泥于变量嵌套~

如何使用sass的更多相关文章

  1. wepack+sass+vue 入门教程(三)

    十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...

  2. wepack+sass+vue 入门教程(二)

    六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...

  3. wepack+sass+vue 入门教程(一)

    一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...

  4. 前端CSS预处理器Sass

    前面的话   "CSS预处理器"(css preprocessor)的基本思想是,用一种专门的编程语言,进行网页样式设计,然后再编译成正常的CSS文件.SASS是一种CSS的开发工 ...

  5. SASS教程sass超详细教程

    SASS安装及使用(sass教程.详细教程) 采用SASS开发CSS,可以提高开发效率. SASS建立在Ruby的基础之上,所以得先安装Ruby. Ruby的安装: 安装 rubyinstaller- ...

  6. Sass之坑Compass编译报错

    前段时间在使用Compass时遇到了其为难处理的一个坑,现记录到博客希望能帮助到各位. 一.问题: 利用Koala或者是gulp编译提示如下,截图为koala编译提示错误: 二.解决办法 从问题截图上 ...

  7. emmet,jade,haml, slim,less,sass,coffeescript等的实战优缺点

    摘要: 文章背景,来自于群内周五晚上的一次头脑风暴式的思维碰撞交流活动. 随着前端技术的蓬勃发展, 各种新技术随着生产力的需要不断的涌入我们的视野, 那今天探讨的话题是这些新时代的前端兵器谱: 一. ...

  8. Sass用法指南

    写在前面的话:随着CSS文件越来越大,内容越来越复杂,对其进行很好的维护将变的很困难.这时CSS预处理器就能够帮上大忙了,它们往往拥有变量.嵌套.继承等许多CSS不具备的特性.有很多CSS预处理器,这 ...

  9. PostCSS深入学习: PostCSS和Sass、Stylus或LESS一起使用

    如果你喜欢使用PostCSS,但又不想抛弃你最喜欢的预处理器.不用担心,你不需要作出二选一的选择,你可以把PostCSS和预处理器(Sass.Stylus或LESS)结合起来使用. 有几个PostCS ...

  10. Sass:初识Sass与Koala工具的使用

    一.下载Koala(找到合适的系统版本)并安装 二.先新建一个css文件夹,并在里面新建一个文本文档(.txt),将其命名为demo.scss 三.打开Koala,将css文件夹拽进来,可以修改一下输 ...

随机推荐

  1. 9.2 NOIP提高组试题精解(2)

    9-18 fruit.c #include <stdio.h> #define MAXN 10000 int Queue1[MAXN], Queue2[MAXN]; void Insert ...

  2. 常见的25个python面试问答

    常见的25个python面试问答 说到好用简洁的大数据技术,除了Hadoop.R等等,Python也是其中熠熠生辉的一员,因而广受企业和商家的青睐.求职季,不少应聘者在面试相关职业时都被要求掌握Pyt ...

  3. 分享知识-快乐自己: Oracle数据库实例、用户、表、表空间之间关系

    数据库: Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件). 其实Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库 ...

  4. ES搜索排序,文档相关度评分介绍——TF-IDF—term frequency, inverse document frequency, and field-length norm—are calculated and stored at index time.

    Theory Behind Relevance Scoring Lucene (and thus Elasticsearch) uses the Boolean model to find match ...

  5. September Challenge 2017

    Little Chef and Sums 分析:水题,去维护一下前缀和以及后缀和就好,注意long long #include "iostream" #include " ...

  6. 微信小程序之tab切换

    .wxml <view class="select_box"> <scroll-view scroll-x="true" style=&quo ...

  7. CentOS7的网络配置

    1.DNS配置 新安装的虚拟机,ping 内网IP可以,但是ping 外网域名却失败,告知“Name or service not known”. 原来是因为需要在/etc/sysconfig/net ...

  8. 51nod 1218 最长递增子序列 V2——LIS+思路(套路)

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...

  9. VijosP1250:分组背包

    背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n个物品,每个物品都有一个价 ...

  10. xml解析中的sax解析

    title: xml解析中的sax解析 tags: grammar_cjkRuby: true --- SAXPasser 类: parser(File file, DefaultHandler ha ...