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. laravel 在apache或nginx的配置

    laravel 下载后,如何运行起来呢,根据自己的应用,记录了几个关键点: 1.apache 配置: 打开http.conf文件,将mod_rewrite前面的#去掉(启用重写模块): 2.nginx ...

  2. codeforces 558C C. Amr and Chemistry(bfs)

    题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. 解决Linux Kettle出现闪退问题

    linux环境, 运行sh spoon.sh打开图形化界面时经常出现闪退情况. 报错信息如下: cfgbuilder - Warning: The configuration parameter [o ...

  4. 《java编程思想》:异常丢失

    finally子句的不恰当使用,会造成异常的丢失,此处列举两种典型的错误使用示例.编程中要避免这种情况 示例一: try{ throw new ExceptionA(); }finally{ thro ...

  5. C++中两个类相互包含引用问题

    在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { int i; B b; } class B { in ...

  6. BaseAdapter/AsyncTask/..等等细节

    BaseAdapter中的getCount之类的函数,是在constructor之后才启动的.这印证了构造函数的优先级是max的. 图片 这一点的意义在于,当你想给getCount返回一个具体参数的时 ...

  7. wpf如何获取control template里的元素

    wpf中的控件模板里的元素有自己独立的命名域. 因此不能通过FindName来根据x:Name来查找子节点. 自己写了一个方法, 通过可视树遍历子节点,然后匹配名字. 如下: private stat ...

  8. cassandra根据用户名密码登录cqlsh

     修改conf目录下cassandra.yaml文件 authenticator: PasswordAuthenticator //将authenticator修改为PasswordAuthentic ...

  9. oracle练习题 实验一

    实验一 练习1.请查询表DEPT中所有部门的情况. select * from dept; 练习2.查询表DEPT中的部门号.部门名称两个字段的所有信息. select deptno,dname fr ...

  10. Elasticsearch5.X Mapping详解

    0.引言 在关系型数据库如Mysql中,设计库表需要注意的是: 1)需要几个表: 2)每个表有哪些字段: 3)表的主键及外键的设定——便于有效关联. 表的设计遵守范式约束,考虑表的可扩展性,避免开发后 ...