CSS 预处理器 - less sass 和 stylus

CSS 预处理器是一种语言用来为 CSS 增加一些编程的的特性,无需考虑浏览器的兼容性问题,例如你可以在 CSS 中使用变量、简单的程序逻辑、函数等等在编程语言中的一些基本技巧,可以让你的 CSS 更见简洁,适应性更强,代码更直观等诸多好处

基本语法

  1. 首先 Sass 和 Less 都使用的是标准的 CSS 语法,默认 Sass 使用 .sass 扩展名,而 Less 使用 .less 扩展名
    语法:
/* style.scss or style.less */
h1 {
  color: #0982C1;
}

Sass 同时也支持老的语法,就是不包含花括号和分号的方式:

/* style.sass */
h1
  color: #0982c1
  1. Stylus 同时支持以下语法,它默认使用 .styl 的文件扩展名
/* style.styl */
h1 {
  color: #0982C1;
}
 
/* omit brackets */
h1
  color: #0982C1;

变量

我们可以在 CSS 预处理器中声明变量,并在整个样式单中使用,支持任何类型的变量,例如颜色、数值(不管是否包括单位)、文本。然后可以任意引用该变量。

  1. Sass 的变量必须是 $ 开始,然后变量名和值使用冒号隔开,跟 CSS 的属性一致:
$mainColor: #0982c1;
$siteWidth: 1024px;

body {
  color: $mainColor;
  max-width: $siteWidth;
}
  1. 而 Less 的变量名使用 @ 符号开始:
@mainColor: #0982c1;
@siteWidth: 1024px;
@borderStyle: dotted;
 
body {
  color: @mainColor;
  border: 1px @borderStyle @mainColor;
  max-width: @siteWidth;
}
  1. Stylus 对变量名没有任何限定,你可以是 $ 开始,也可以是任意的字符,而且与变量值之间可以用冒号、空格 隔开,为了增加易读性,建议用 $ 开头,:分隔。
mainColor = #0982c1
siteWidth = 1024px
$borderStyle = dotted
 
body
  color: mainColor
  border: 1px $borderStyle mainColor
  max-width: siteWidth

嵌套

如果需要为相同 parent 下的不同节设置样式,需要重复书写父节点:

section {
  margin: 10px;
}
section nav {
  height: 25px;
}
section nav a {
  color: #0982C1;
}
section nav a:hover {
  text-decoration: underline;
}

三个预处理器都支持嵌套语法:

section {
  margin: 10px;
 
  nav {
    height: 25px;
 
    a {
      color: #0982C1;
 
      &:hover {
        text-decoration: underline;
      }
    }
  }
}

Mixins (混入)

当你某段 CSS 经常需要在多个元素中使用时,你可以为这些共用的 CSS 定义一个 Mixin,然后你只需要在需要引用这些 CSS 地方调用该 Mixin 即可。

  • Sass 混入语法:
/* 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 混入语法:
/* 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 混入语法:
/* 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 */
}

最终编译:

.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;
}

继承

当我们需要为多个元素定义相同样式的时候,我们可以考虑使用继承的做法

p,
ul,
ol {
  /* styles here */
}
  • Sass 和 Stylus 写法:
.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;
}
  • Less 更像混入写法:
.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;
}

导入 (Import)

reset.css:

/* file.{type} */
body {
  background: #EEE;
}

main.xxx:

@ import "reset.css";
@ import "file.{type}";
 
p {
  background: #0982C1;
}

最终生成的 CSS:

@ import "reset.css";
body {
  background: #EEE;
}
p {
  background: #0982C1;
}

颜色函数

CSS 预处理器一般都会内置一些颜色处理函数用来对颜色值进行处理,例如加亮、变暗、颜色梯度等。

  • Sass:
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($color1, $color2, 50%); /* mix $color1 with $color2 with a weight of 50% */

上面只是简单列了 Sass 的一些基本颜色处理函数,完整的列表请看 Sass Documentation.
Sass 例子:

$color: #0982C1;
 
h1 {
  background: $color;
  border: 3px solid darken($color, 50%);
}
  • Less CSS:
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(@color1, @color2); /* return a mix of @color1 and @color2 */

Less 例子:

@color: #0982C1;
 
h1 {
  background: @color;
  border: 3px solid darken(@color, 50%);
}
  • stylus
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 例子:
color = #0982C1
 
h1
  background color
  border 3px solid darken(color, 50%)

运算符

你可以直接在 CSS 预处理器中进行样式的计算,例如:

body {
  margin: (14px/2);
  top: 50px + 100px;
  right: 100px - 50px;
  left: 10 * 10;
}

一些跟具体浏览器相关的处理

创建一个mixin来处理不同浏览器的CSS写法是很简单的,节省了大量的重复工作和痛苦的代码编辑。
Sass:

@mixin border-radius($values) {
  -webkit-border-radius: $values;
     -moz-border-radius: $values;
          border-radius: $values;
}
 
div {
  @ include border-radius(10px);
}

Less CSS:

.border-radius(@values) {
  -webkit-border-radius: @values;
     -moz-border-radius: @values;
          border-radius: @values;
}
 
div {
  .border-radius(10px);
}

Stylus:

border-radius(values) {
  -webkit-border-radius: values;
     -moz-border-radius: values;
          border-radius: values;
}
 
div {
  border-radius(10px);
}

编译结果:

div {
  -webkit-border-radius: 10px;
     -moz-border-radius: 10px;
          border-radius: 10px;
}

以上三种框架都支持形如 /* */ 的多行注释以及 // 的单行注释。

总结

Less 从语言特性的设计到功能的健壮程度和另外两者相比都有一些缺陷,但用 Less 可以满足大多数场景的需求。
但相比另外两者,基于 Less 开发类库会复杂得多,实现的代码会比较脏,能实现的功能也会受到 DSL 的制约。

Sass 在三者之中历史最久,也吸收了其他两者的一些优点。
从功能上来说 Sass 大而全,语义明晰但是代码很容易显得累赘。
主项目基于 Ruby 可能也是一部分人不选择它的理由(Less 开始也是基于 Ruby 开发,后来逐渐转到 less.js 项目中)。
Sass 有一个「事实标准」库——Compass,于是对于很多开发者而言省去了选择类库的烦恼,对于提升开发效率也有不小的帮助。

Stylus 的语法非常灵活,很多语义都是根据上下文隐含的。
基于 Stylus 可以写出非常简洁的代码,但对使用团队的开发素养要求也更高,更需要有良好的开发规范或约定。

总的来说,三种预处理器的功能是类似的。

本文为根据网络整理,纯属自学使用,参考原文地址:
https://www.oschina.net/question/12_44255

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值