比较三个 CSS 预处理器:Sass、LESS 和 Stylus(下)

2023-01-07,,,

五、Mixins (混入)

  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 CSS 的混入语法:

/* 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 */
}

  最终它们都将编译成如下的 CSS 样式:

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

  在这里首先定义 .block 块,然后让 p 、ul 和 ol 元素继承 .block ,最终生成的 CSS 如下:

.block, p, ul, ol {
margin: 10px 5px;
padding: 2px;
}
p {
border: 1px solid #EEE;
}
ul, ol {
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;
}

  生成的 CSS 如下:

.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:

/* 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.

  下面是一个具体的例子:

$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 完整的颜色函数列表请看 LESS Documentation.

  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 Documentation.

  实例:

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

 3D文本

  要生成具有 3D 效果的文本可以使用 text-shadows ,唯一的问题就是当要修改颜色的时候就非常的麻烦,而通过 mixin 和颜色函数可以很轻松的实现:

  Sass:

@mixin text3d($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;
} h1 {
font-size: 32pt;
@ include text3d(#0982c1);
}

  Less CSS

.text3d(@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;
.text3d(#0982c1);
}

  Stylus

text3d(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
text3d(#0982c1)

  生成的 CSS

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

$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

@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

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

  实际效果

body {
margin: 0 auto;
width: 1024px;
}
.content {
float: left;
width: 704px;
}
.sidebar {
float: left;
margin-left: 20px;
width: 300px;
}

你们的支持,是我坚持的动力~

比较三个 CSS 预处理器:Sass、LESS 和 Stylus(下)的相关教程结束。

《比较三个 CSS 预处理器:Sass、LESS 和 Stylus(下).doc》

下载本文的Word格式文档,以方便收藏与打印。