当前位置:Gxlcms > css > 邂逅Sass和Compass之Sass篇

邂逅Sass和Compass之Sass篇

时间:2021-07-01 10:21:17 帮助过:8人阅读

对于一个从后台转到前端的web开发者来说,最大的麻烦就是写CSS,了解CSS的人都知道,它可以开发网页样式,但是没法用它编程,感觉耦合性相当的高,如果想要方便以后维护,只能逐句修改甚至重写相当一部分的CSS。随着后台人员大量的涌入前端这个行业,CSS又焕发了新的春天,人们开始为CSS加入编程元素,也就是“CSS预处理器”。它的基本思想就是用一门专门的编程语言,进行网页样式设计,然后再编译成正常的CSS文件。下面我们结合官方文档和阮一峰老师的用法指南,再次系统的总结一下Sass的主要用法。

1.Sass的安装

Sass是Ruby语言写的,我们在安装Sass之前必须先安装Ruby,安装Ruby的过程就不再赘述,安装好Ruby后可以直接在命令行(windows在cmd中)输入下面的命令

  1. gem install sass

2.Sass的使用

Sass文件就是普通的文本文件,后缀名有.sass和.scss两种。 后缀名为.sass的文件编码风格不需要添加大括号,但需要处理缩进等操作,如下

  1. *
  2. margin:0;
  3. padding:0;

后缀名为.scss的文件编码风格偏向于正常的CSS风格如下:

  1. *{
  2. margin:0;
  3. padding:0;
  4. }

Sass官方都会对两者风格平行更新,所以喜欢哪种风格还是看个人的爱好,由于本人偏向于.scss文件后缀,所以下面的例子都会基于.scss文件风格。

下面的命令,可以在屏幕上显示.scss文件转化的css(假设文件名为test)

  1. sass test.scss

如果要把.scss文件直接转化为css文件,后面需要再跟.css的文件名

  1. sass test.scss test.css

Sass提供了四种编译风格的选项

  1. * nested: 嵌套缩进的css的代码,默认
  2. * expanded:没有缩进、扩展的css代码
  3. * compact:简介格式的css代码
  4. * compressed:压缩后的css代码

我们可以用如下命令切换编译风格

  1. sass --style compressed test.scss test.css

我们也可以让Sass监听某个文件或目录,一旦源文件发生变动,就自动生成编译后的版本

  1. // watch a file
  2. sass --watch test.scss:test.css
  3. // watch a direcoty
  4. sass --watch app/sass:public/stylesheets

如果有使用webStrom开发的同学可以添加File Watchers,但是我感觉不如开启整个目录监听的好,有一点需要注意的就是不同的批处理对应不同的的后缀名文件即sass.bat对应.sass文件、scss.bat对应.scss文件

3.Sass的基本用法

3.1 导入

Sass的导入(@import)规则和CSS的有所不同,让我们回顾一下CSS@import导入的缺点:CSS在@import一个文件的时候,它不会立即导入,只有在引用它的那个css文件被下载、解析之后,浏览器才会知道还有另外一个css需要下载,这时才去下载,然后下载后开始解析、构建render tree等一系列操作,从而导致css无法并行下载。但是Sass的导入(@import),编译时会将@import的scss文件合并进来只生成一个CSS文件。但是如果你在sass文件中导入css文件如@import 'reset.css',那效果跟普通CSS导入样式文件一样,导入的css文件不会合并到编译后的文件中,而是以@import方式存在。

所有的sass导入文件都可以忽略后缀名.scss。一般来说基础的文件命名方法以开头,如mixin.scss。这种文件在导入的时候可以不写下划线,可写成@import "mixin"。

被导入sass文件a.scss:

  1. //a.scss
  2. //-------------------------------
  3. body {
  4. background: #eee;
  5. }

需要导入样式的sass文件b.scss:

  1. @import "reset.css";
  2. @import "a";
  3. p{
  4. background: #0982c1;
  5. }

转译出来的b.css样式:

  1. @import "reset.css";
  2. body {
  3. background: #eee;
  4. }
  5. p{
  6. background: #0982c1;
  7. }

根据上面的代码可以看出,b.scss编译后,reset.css继续保持import的方式,而a.scss则被整合进来了。

3.2 注释

sass有两种注释方式,一种是标准的css注释方式/* */,另一种则是//双斜杆形式的单行注释,不过这种单行注释不会被转译出来。 标准的css注释

  1. /*
  2. *我是css的标准注释
  3. *设置body内距
  4. */
  5. body{
  6. padding:5px;
  7. }

双斜杆单行注释 单行注释跟JavaScript语言中的注释一样,使用又斜杠(//),但单行注释不会输入到CSS中。

  1. //我是双斜杠表示的单行注释
  2. //设置body内距
  3. body{
  4. padding:5px; //5px
  5. }

3.3 变量

sass的变量必须是$开头,后面紧跟变量名,而变量值和变量名之间就需要使用冒号(:)分隔开(就像CSS属性设置一样),如果值后面加上!default则表示默认值。

3.3.1 普通变量

定义之后可以在全局范围内使用。

  1. //sass style
  2. //-------------------------------
  3. $fontSize: 12px;
  4. body{
  5. font-size:$fontSize;
  6. }
  7. //css style
  8. //-------------------------------
  9. body{
  10. font-size:12px;
  11. }

3.3.2 默认变量

sass的默认变量仅需要在值后面加上!default即可。

  1. //sass style
  2. //-------------------------------
  3. $baseLineHeight: 1.5 !default;
  4. body{
  5. line-height: $baseLineHeight;
  6. }
  7. //css style
  8. //-------------------------------
  9. body{
  10. line-height:1.5;
  11. }

sass的默认变量一般是用来设置默认值,然后根据需求来覆盖的,覆盖的方式也很简单,只需要在默认变量之前重新声明下变量即可

  1. //sass style
  2. //-------------------------------
  3. $baseLineHeight: 2;
  4. $baseLineHeight: 1.5 !default;
  5. body{
  6. line-height: $baseLineHeight;
  7. }
  8. //css style
  9. //-------------------------------
  10. body{
  11. line-height:2;
  12. }

可以看出现在编译后的line-height为2,而不是我们默认的1.5。默认变量的价值在进行组件化开发的时候会非常有用。

3.3.3 特殊变量

一般我们定义的变量都为属性值,可直接使用,但是如果变量作为属性或在某些特殊情况下等则必须要以#{$variables}形式使用。

  1. //sass style
  2. //-------------------------------
  3. $borderDirection: top !default;
  4. $baseFontSize: 12px !default;
  5. $baseLineHeight: 1.5 !default;
  6. //应用于class和属性
  7. .border-#{$borderDirection}{
  8. border-#{$borderDirection}:1px solid #ccc;
  9. }
  10. //应用于复杂的属性值
  11. body{
  12. font:#{$baseFontSize}/#{$baseLineHeight};
  13. }
  14. //css style
  15. //-------------------------------
  16. .border-top{
  17. border-top:1px solid #ccc;
  18. }
  19. body {
  20. font: 12px/1.5;
  21. }

3.3.4 多值变量

多值变量分为list类型和map类型,简单来说list类型有点像js中的数组,而map类型有点像js中的对象。

3.3.4.1 list

list数据可通过空格,逗号或小括号分隔多个值,可用nth($var,$index)取值。关于list数据操作还有很多其他函数如length($list),join($list1,$list2,[$separator]),append($list,$value,[$separator])等,具体可参考sass Functions(搜索List Functions即可) 定义

  1. //一维数据
  2. $px: 5px 10px 20px 30px;
  3. //二维数据,相当于js中的二维数组
  4. $px: 5px 10px, 20px 30px;
  5. $px: (5px 10px) (20px 30px);

使用

  1. //sass style
  2. //-------------------------------
  3. $linkColor: #08c #333 !default;//第一个值为默认值,第二个鼠标滑过值
  4. a{
  5. color:nth($linkColor,1);
  6. &:hover{
  7. color:nth($linkColor,2);
  8. }
  9. }
  10. //css style
  11. //-------------------------------
  12. a{
  13. color:#08c;
  14. }
  15. a:hover{
  16. color:#333;
  17. }

3.3.4.2 map

map数据以key和value成对出现,其中value又可以是list。格式为:$map: (key1: value1, key2: value2, key3: value3);。可通过map-get($map,$key)取值。关于map数据还有很多其他函数如map-merge($map1,$map2),map-keys($map),map-values($map)等,具体可参考sass Functions(搜索Map Functions即可) 定义

  1. $heading: (h1: 2em, h2: 1.5em, h3: 1.2em);

使用

  1. //sass style
  2. //-------------------------------
  3. $headings: (h1: 2em, h2: 1.5em, h3: 1.2em);
  4. @each $header, $size in $headings {
  5. #{$header} {
  6. font-size: $size;
  7. }
  8. }
  9. //css style
  10. //-------------------------------
  11. h1 {
  12. font-size: 2em;
  13. }
  14. h2 {
  15. font-size: 1.5em;
  16. }
  17. h3 {
  18. font-size: 1.2em;
  19. }

3.4 嵌套(Nesting)

sass的嵌套包括两种:一种是选择器的嵌套;另一种是属性的嵌套。我们一般说起或用到的都是选择器的嵌套。

3.4.1 选择器嵌套

所谓选择器嵌套指的是在一个选择器中嵌套另一个选择器来实现继承,从而增强了sass文件的结构性和可读性。 在选择器嵌套中,可以使用&表示父元素选择器

  1. //sass style
  2. //-------------------------------
  3. #top_nav{
  4. line-height: 40px;
  5. text-transform: capitalize;
  6. background-color:#333;
  7. li{
  8. float:left;
  9. }
  10. a{
  11. display: block;
  12. padding: 0 10px;
  13. color: #fff;
  14. &:hover{
  15. color:#ddd;
  16. }
  17. }
  18. }
  19. //css style
  20. //-------------------------------
  21. #top_nav{
  22. line-height: 40px;
  23. text-transform: capitalize;
  24. background-color:#333;
  25. }
  26. #top_nav li{
  27. float:left;
  28. }
  29. #top_nav a{
  30. display: block;
  31. padding: 0 10px;
  32. color: #fff;
  33. }
  34. #top_nav a:hover{
  35. color:#ddd;
  36. }

3.4.2 属性嵌套

所谓属性嵌套指的是有些属性拥有同一个开始单词,如border-width,border-color都是以border开头。拿个官网的实例看下:

  1. //sass style
  2. //-------------------------------
  3. .fakeshadow {
  4. border: {
  5. style: solid;
  6. left: {
  7. width: 4px;
  8. color: #888;
  9. }
  10. right: {
  11. width: 2px;
  12. color: #ccc;
  13. }
  14. }
  15. }
  16. //css style
  17. //-------------------------------
  18. .fakeshadow {
  19. border-style: solid;
  20. border-left-width: 4px;
  21. border-left-color: #888;
  22. border-right-width: 2px;
  23. border-right-color: #ccc;
  24. }

3.5 混合(mixin)

sass中使用@mixin声明混合,可以传递参数,参数名以$符号开始,多个参数以逗号分开,也可以给参数设置默认值。声明的@mixin通过@include来调用。

3.5.1 无参数mixin

  1. //sass style
  2. //-------------------------------
  3. @mixin center-block {
  4. margin-left:auto;
  5. margin-right:auto;
  6. }
  7. .demo{
  8. @include center-block;
  9. }
  10. //css style
  11. //-------------------------------
  12. .demo{
  13. margin-left:auto;
  14. margin-right:auto;
  15. }

3.5.2 有参数mixin

  1. //sass style
  2. //-------------------------------
  3. @mixin opacity($opacity:50) {
  4. opacity: $opacity / 100;
  5. filter: alpha(opacity=$opacity);
  6. }
  7. //css style
  8. //-------------------------------
  9. .opacity{
  10. @include opacity; //参数使用默认值
  11. }
  12. .opacity-80{
  13. @include opacity(80); //传递参数
  14. }

3.5.3 多个参数mixin

调用时可直接传入值,如@include传入参数的个数小于@mixin定义参数的个数,则按照顺序表示,后面不足的使用默认值,如不足的没有默认值则报错。除此之外还可以选择性的传入参数,使用参数名与值同时传入。

  1. //sass style
  2. //-------------------------------
  3. @mixin horizontal-line($border:1px dashed #ccc, $padding:10px){
  4. border-bottom:$border;
  5. padding-top:$padding;
  6. padding-bottom:$padding;
  7. }
  8. .imgtext-h li{
  9. @include horizontal-line(1px solid #ccc);
  10. }
  11. .imgtext-h--product li{
  12. @include horizontal-line($padding:15px);
  13. }
  14. //css style
  15. //-------------------------------
  16. .imgtext-h li {
  17. border-bottom: 1px solid #cccccc;
  18. padding-top: 10px;
  19. padding-bottom: 10px;
  20. }
  21. .imgtext-h--product li {
  22. border-bottom: 1px dashed #cccccc;
  23. padding-top: 15px;
  24. padding-bottom: 15px;
  25. }

3.5.4 多组值参数mixin

如果一个参数可以有多组值,如box-shadow、transition等,那么参数则需要在变量后加三个点表示,如$variables...。

  1. //sass style
  2. //-------------------------------
  3. //box-shadow可以有多组值,所以在变量参数后面添加...
  4. @mixin box-shadow($shadow...) {
  5. -webkit-box-shadow:$shadow;
  6. box-shadow:$shadow;
  7. }
  8. .box{
  9. border:1px solid #ccc;
  10. @include box-shadow(0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3));
  11. }
  12. //css style
  13. //-------------------------------
  14. .box{
  15. border:1px solid #ccc;
  16. -webkit-box-shadow:0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3);
  17. box-shadow:0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3);
  18. }

3.5.5 @content

@content在sass3.2.0中引入,可以用来解决css3的@media等带来的问题。它可以使@mixin接受一整块样式,接受的样式从@content开始。

  1. //sass style
  2. //-------------------------------
  3. @mixin max-screen($res){
  4. @media only screen and ( max-width: $res )
  5. {
  6. @content;
  7. }
  8. }
  9. @include max-screen(480px) {
  10. body { color: red }
  11. }
  12. //css style
  13. //-------------------------------
  14. @media only screen and (max-width: 480px) {
  15. body { color: red }
  16. }

3.6 继承

sass中,选择器继承可以让选择器继承另一个选择器的所有样式,并联合声明。使用选择器的继承,要使用关键词@extend,后面紧跟需要继承的选择器。

3.6.1 普通继承

  1. //sass style
  2. //-------------------------------
  3. h1{
  4. border: 4px solid #ff9aa9;
  5. }
  6. .speaker{
  7. @extend h1;
  8. border-width: 2px;
  9. }
  10. //css style
  11. //-------------------------------
  12. h1,.speaker{
  13. border: 4px solid #ff9aa9;
  14. }
  15. .speaker{
  16. border-width: 2px;
  17. }

3.6.2 占位选择器%

从sass 3.2.0以后就可以定义占位选择器%。这种选择器的优势在于:如果不调用则不会有任何多余的css文件,避免了以前在一些基础的文件中预定义了很多基础的样式,然后实际应用中不管是否使用了@extend去继承相应的样式,都会解析出来所有的样式。占位选择器以%标识定义,通过@extend调用。

  1. //sass style
  2. //-------------------------------
  3. %ir{
  4. color: transparent;
  5. text-shadow: none;
  6. background-color: transparent;
  7. border: 0;
  8. }
  9. %clearfix{
  10. @if $lte7 {
  11. *zoom: 1;
  12. }
  13. &:before,
  14. &:after {
  15. content: "";
  16. display: table;
  17. font: 0/0 a;
  18. }
  19. &:after {
  20. clear: both;
  21. }
  22. }
  23. #header{
  24. h1{
  25. @extend %ir;
  26. width:300px;
  27. }
  28. }
  29. .ir{
  30. @extend %ir;
  31. }
  32. //css style
  33. //-------------------------------
  34. #header h1,
  35. .ir{
  36. color: transparent;
  37. text-shadow: none;
  38. background-color: transparent;
  39. border: 0;
  40. }
  41. #header h1{
  42. width:300px;
  43. }

如上代码,定义了两个占位选择器%ir和%clearfix,其中%clearfix这个没有调用,所以解析出来的css样式也就没有clearfix部分。占位选择器的出现,使css文件更加简练可控,没有多余。所以可以用其定义一些基础的样式文件,然后根据需要调用产生相应的css。

ps:在@media中暂时不能@extend @media外的代码片段,以后将会可以。

3.7 函数

sass定义了很多函数可供使用,当然你也可以自己定义函数,以@fuction开始。实际项目中我们使用最多的应该是颜色函数,而颜色函数中又以lighten减淡和darken加深为最,其调用方法为lighten($color,$amount)和darken($color,$amount),它们的第一个参数都是颜色值,第二个参数都是百分比。

  1. //sass style
  2. //-------------------------------
  3. $baseFontSize: 10px !default;
  4. $gray: #ccc !defualt;
  5. // pixels to rems
  6. @function pxToRem($px) {
  7. @return $px / $baseFontSize * 1rem;
  8. }
  9. body{
  10. font-size:$baseFontSize;
  11. color:lighten($gray,10%);
  12. }
  13. .test{
  14. font-size:pxToRem(16px);
  15. color:darken($gray,10%);
  16. }
  17. //css style
  18. //-------------------------------
  19. body{
  20. font-size:10px;
  21. color:#E6E6E6;
  22. }
  23. .test{
  24. font-size:1.6rem;
  25. color:#B3B3B3;
  26. }

3.8 运算

sass具有运算的特性,可以对数值型的Value(如:数字、颜色、变量等)进行加减乘除四则运算。请注意运算符前后请留一个空格,不然会出错。

  1. $baseFontSize: 14px !default;
  2. $baseLineHeight: 1.5 !default;
  3. $baseGap: $baseFontSize * $baseLineHeight !default;
  4. $halfBaseGap: $baseGap / 2 !default;
  5. $samllFontSize: $baseFontSize - 2px !default;
  6. //grid
  7. $_columns: 12 !default; // Total number of columns
  8. $_column-width: 60px !default; // Width of a single column
  9. $_gutter: 20px !default; // Width of the gutter
  10. $_gridsystem-width: $_columns * ($_column-width + $_gutter); //grid system width

3.9 条件判断及循环

3.9.1 @if判断

@if可一个条件单独使用,也可以和@else结合多条件使用

  1. //sass style
  2. //-------------------------------
  3. $lte7: true;
  4. $type: monster;
  5. .ib{
  6. display:inline-block;
  7. @if $lte7 {
  8. *display:inline;
  9. *zoom:1;
  10. }
  11. }
  12. p {
  13. @if $type == ocean {
  14. color: blue;
  15. } @else if $type == matador {
  16. color: red;
  17. } @else if $type == monster {
  18. color: green;
  19. } @else {
  20. color: black;
  21. }
  22. }
  23. //css style
  24. //-------------------------------
  25. .ib{
  26. display:inline-block;
  27. *display:inline;
  28. *zoom:1;
  29. }
  30. p {
  31. color: green;
  32. }

3.9.2 三目判断

语法为:if($condition, $iftrue, $iffalse) 。三个参数分别表示:条件,条件为真的值,条件为假的值。

  1. if(true, 1px, 2px) => 1px
  2. if(false, 1px, 2px) => 2px

3.9.3for循环

for循环有两种形式,分别为:@for $var from through 和@for $var from to 。$i表示变量,start表示起始值,end表示结束值,这两个的区别是关键字through表示包括end这个数,而to则不包括end这个数。

  1. //sass style
  2. //-------------------------------
  3. @for $i from 1 through 3 {
  4. .item-#{$i} { width: 2em * $i; }
  5. }
  6. //css style
  7. //-------------------------------
  8. .item-1 {
  9. width: 2em;
  10. }
  11. .item-2 {
  12. width: 4em;
  13. }
  14. .item-3 {
  15. width: 6em;
  16. }

3.9.4 @each循环

语法为:@each $var in 。其中$var表示变量,而list和map表示list类型数据和map类型数据。sass 3.3.0新加入了多字段循环和map数据循环。

3.9.4.1 单个字段list数据循环

  1. //sass style
  2. //-------------------------------
  3. $animal-list: puma, sea-slug, egret, salamander;
  4. @each $animal in $animal-list {
  5. .#{$animal}-icon {
  6. background-image: url('/images/#{$animal}.png');
  7. }
  8. }
  9. //css style
  10. //-------------------------------
  11. .puma-icon {
  12. background-image: url('/images/puma.png');
  13. }
  14. .sea-slug-icon {
  15. background-image: url('/images/sea-slug.png');
  16. }
  17. .egret-icon {
  18. background-image: url('/images/egret.png');
  19. }
  20. .salamander-icon {
  21. background-image: url('/images/salamander.png');
  22. }

3.9.4.2 多个字段list数据循环

  1. //sass style
  2. //-------------------------------
  3. $animal-data: (puma, black, default),(sea-slug, blue, pointer),(egret, white, move);
  4. @each $animal, $color, $cursor in $animal-data {
  5. .#{$animal}-icon {
  6. background-image: url('/images/#{$animal}.png');
  7. border: 2px solid $color;
  8. cursor: $cursor;
  9. }
  10. }
  11. //css style
  12. //-------------------------------
  13. .puma-icon {
  14. background-image: url('/images/puma.png');
  15. border: 2px solid black;
  16. cursor: default;
  17. }
  18. .sea-slug-icon {
  19. background-image: url('/images/sea-slug.png');
  20. border: 2px solid blue;
  21. cursor: pointer;
  22. }
  23. .egret-icon {
  24. background-image: url('/images/egret.png');
  25. border: 2px solid white;
  26. cursor: move;
  27. }

3.9.4.3多个字段map数据循环

  1. //sass style
  2. //-------------------------------
  3. $headings: (h1: 2em, h2: 1.5em, h3: 1.2em);
  4. @each $header, $size in $headings {
  5. #{$header} {
  6. font-size: $size;
  7. }
  8. }
  9. //css style
  10. //-------------------------------
  11. h1 {
  12. font-size: 2em;
  13. }
  14. h2 {
  15. font-size: 1.5em;
  16. }
  17. h3 {
  18. font-size: 1.2em;
  19. }

人气教程排行