为您详细比较三个css预处理器(框架):sass、less和stylus

上传人:自*** 文档编号:79020298 上传时间:2019-02-16 格式:DOC 页数:17 大小:284KB
返回 下载 相关 举报
为您详细比较三个css预处理器(框架):sass、less和stylus_第1页
第1页 / 共17页
为您详细比较三个css预处理器(框架):sass、less和stylus_第2页
第2页 / 共17页
为您详细比较三个css预处理器(框架):sass、less和stylus_第3页
第3页 / 共17页
为您详细比较三个css预处理器(框架):sass、less和stylus_第4页
第4页 / 共17页
为您详细比较三个css预处理器(框架):sass、less和stylus_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《为您详细比较三个css预处理器(框架):sass、less和stylus》由会员分享,可在线阅读,更多相关《为您详细比较三个css预处理器(框架):sass、less和stylus(17页珍藏版)》请在金锄头文库上搜索。

1、CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架。本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass、Less CSS、Stylus。首先我们来简单介绍下什么是 CSS 预处理器,CSS 预处理器是一种语言用来为 CSS 增加一些编程的的特性,无需考虑浏览器的兼容性问题,例如你可以在 CSS 中使用变量、简单的程序逻辑、函数等等在编程语言中的一些基本技巧,可以让你的 CSS 更见简洁,适应性更强,代码更直观等诸多好处。不要再停留在石器时代了,下面让我们开始 CSS 预处理器之旅。我们将会从语法、变量、嵌套、混入(Mixin)、继承、导入、

2、函数和操作符等方面分别对这三个框架进行比较介绍。语法在使用 CSS 预处理器之前最重要的是理解语法,幸运的是基本上大多数预处理器的语法跟 CSS 都差不多。首先 Sass 和 Less 都使用的是标准的 CSS 语法,因此如果你可以很方便的将已有的 CSS 代码转为预处理器代码,默认 Sass 使用 .sass 扩展名,而 Less 使用 .less 扩展名。下面是这二者的语法:?1234/* style.scss or style.less */h1 color: #0982C1;你注意到了,这是一个再普通不过的,不过 Sass 同时也支持老的语法,就是不包含花括号和分号的方式:?123/*

3、 style.sass */h1color: #0982c1而 Stylus 支持的语法要更多样性一点,它默认使用 .styl 的文件扩展名,下面是 Stylus 支持的语法:?123456789101112/* style.styl */h1 color: #0982C1;/* omit brackets */h1color: #0982C1;/* omit colons and semi-colons */h1color #0982C1你也可以在同一个样式单中使用不同的变量,例如下面的写法也不会报错:?12345h1 color #0982c1h2font-size: 1.2em变量你可以

4、在 CSS 预处理器中声明变量,并在整个样式单中使用,支持任何类型的变量,例如颜色、数值(不管是否包括单位)、文本。然后你可以任意引用该变量。Sass 的变量必须是 $ 开始,然后变量名和值使用冒号隔开,跟 CSS 的属性一致:?123456789$mainColor: #0982c1;$siteWidth: 1024px;$borderStyle: dotted;body color: $mainColor;border: 1px $borderStyle $mainColor;max-width: $siteWidth;而 Less 的变量名使用 符号开始:?123456789mainCo

5、lor: #0982c1;siteWidth: 1024px;borderStyle: dotted;body color: mainColor;border: 1px borderStyle mainColor;max-width: siteWidth;Stylus 对变量名没有任何限定,你可以是 $ 开始,也可以是任意的字符,而且与变量值之间可以用冒号、空格隔开,需要注意的是 Stylus (0.22.4) 将会编译 开始的变量,但其对应的值并不会赋予该变量,换句话说,在 Stylus 的变量名不要用 开头。?12345678mainColor = #0982c1siteWidth = 1

6、024px$borderStyle = dottedbodycolor mainColorborder 1px $borderStyle mainColormax-width siteWidth上面的三种不同的 CSS 预处理器的写法,最终都将产生相同的结果:?12345body color: #0982c1;border: 1px dotted #0982c1;max-width: 1024px;你可以想象,加入你的 CSS 中使用了某个颜色的地方多达数十次,那么要修改颜色时你必须找到这数十次的地方并一一修改,而有了 CSS 预处理器,修改一个地方就够了!嵌套如果我们需要在CSS中相同的 p

7、arent 引用多个元素,这将是非常乏味的,你需要一遍又一遍地写 parent。例如:?123456789101112section margin: 10px;section nav height: 25px;section nav a color: #0982C1;section nav a:hover text-decoration: underline;而如果用 CSS 预处理器,就可以少些很多单词,而且父子节点关系一目了然。我们这里提到的三个 CSS 框架都是允许嵌套语法:?123456789101112131415section margin: 10px;nav height: 25

8、px;a color: #0982C1;&:hover text-decoration: underline;最终生成的 CSS 结果是:?123456789101112section margin: 10px;section nav height: 25px;section nav a color: #0982C1;section nav a:hover text-decoration: underline;非常之方便!Mixins (混入)Mixins 有点像是函数或者是宏,当你某段 CSS 经常需要在多个元素中使用时,你可以为这些共用的 CSS 定义一个 Mixin,然后你只需要

9、在需要引用这些 CSS 地方调用该 Mixin 即可。Sass 的混入语法:?1234567891011121314151617/* 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(); /*

10、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 的混入语法:?1234567891011121314151617/* LESS mixin error with (optional) argument borderWidth which defaults to

11、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 border

12、Width equal to 5px */Stylus 的混入语法:?1234567891011121314151617/* 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

13、mixin error */.login-error left: 12px;position: absolute;top: 20px;error(5px); /* Applies styles from mixin error with argument borderWidth equal to 5px */最终它们都将编译成如下的 CSS 样式:?12345678910111213.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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号