sass less
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架。本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass、Less CSS、Stylus。
首先我们来简单介绍下什么是 CSS 预处理器,CSS 预处理器是一种语言用来为 CSS 增加一些编程的的特性,无需考虑浏览器的兼容性问题,例如你可以在 CSS 中使用变量、简单的程序逻辑、函数等等在编程语言中的一些基本技巧,可以让你的 CSS 更见简洁,适应性更强,代码更直观等诸多好处。
不要再停留在石器时代了,下面让我们开始 CSS 预处理器之旅。
我们将会从语法、变量、嵌套、混入(Mixin)、继承、导入、函数和操作符等方面分别对这三个框架进行比较介绍。
语法
在使用 CSS 预处理器之前最重要的是理解语法,幸运的是基本上大多数预处理器的语法跟 CSS 都差不多。
首先 Sass 和 Less 都使用的是标准的 CSS 语法,因此如果你可以很方便的将已有的 CSS 代码转为预处理器代码,默认 Sass 使用 .sass 扩展名,而 Less 使用 .less 扩展名。
下面是这二者的语法:
1
2
3
4
|
/* style.scss or style.less */ h 1 { color : #0982C1 ; } |
你注意到了,这是一个再普通不过的,不过 Sass 同时也支持老的语法,就是不包含花括号和分号的方式:
1
2
3
|
/* style.sass */ h 1 color : #0982c1 |
而 Stylus 支持的语法要更多样性一点,它默认使用 .styl 的文件扩展名,下面是 Stylus 支持的语法:
1
2
3
4
5
6
7
8
9
10
11
12
|
/* style.styl */ h 1 { color : #0982C1 ; } /* omit brackets */ h 1 color : #0982C1 ; /* omit colons and semi-colons */ h 1 color #0982C1 |
你也可以在同一个样式单中使用不同的变量,例如下面的写法也不会报错:
1
2
3
4
5
|
h 1 { color #0982c1 } h 2 font-size : 1.2em |
变量
你可以在 CSS 预处理器中声明变量,并在整个样式单中使用,支持任何类型的变量,例如颜色、数值(不管是否包括单位)、文本。然后你可以任意引用该变量。
Sass 的变量必须是 $ 开始,然后变量名和值使用冒号隔开,跟 CSS 的属性一致:
1
2
3
4
5
6
7
8
9
|
$mainColor: #0982c1 ; $siteWidth: 1024px ; $borderStyle: dotted ; body { color : $mainColor; border : 1px $borderStyle $mainColor; max-width : $siteWidth; } |
而 Less 的变量名使用 @ 符号开始:
1
2
3
4
5
6
7
8
9
|
@mainColor: #0982c1 ; @siteWidth: 1024px ; @borderStyle: dotted ; body { color : @mainColor; border : 1px @borderStyle @mainColor; max-width : @siteWidth; } |
Stylus 对变量名没有任何限定,你可以是 $ 开始,也可以是任意的字符,而且与变量值之间可以用冒号、空格隔开,需要注意的是 Stylus (0.22.4) 将会编译 @ 开始的变量,但其对应的值并不会赋予该变量,换句话说,在 Stylus 的变量名不要用 @ 开头。
1
2
3
4
5
6
7
8
|
mainColor = #0982c1 siteWidth = 1024px $borderStyle = dotted body color mainColor border 1px $borderStyle mainColor max-width siteWidth |
上面的三种不同的 CSS 预处理器的写法,最终都将产生相同的结果:
1
2
3
4
5
|
body { color : #0982c1 ; border : 1px dotted #0982c1 ; max-width : 1024px ; } |
你可以想象,加入你的 CSS 中使用了某个颜色的地方多达数十次,那么要修改颜色时你必须找到这数十次的地方并一一修改,而有了 CSS 预处理器,修改一个地方就够了!
嵌套
如果我们需要在CSS中相同的 parent 引用多个元素,这将是非常乏味的,你需要一遍又一遍地写 parent。例如:
1
2
3
4
5
6
7
8
9
10
11
12
|
section { margin : 10px ; } section nav { height : 25px ; } section nav a { color : #0982C1 ; } section nav a:hover { text-decoration : underline ; } |
而如果用 CSS 预处理器,就可以少些很多单词,而且父子节点关系一目了然。我们这里提到的三个 CSS 框架都是允许嵌套语法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
section { margin : 10px ; nav { height : 25px ; a { color : #0982C1 ; &:hover { text-decoration : underline ; } } } } |
最终生成的 CSS 结果是:
1
2
3
4
5
6
7
8
9
10
11
12
|
section { margin : 10px ; } section nav { height : 25px ; } section nav a { color : #0982C1 ; } section nav a:hover { text-decoration : underline ; } |
非常之方便!
Mixins (混入)
Mixins 有点像是函数或者是宏,当你某段 CSS 经常需要在多个元素中使用时,你可以为这些共用的 CSS 定义一个 Mixin,然后你只需要在需要引用这些 CSS 地方调用该 Mixin 即可。
Sass 的混入语法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* Sass mixin error with (optional) argument $borderWidth which defaults to 2px if not specified */ @mixin error($borderWidth: 2px ) { border : $borderWidth solid #F00 ; color : #F00 ; } .generic-error { padding : 20px ; margin : 4px ; @ include error(); /* Applies styles from mixin error */ } .login-error { left : 12px ; position : absolute ; top : 20px ; @ include error( 5px ); /* Applies styles from mixin error with argument $borderWidth equal to 5px*/ } |
Less CSS 的混入语法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* LESS mixin error with (optional) argument @borderWidth which defaults to 2px if not specified */ .error(@borderWidth: 2px ) { border : @borderWidth solid #F00 ; color : #F00 ; } .generic-error { padding : 20px ; margin : 4px ; .error(); /* Applies styles from mixin error */ } .login-error { left : 12px ; position : absolute ; top : 20px ; .error( 5px ); /* Applies styles from mixin error with argument @borderWidth equal to 5px */ } |
Stylus 的混入语法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* Stylus mixin error with (optional) argument borderWidth which defaults to 2px if not specified */ error(borderWidth= 2px ) { border : borderWidth solid #F00 ; color : #F00 ; } .generic-error { padding : 20px ; margin : 4px ; error(); /* Applies styles from mixin error */ } .login-error { left : 12px ; position : absolute ; top : 20px ; error( 5px ); /* Applies styles from mixin error with argument borderWidth equal to 5px */ } |
最终它们都将编译成如下的 CSS 样式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.generic-error { padding : 20px ; margin : 4px ; border : 2px solid #f00 ; color : #f00 ; } .login-error { left : 12px ; position : absolute ; top : 20px ; border : 5px solid #f00 ; color : #f00 ; } |
继承
当我们需要为多个元素定义相同样式的时候,我们可以考虑使用继承的做法。例如我们经常需要:
1
2
3
4
5
|
p, ul, ol { /* styles here */ } |
在 Sass 和 Stylus 我们可以这样写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
. block { margin : 10px 5px ; padding : 2px ; } p { @extend . block ; /* Inherit styles from '.block' */ border : 1px solid #EEE ; } ul, ol { @extend . block ; /* Inherit styles from '.block' */ color : #333 ; text-transform : uppercase ; } |
在这里首先定义 .block 块,然后让 p 、ul 和 ol 元素继承 .block ,最终生成的 CSS 如下:
1
2
3
4
5
6
7
8
9
10
11
|
. block , p, ul, ol { margin : 10px 5px ; padding : 2px ; } p { border : 1px solid #EEE ; } ul, ol { color : #333 ; text-transform : uppercase ; } |
在这方面 Less 表现的稍微弱一些,更像是混入写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
. block { margin : 10px 5px ; padding : 2px ; } p { . block ; /* Inherit styles from '.block' */ border : 1px solid #EEE ; } ul, ol { . block ; /* Inherit styles from '.block' */ color : #333 ; text-transform : uppercase ; } |
生成的 CSS 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
. block { margin : 10px 5px ; padding : 2px ; } p { margin : 10px 5px ; padding : 2px ; border : 1px solid #EEE ; } ul, ol { margin : 10px 5px ; padding : 2px ; color : #333 ; text-transform : uppercase ; } |
你所看到的上面的代码中,.block 的样式将会被插入到相应的你想要继承的选择器中,但需要注意的是优先级的问题。
导入 (Import)
很多 CSS 开发者对导入的做法都不太感冒,因为它需要多次的 HTTP 请求。但是在 CSS 预处理器中的导入操作则不同,它只是在语义上包含了不同的文件,但最终结果是一个单一的 CSS 文件,如果你是通过 @ import "file.css";
导入 CSS 文件,那效果跟普通的 CSS 导入一样。注意:导入文件中定义的混入、变量等信息也将会被引入到主样式文件中,因此需要避免它们互相冲突。
reset.css:
1
2
3
4
|
/* file.{type} */ body { background : #EEE ; } |
main.xxx:
1
2
3
4
5
6
|
@ import "reset.css" ; @ import "file.{type}" ; p { background : #0982C1 ; } |
最终生成的 CSS:
1
2
3
4
5
6
7
|
@ import "reset.css" ; body { background : #EEE ; } p { background : #0982C1 ; } |
颜色函数
CSS 预处理器一般都会内置一些颜色处理函数用来对颜色值进行处理,例如加亮、变暗、颜色梯度等。
Sass:
1
2
3
4
5
6
7
8
9
10
11
|
lighten($color, 10% ); /* returns a color 10% lighter than $color */ darken($color, 10% ); /* returns a color 10% darker than $color */ saturate($color, 10% ); /* returns a color 10% more saturated than $color */ desaturate($color, 10% ); /* returns a color 10% less saturated than $color */ grayscale($color); /* returns grayscale of $color */ complement($color); /* returns complement color of $color */ invert ($color); /* returns inverted color of $color */ mix ($color 1 , $color 2 , 50% ); /* mix $color1 with $color2 with a weight of 50% */ |
上面只是简单列了 Sass 的一些基本颜色处理函数,完整的列表请看 Sass Documentation.
下面是一个具体的例子:
1
2
3
4
5
6
|
$ color : #0982C1 ; h 1 { background : $color; border : 3px solid darken($color, 50% ); } |
Less CSS:
1
2
3
4
5
6
7
8
9
10
|
lighten(@color, 10% ); /* returns a color 10% lighter than @color */ darken(@color, 10% ); /* returns a color 10% darker than @color */ saturate(@color, 10% ); /* returns a color 10% more saturated than @color */ desaturate(@color, 10% ); /* returns a color 10% less saturated than @color */ spin(@color, 10 ); /* returns a color with a 10 degree larger in hue than @color */ spin(@color, -10 ); /* returns a color with a 10 degree smaller hue than @color */ mix (@color 1 , @color 2 ); /* return a mix of @color1 and @color2 */ |
LESS 完整的颜色函数列表请看 LESS Documentation.
LESS 使用颜色函数的例子:
1
2
3
4
5
6
|
@ color : #0982C1 ; h 1 { background : @color; border : 3px solid darken(@color, 50% ); } |
Stylus:
1
2
3
4
5
|
lighten(color, 10% ); /* returns a color 10% lighter than 'color' */ darken(color, 10% ); /* returns a color 10% darker than 'color' */ saturate(color, 10% ); /* returns a color 10% more saturated than 'color' */ desaturate(color, 10% ); /* returns a color 10% less saturated than 'color' */ |
完整的颜色函数列表请阅读 Stylus Documentation.
实例:
1
2
3
4
5
|
color = #0982C1 h 1 background color border 3px solid darken(color, 50% ) |
运算符
你可以直接在 CSS 预处理器中进行样式的计算,例如:
1
2
3
4
5
6
|
body { margin : ( 14px / 2 ); top : 50px + 100px ; right : 100px - 50px ; left : 10 * 10 ; } |
一些跟具体浏览器相关的处理
这是宣传使用预处理的原因之一,并且是一个很好的理由,这样可以节省的大量的时间和汗水。创建一个mixin来处理不同浏览器的CSS写法是很简单的,节省了大量的重复工作和痛苦的代码编辑。
Sass
1
2
3
4
5
6
7
8
9
|
@mixin border-radius($values) { -webkit-border-radius: $values; -moz-border-radius: $values; border-radius: $values; } div { @ include border-radius( 10px ); } |
Less CSS
1
2
3
4
5
6
7
8
9
|
.border-radius(@values) { -webkit-border-radius: @values; -moz-border-radius: @values; border-radius: @values; } div { .border-radius( 10px ); } |
Stylus
1
2
3
4
5
6
7
8
9
|
border-radius(values) { -webkit-border-radius: values; -moz-border-radius: values; border-radius: values; } div { border-radius( 10px ); } |
编译结果:
1
2
3
4
5
|
div { -webkit-border-radius: 10px ; -moz-border-radius: 10px ; border-radius: 10px ; } |
3D文本
要生成具有 3D 效果的文本可以使用 text-shadows
,唯一的问题就是当要修改颜色的时候就非常的麻烦,而通过 mixin 和颜色函数可以很轻松的实现:
Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@mixin text 3 d($color) { color : $color; text-shadow : 1px 1px 0px darken($color, 5% ), 2px 2px 0px darken($color, 10% ), 3px 3px 0px darken($color, 15% ), 4px 4px 0px darken($color, 20% ), 4px 4px 2px #000 ; } h 1 { font-size : 32pt ; @ include text 3 d( #0982c1 ); } |
Less CSS
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.text 3 d(@color) { color : @color; text-shadow : 1px 1px 0px darken(@color, 5% ), 2px 2px 0px darken(@color, 10% ), 3px 3px 0px darken(@color, 15% ), 4px 4px 0px darken(@color, 20% ), 4px 4px 2px #000 ; } span { font-size : 32pt ; .text 3 d( #0982c1 ); } |
Stylus
1
2
3
4
5
6
|
text 3 d(color) color : color text-shadow : 1px 1px 0px darken(color, 5% ), 2px 2px 0px darken(color, 10% ), 3px 3px 0px darken(color, 15% ), 4px 4px 0px darken(color, 20% ), 4px 4px 2px #000 span font-size : 32pt text 3 d( #0982c1 ) |
生成的 CSS
1
2
3
4
5
6
7
8
9
|
span { font-size : 32pt ; color : #0982c1 ; text-shadow : 1px 1px 0px #097bb7 , 2px 2px 0px #0875ae , 3px 3px 0px #086fa4 , 4px 4px 0px #07689a , 4px 4px 2px #000 ; } |
效果图:
列 (Columns)
使用数值操作和变量可以很方便的实现适应屏幕大小的布局处理。
Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$siteWidth: 1024px ; $gutterWidth: 20px ; $sidebarWidth: 300px ; body { margin : 0 auto ; width : $siteWidth; } .content { float : left ; width : $siteWidth - ($sidebarWidth+$gutterWidth); } .sidebar { float : left ; margin-left : $gutterWidth; width : $sidebarWidth; } |
Less CSS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@siteWidth: 1024px ; @gutterWidth: 20px ; @sidebarWidth: 300px ; body { margin : 0 auto ; width : @siteWidth; } .content { float : left ; width : @siteWidth - (@sidebarWidth+@gutterWidth); } .sidebar { float : left ; margin-left : @gutterWidth; width : @sidebarWidth; } |
Stylus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
siteWidth = 1024px ; gutterWidth = 20px ; sidebarWidth = 300px ; body { margin : 0 auto ; width : siteWidth; } .content { float : left ; width : siteWidth - (sidebarWidth+gutterWidth); } .sidebar { float : left ; margin-left : gutterWidth; width : sidebarWidth; } |
实际效果
1
2
3
4
5
6
7
8
9
10
11
12
13
|
body { margin : 0 auto ; width : 1024px ; } .content { float : left ; width : 704px ; } .sidebar { float : left ; margin-left : 20px ; width : 300px ; } |
错误报告
如果你经常 CSS 你会发现很难找到 CSS 中错误的地方,这也是预处理框架的好处,它会报告错误,你可以从这篇文章中学习如何让 CSS 框架错误报告。
注释
以上三种框架都支持形如 /* */ 的多行注释以及 // 的单行注释。
sass less的更多相关文章
- wepack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
- wepack+sass+vue 入门教程(一)
一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...
- 前端CSS预处理器Sass
前面的话 "CSS预处理器"(css preprocessor)的基本思想是,用一种专门的编程语言,进行网页样式设计,然后再编译成正常的CSS文件.SASS是一种CSS的开发工 ...
- SASS教程sass超详细教程
SASS安装及使用(sass教程.详细教程) 采用SASS开发CSS,可以提高开发效率. SASS建立在Ruby的基础之上,所以得先安装Ruby. Ruby的安装: 安装 rubyinstaller- ...
- Sass之坑Compass编译报错
前段时间在使用Compass时遇到了其为难处理的一个坑,现记录到博客希望能帮助到各位. 一.问题: 利用Koala或者是gulp编译提示如下,截图为koala编译提示错误: 二.解决办法 从问题截图上 ...
- emmet,jade,haml, slim,less,sass,coffeescript等的实战优缺点
摘要: 文章背景,来自于群内周五晚上的一次头脑风暴式的思维碰撞交流活动. 随着前端技术的蓬勃发展, 各种新技术随着生产力的需要不断的涌入我们的视野, 那今天探讨的话题是这些新时代的前端兵器谱: 一. ...
- Sass用法指南
写在前面的话:随着CSS文件越来越大,内容越来越复杂,对其进行很好的维护将变的很困难.这时CSS预处理器就能够帮上大忙了,它们往往拥有变量.嵌套.继承等许多CSS不具备的特性.有很多CSS预处理器,这 ...
- PostCSS深入学习: PostCSS和Sass、Stylus或LESS一起使用
如果你喜欢使用PostCSS,但又不想抛弃你最喜欢的预处理器.不用担心,你不需要作出二选一的选择,你可以把PostCSS和预处理器(Sass.Stylus或LESS)结合起来使用. 有几个PostCS ...
- Sass:初识Sass与Koala工具的使用
一.下载Koala(找到合适的系统版本)并安装 二.先新建一个css文件夹,并在里面新建一个文本文档(.txt),将其命名为demo.scss 三.打开Koala,将css文件夹拽进来,可以修改一下输 ...
随机推荐
- VC++获取计算机Hardware Information (CPU ID, MainBoard Info, Hard Disk Serial, System Information)
转载:http://blog.csdn.net/yapingxin/article/details/50107799 转载:http://zhidao.baidu.com/link?url=A5K6N ...
- 解决点击状态栏时ScrollView自动滚动到初始位置失效办法
http://www.cocoachina.com/ios/20150807/12949.html 取消点击状态栏scrollView会自动滚动到初始位置的功能 _scrollView.scrolls ...
- git -C
https://git-scm.com/docs/git -C <path> Run as if git was started in <path> instead of th ...
- C#打印条码的几种方式
标题虽然是说C#,但是以下介绍的几种方法不是只能在C#中使用,在其它的语言里面也行. 总结一下常见的条码打印方法,其实打条码的方式很多,大概有以下几种: 1.斑马打印软件制作好模板,保存为.prn格式 ...
- poj 3335(半平面交)
链接:http://poj.org/problem?id=3335 //大牛们常说的测模板题 ------------------------------------------------- ...
- Windows Internals学习笔记(二)系统架构
参考资料: 1. <Windows Internals> 2. http://bestcbooks.com 3. Windows Drive Kit 4. Microsoft Window ...
- 深入理解 C# 协变和逆变
msdn 解释如下: “协变”是指能够使用与原始指定的派生类型相比,派生程度更大的类型. “逆变”则是指能够使用派生程度更小的类型. 解释的很正确,大致就是这样,不过不够直白. 直白的理解: “协变” ...
- 讓 SourceTree 讀取自定的 SSH key
我目前都在 Mac 底下開發,用 Git 來管理我的程式碼,比較一番之後決定用 SourceTree 來做為 Git client.SourceTree 是一款 Mac 底下的版本控制系統 clien ...
- android tablelayout 显示图片
当在tablelayout中显示图片时,设置imageView为固定大小时,会出现divide by zero 错误 将LayoutParams 改为 TableRow.LayoutParams即可 ...
- phpStorm快捷键
1.快速寻找方法,变量定义处:ctrl + b或者ctrl+单击 2. 移动视图,方便快捷的移动代码窗口: ctrl + up, down 3. 代码方法间快速跳转:alt + up, down