Sass快速入门

用过CSS3的都知道,CSS3没有变量,也不支持任何逻辑脚本。人总是很聪明的,随着需求的增加,Sass诞生,目的在于使样式支持逻辑脚本,使程序猿们更高效的写样式,Sass脚本经过编译后,最终会转换成.css文件。Sass脚本有两种格式,分别是.sass和.scss,这两种格式的区别在于:.sass不使用大括号和分号。由于.scss的写法与.css更加相似,所以通常我们都使用.scss格式。本篇博客将从Sass安装、Sass编译到Sass语法应用逐步带程序猿们快速掌握Sass。

Sass安装

因为sass依赖于ruby环境,所以装sass之前先确认装了ruby,安装ruby的作用在于:它可以安装和更新sass。ruby官网下载地址:http://rubyinstaller.org/downloads/

在安装的时候,请勾选Add Ruby executables to your PATH这个选项,添加环境变量,这样在安装成功后就可以直接使用命令行使用ruby。安装完成后需测试安装有没有成功,可运行CMD输入命令:

$ ruby -v

如能正确显示ruby版本号,表明安装成功。由于国内对外网有所限制,接着需要安装gem源的淘宝镜像,目的在于保证顺利下载Sass等资源。运行CMD输入命令:

  1. //1.删除原gem源
  2. $ gem sources --remove https://rubygems.org/
  3. //2.添加国内淘宝源
  4. $ gem sources -a https://ruby.taobao.org/
  5. //3.打印是否替换成功
  6. $ gem sources -l
  7. //4.更换成功后打印如下
  8. *** CURRENT SOURCES ***
  9. https://ruby.taobao.org/

如果您系统不支持https,请将淘宝源更换成:gem sources -a http://gems.ruby-china.org。至此,所有准备工作就绪,开始安装Sass,安装命令行如下:

  1. $ gem install sass
  2. $ gem install compass
安装成功后,同样可通过sass -v检测是否安装成功,若能显示版本号则表示安装成功。

Sass编译

这步是实现.scss向.css格式转换的关键。进入到编译文件所在的目录底下,编译命令如下:
$ sass input.scss output.css
如需实现动态编译(只要.scss改变就会马上编译成.css文件),编译命令如下:
$ sass --watch input.scss:output.css

如果是多文件编译,编译对象也可以是文件目录:

$ sass --watch app/sass:public/stylesheets
编辑出来的文件有四种排版可配置,分别是nested、expanded、compact和compressed。以expanded为例,配置时只需在命令行末尾加入--style expanded即可。
$ sass --watch input.scss:output.css --style expanded

这四种排版的区别程序猿们可以编译后自己对比一下,或直接去官网查看,在实际开发过程中,较常用的是expanded和compressed排版。当然,如果对命令行没有操作习惯,还可以使用编译工具koala,官网下载地址:http://koala-app.com/index-zh.html.


Sass语法

——技巧——

(1)在.scss开头加上@charset "utf-8";这样编译时遇到中文便不会出错;

(2)命名变量时常用到横杆或下横岗,如$link-color和$link_color,在编译时这两者并没有区别,指向的是同一对象。


1.变量

变量用$符号定义,如$fontSize: 20px;变量的好处在于一次定义多次使用,免去重复写相同的代码。变量的格式分为普通变量、默认变量、特殊变量、多值变量和对象变量。

(1)普通变量,最简单的变量使用方法。

  1. @charset "utf-8";
  2. //编译前.scss
  3. $fontSize: 20px;
  4. div {
  5. font-size: $fontSize;
  6. }
  7. //编译后.css
  8. div {
  9. font-size: 20px;
  10. }
(2)默认变量,在普通变量后加上!default,目的在于强调变量用意,可修改,常用于组件的入参定义。
  1. //编译前.scss
  2. $fontSize: 20px ! default;
  3. div {
  4. font-size: $fontSize;
  5. }
  6. //编译后.css
  7. div {
  8. font-size: 20px;
  9. }

(3)特殊变量,通过 #{变量名称} 的方式,可引用为样式名称。

  1. //编译前.scss
  2. $direction: top ! default;
  3. div {
  4. border-#{$direction}: 1px solid red;
  5. }
  6. //编译后.css
  7. div {
  8. border-top: 1px solid red;
  9. }
(4)多值变量,该种变量可赋多个值,值之间用空格隔开,引用时通过nth(多值变量,索引);的方式获取值内容,索引从1开始计数。
  1. //编译前.scss
  2. $linkColor: red blue;
  3. div {
  4. cursor: pointer;
  5. color: nth($linkColor, 1);
  6. //此处&表示this,即自身div。
  7. &:hover {
  8. color: nth($linkColor, 2);
  9. }
  10. }
  11. //编译后.css
  12. div {
  13. cursor: pointer;
  14. color: red;
  15. }
  16. div:hover {
  17. color: blue;
  18. }
(5)对象变量,指用(key1:value1, key2:value2)的形式定义变量。通过map-get($map,$key)获取内容值,除此之外,还有map-merge($map1,$map2), map-keys($map), map-values($map)等方法。
  1. //编译前.scss
  2. $mapColor:( "color1":red, "color2":blue);
  3. div{
  4. cursor: pointer;
  5. color: map-get($mapColor, "color1");
  6. &:hover {
  7. color: map-get($mapColor, "color2");
  8. }
  9. }
  10. //编译后.css
  11. div {
  12. cursor: pointer;
  13. color: red;
  14. }
  15. div:hover {
  16. color: blue;
  17. }


2.嵌套

(1)选择器嵌套,最常用的嵌套语法,是指当描述多层级元素样式时,子层级元素可嵌套到父层级元素内,这种语法极大地优化了样式代码的可读性。

  1. //编译前.scss
  2. ul {
  3. list-style: none;
  4. li {
  5. float: left;
  6. a {
  7. color: red;
  8. &:hover {
  9. color: yellow;
  10. }
  11. }
  12. }
  13. }
  14. //编译后.css
  15. ul {
  16. list-style: none;
  17. }
  18. ul li {
  19. float: left;
  20. }
  21. ul li a {
  22. color: red;
  23. }
  24. ul li a:hover {
  25. color: yellow;
  26. }
(2)群组嵌套,是指嵌套元素时每次可嵌套多个元素,嵌套的元素用逗号隔开。
  1. //编译前.scss
  2. .container {
  3. h1, h2, h3 {margin-bottom: .8em}
  4. }
  5. nav,aside {
  6. a {
  7. color: blue
  8. }
  9. }
  10. //编译后.css
  11. .container h1, .container h2, .container h3 {
  12. margin-bottom: .8em;
  13. }
  14. nav a, aside a {
  15. color: blue;
  16. }
(3)属性嵌套,主要用于具有类似相同属性名称的元素嵌套,注意与其它嵌套写法的差别,在嵌套时大挂弧前会多一个冒号,由于这种嵌套方法不利于可读性,建议尽量少用。
  1. //编译前.scss
  2. div {
  3. border: {
  4. style: solid;
  5. left: {
  6. color: red;
  7. }
  8. right: {
  9. color: yellow;
  10. }
  11. }
  12. }
  13. //编译后.css
  14. div {
  15. border-style: solid;
  16. border-left-color: red;
  17. border-right-color: yellow;
  18. }

(4)样式嵌套,是指样式与样式间的嵌套,注意样式嵌套时带空格与不带空格的区别:即子级与同级的区别。

  1. //编译前.scss
  2. .list {
  3. border: solid 1px red;
  4. &.item {
  5. border: solid 1px yellow;
  6. }
  7. & .item {
  8. border: solid 1px yellow;
  9. }
  10. }
  11. //编译后.css
  12. .list {
  13. border: solid 1px red;
  14. }
  15. .list.item {
  16. border: solid 1px yellow;
  17. }
  18. .list .item {
  19. border: solid 1px yellow;
  20. }
(5)跳出嵌套@at-root,用于定义有相同开头的系列样式,但又不在嵌套内。
  1. //编译前.scss
  2. .block {
  3. color: red;
  4. @at-root #{&}__element {
  5. color: yellow;
  6. }
  7. @at-root #{&}__modifier {
  8. color: orange;
  9. }
  10. }
  11. //编译后.css
  12. .block {
  13. color: red;
  14. }
  15. .block__element {
  16. color: yellow;
  17. }
  18. .block__modifier {
  19. color: orange;
  20. }


3.导入

在开发大型项目时,往往会有一些公共样式文件,该类文件可以通过@import实现导入。公共样式文件命名规则是用下横杠_,如_common.scss,导入时无需写后缀名和下横杠,导入可用如下语句:
@import "common";


4.mixin
minin,是指样式混合,此语法的主要用途在于实现共享样式,用@mixin声明的变量可以作为参数传递,使用@include引用。当没有参数时,@mixin的用法如下
  1. //编译前.scss
  2. @mixin block{
  3. color: red;
  4. }
  5. .demo{
  6. @include block;
  7. }
  8. //编译后.css
  9. .demo {
  10. color: red;
  11. }
上例中,.demo通过包含的方式,将@mixin定义的block对象样式引入到了自身。要注意的是,有这样一种情况,在.demo中再次定义color属性(注意,block对象中的color:red脚本是一定会加入到.demo中),那么具体是取@mixin定义的block对象样式还是.demo自己定义的color,取决于自身color和@include引入之间的脚本顺序。@mixin还可以实现带参,用法如下
  1. //编译前.scss
  2. @mixin param($opa){
  3. opacity: $opa;
  4. }
  5. .opa{
  6. @include param( 0.5);
  7. }
  8. //编译后.css
  9. .opa {
  10. opacity: 0.5;
  11. }
配合@content用法,可以用来解决css3的@media等带来的问题。它可以使 @mixin接受一整块样式,接受的样式从@content开始,用法如下:
  1. //编译前.scss
  2. @mixin max-screen($res) {
  3. @media only screen and ( max-width: $res) {
  4. @content;
  5. }
  6. }
  7. @include max-screen( 480px) {
  8. body {
  9. color: red
  10. }
  11. }
  12. //编译后.css
  13. @media only screen and (max-width: 480px) {
  14. body {
  15. color: red;
  16. }
  17. }


5.继承
继承分为普通继承和占位符继承,它们之前的区别在于使用占位符继承时,被继承的父结构的样式将不会被编译出来。普通继承用例如下:
  1. //编译前.scss
  2. .h {
  3. border: 4px solid #ff9aa9;
  4. }
  5. .speaker {
  6. @extend .h;
  7. border-width: 2px;
  8. }
  9. //编译后.css
  10. .h, .speaker {
  11. border: 4px solid #ff9aa9;
  12. }
  13. .speaker {
  14. border-width: 2px;
  15. }
此时.h的样式会被编译出来。如不想把父结构样式编译出来,可使用占位符继承,在父结构命名前加上百分号%
  1. //编译前.scss
  2. %h {
  3. border: 4px solid #ff9aa9;
  4. }
  5. .speaker {
  6. @extend %h;
  7. border-width: 2px;
  8. }
  9. //编译后.css
  10. .speaker {
  11. border: 4px solid #ff9aa9;
  12. }
  13. .speaker {
  14. border-width: 2px;
  15. }
(注)mixin与继承实现的目的是一样的,都是为了共享样式,只是写法不同。但在实际应用中,尽可能使用mixin来实现共享样式,据官网表示,接下来的Sass版本很可能会去掉继承的语法。

6.函数
函数是指通过@function定义一个函数实现加减乘除的逻辑算法,并通过@return返回。如实现单位的转换:
  1. //编译前.scss
  2. $baseFontSize: 16px ! default;
  3. @ function pxToRem($px) {
  4. @ return $px / $baseFontSize * 1rem;
  5. }
  6. .test1 {
  7. font-size: $baseFontSize;
  8. }
  9. .test2 {
  10. font-size: pxToRem( 16px);
  11. }
  12. //编译后.css
  13. .test1 {
  14. font-size: 16px;
  15. }
  16. .test2 {
  17. font-size: 1rem;
  18. }


7.四则运算
表示涉及带数值的样式设置时,可直接进行加减乘除运算。
  1. //编译前.scss
  2. $num1: 10px ! default;
  3. $num2: 5px ! default;
  4. $num3: 10 ! default;
  5. $num4: 5 ! default;
  6. .jia{
  7. font-size: $num1 + $num2;
  8. }
  9. .jian{
  10. font-size: $num1 - $num2;
  11. }
  12. .cheng{
  13. font-size: ($num3 * $num4)+px;
  14. }
  15. .chu{
  16. font-size: ($num3 / $num4)+px;
  17. }
  18. //编译后.css
  19. .jia {
  20. font-size: 15px;
  21. }
  22. .jian {
  23. font-size: 5px;
  24. }
  25. .cheng {
  26. font-size: 50px;
  27. }
  28. .chu {
  29. font-size: 2px;
  30. }
(注)运算乘除时需先把单位拿出来,否则会编译失败。

8.条件判断

条件判断有@if @else和三目判断两种,@if @else用法如下:

  1. //编译前.scss
  2. $show: true;
  3. $type: car;
  4. .test1 {
  5. @ if $show {
  6. opacity: 1;
  7. }
  8. @ else {
  9. opacity: 0;
  10. }
  11. }
  12. .test2 {
  13. @ if $type==bus {
  14. color: yellow;
  15. }
  16. @ else if $type==car {
  17. color: red;
  18. }
  19. @ else {
  20. color: blue;
  21. }
  22. }
  23. //编译后.css
  24. .test1 {
  25. opacity: 1;
  26. }
  27. .test2 {
  28. color: red;
  29. }
三目判断用法如下:
  1. //编译前.scss
  2. .test3 {
  3. font-size: if( false, 1px, 2px);
  4. }
  5. //编译后.css
  6. .test3 {
  7. font-size: 2px;
  8. }


9.循环

循环主要使用的关键字有@for和@each。先来介绍@for,这种循环并不常用,因为这种循环并不支持入参,只简单实现1、2、3...这样的排序循环,主要语法有from+through和form+to,区别在于to并不包括最后的边界值。如
  1. //编译前.scss
  2. @ for $i from 1 through 3 {
  3. .through-#{$i} {
  4. width: 2em * $i;
  5. }
  6. }
  7. @ for $i from 1 to 3 {
  8. .to-#{$i} {
  9. width: 2em * $i;
  10. }
  11. }
  12. //编译后.css
  13. .through -1 {
  14. width: 2em;
  15. }
  16. .through -2 {
  17. width: 4em;
  18. }
  19. .through -3 {
  20. width: 6em;
  21. }
  22. .to -1 {
  23. width: 2em;
  24. }
  25. .to -2 {
  26. width: 4em;
  27. }
@each循环则较为常用,它的入参可以是一维数组、二维数组或map对象格式。
  1. //一维数组
  2. $arr: a, b, c;
  3. //二维数组
  4. $arrs: (a1, b1, c1),(a2, b2, c2);
  5. //map对象格式
  6. $map: (key1: value1, key2: value2);
一维数组用法:
  1. //编译前.scss
  2. $animal-list: puma,sea-slug,egret,salamander;
  3. @each $animal in $animal-list {
  4. .#{$animal}-icon {
  5. background-image: url( '/images/#{$animal}.png');
  6. }
  7. }
  8. //编译后.css
  9. .puma-icon {
  10. background-image: url( "/images/puma.png");
  11. }
  12. .sea-slug-icon {
  13. background-image: url( "/images/sea-slug.png");
  14. }
  15. .egret-icon {
  16. background-image: url( "/images/egret.png");
  17. }
  18. .salamander-icon {
  19. background-image: url( "/images/salamander.png");
  20. }
二维数组用法:
  1. //编译前.scss
  2. $animal-data: (puma, black, default),(sea-slug, blue, pointer),(egret, white, move);
  3. @each $animal,$color,$cursor in $animal-data {
  4. .#{$animal}-icon {
  5. background-image: url( '/images/#{$animal}.png');
  6. border: 2px solid $color;
  7. cursor: $cursor;
  8. }
  9. }
  10. //编译后.css
  11. .puma-icon {
  12. background-image: url( "/images/puma.png");
  13. border: 2px solid black;
  14. cursor: default;
  15. }
  16. .sea-slug-icon {
  17. background-image: url( "/images/sea-slug.png");
  18. border: 2px solid blue;
  19. cursor: pointer;
  20. }
  21. .egret-icon {
  22. background-image: url( "/images/egret.png");
  23. border: 2px solid white;
  24. cursor: move;
  25. }
map对象格式用法:
  1. //编译前.scss
  2. $headings: (h1: 2em, h2: 1.5em, h3: 1.2em);
  3. @each $header,$size in $headings {
  4. #{$header} {
  5. font-size: $size;
  6. }
  7. }
  8. //编译后.css
  9. h1 {
  10. font-size: 2em;
  11. }
  12. h2 {
  13. font-size: 1.5em;
  14. }
  15. h3 {
  16. font-size: 1.2em;
  17. }
至此,本篇博客有关Sass的知识已经介绍完毕,现在有没有发现Sass并没有想象中那么困难,开始您的Sass编程之路吧,让我们一起来高效编写CSS脚本。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值