Comparison of CSS preprocessors — Sass, Less, and Stylus

高洛峰
Release: 2017-02-20 13:47:05
Original
1605 people have browsed it

Playing the role of CSS preprocessor is a very challenging thing. CSS preprocessors come in different languages, with different syntax and functionality.

In this article, we will introduce the power, functions and benefits of three different CSS preprocessors - sass, less and stylus.

Introduction

The CSS preprocessor is a language used to write some CSS features without considering browser compatibility issues. They compile code into regular CSS and don't stay in the Stone Age anymore. CSS preprocessors have thousands of features, and we will introduce them one by one in this article. Let's start.

Grammar

The most important thing before using a CSS preprocessor is an understanding of the syntax. Fortunately, the syntax of these three CSS preprocessors is similar to that of CSS.

sass and less

Both sass and less use standard CSS syntax. This uses CSS preprocessors to easily convert preprocessor code into CSS code. By default, sass uses the .scss extension, while less uses the .less extension. The basic settings for Sass and Less can look like this:

/* style.scss 或者 style.less */
h1 {
  color: #0982C1;
}
Copy after login

As you can see, in Sass and Less styles, such code cannot be simpler.

The important thing is that sass also supports the old syntax, that is, it does not use curly braces and semicolons, and the file uses the .sass extension. Its syntax is similar to:

/* style.sass */
h1
  color: #0982c1
Copy after login

stylus

stylus has more syntax, it uses the extension of .styl, stylus also accepts standard CSS syntax, but it also accepts without curly braces and The syntax of semicolons is as follows:

/* style.styl */
h1 {
  color: #0982C1;
}

/* 省略花括号 */
h1
  color: #0982C1;

/* 省略花括号和分号 */
h1
  color #0982C1
Copy after login

You can also use different variables in the same style sheet. For example, the following writing method will not report an error:

h1 {
  color #0982c1
}
h2
  font-size: 1.2em
Copy after login

Variables

You can declare variables in the CSS preprocessor and use them throughout the style sheet. The CSS preprocessor supports any variable (e.g. color, numeric value (with or without units), text). Then you can reference the variable anywhere.

sass

Sass declaration of variables must start with $, followed by the variable name and variable value, and the variable name and variable value need to use colons : separated, just like CSS properties:

$mainColor: #0982c1;
$siteWidth: 1024px;
$borderStyle: dotted;
body {
  color: $mainColor;
  border: 1px $borderStyle $mainColor;
  max-width: $siteWidth;
}
Copy after login

less

less declares variables the same as sass declares variables. The only difference is that the @ characters are used in front of the variable name:

@mainColor: #0982c1;
@siteWidth: 1024px;
@borderStyle: dotted;
body {
  color: @mainColor;
  border: 1px @borderStyle @mainColor;
  max-width: @siteWidth;
}
Copy after login

stylus

stylus declares variables without any restrictions. You can use the $ symbol to start, and the trailing semicolon ; is optional. But the equal sign = between the variable name and the variable value is required. One thing to note is that if we use the @ symbol to declare (0.22.4) variables, stylus will compile, but the corresponding value will not be assigned to the variable. In other words, do not use @ symbols to declare variables in stylus.

mainColor = #0982c1
siteWidth = 1024px
$borderStyle = dotted
body
  color mainColor
  border 1px $borderStyle mainColor
  max-width siteWidth
Copy after login

Translated CSS

The above codes will be translated into the same CSS. You can imagine how powerful variables are. We don't need to enter it many times to modify a color, nor do we need to search everywhere to modify a width. (We only need to modify the defined variables, and modifying them once is enough. The following is the translated CSS code:

 body {
  color: #0982c1;
  border: 1px dotted #0982c1;
  max-width: 1024px;
}
Copy after login

Nesting

If we have multiple elements in CSS With the same parent element, writing styles will become tedious. We need to write this parent element over and over again in front of each element.

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

On the contrary, using CSS preprocessor, we can write the parent element in front of each element. The curly braces {} are used to write these elements, while the & symbols can be used to refer to parent selectors

for sass, less and stylus

. For nested selectors, the three CSS preprocessors all have the same syntax:

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

Translated CSS

The CSS code translated by the above preprocessor is the same as what we started to show The CSS code is the same. It's very convenient!

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

Mixins

##Mixins are functions in the preprocessor that you must have encountered when writing styles. A certain CSS style is often used in multiple elements, so you need to write it multiple times. In the CSS preprocessor, you can define a Mixin for these common CSS styles, and then use these styles in your CSS. Where, directly call the Mixin you defined. This is a very useful feature. Mixins are a recognized selector, and you can also define variables or default parameters in Mixins.

less

 /* Sass定义了一个名叫error的mixin,这个error设置了一个参数“$borderWidth”,在没特别定义外,这个参数的值是默认值2px*/
@mixin error($borderWidth: 2px) {
  border: $borderWidth solid #F00;
  color: #F00;
}
.generic-error {
  padding: 20px;
  margin: 4px;
  @include error(); /* 调用error mixins */
}
.login-error {
  left: 12px;
  position: absolute;
  top: 20px;
  @include error(5px); /* 调用error mixins,并将参数$borderWidth的值指定为5px*/
}
Copy after login

stylus

 /* LESS 定义了一个名叫error的mixin,这个error设置了一个参数“$borderWidth”,在没特别定义外,这个参数的值是默认值2px */
.error(@borderWidth: 2px) {
  border: @borderWidth solid #F00;
  color: #F00;
}
.generic-error {
  padding: 20px;
  margin: 4px;
  .error(); /* 调用error mixins */
}
.login-error {
  left: 12px;
  position: absolute;
  top: 20px;
  .error(5px); /* 调用error mixins,并将参数$borderWidth的值指定为5px */
}
Copy after login

Translated CSS

The CSS codes translated by the above three CSS preprocessors are the same

 /* Stylus 定义了一个名叫error的mixin,这个error设置了一个参数“$borderWidth”,在没特别定义外,这个参数的值是默认值2px */
error(borderWidth= 2px) {
  border: borderWidth solid #F00;
  color: #F00;
}
.generic-error {
  padding: 20px;
  margin: 4px;
  error(); /* 调用error mixins */
}
.login-error {
  left: 12px;
  position: absolute;
  top: 20px;
  error(5px); /* 调用error mixins,并将参数$borderWidth的值指定为5px */
}
Copy after login

Mixins are different in the three preprocessors:

sass: The definition of Mixins in sass is different from less and stylus. When declaring Mixins, you need to use "@mixin", followed by the name of the Mixins. He can also define parameters and set a default value for this parameter, but the parameter name starts with the "$" symbol and is the same as the parameter value. You need to use a colon (:) to separate them. In addition, when calling Mixins in sass, you need to use "@include", followed by the name of the Mixins you want to call.
  • less:less中声明Mixins和CSS定义样式非常类似,可以将Mixins看成是一个选择器,当然Mixins也可以设置参数,并给参数设置默认值。不过设置参数的变量名是使用“@”开始,同样参数和默认参数值之间需要使用冒号(:)分开。

  • stylus:stylus和前两者也略有不同,他可以不使用任何符号,就是直接定义Mixins名,然后在定义参数和默认值之间用等号(=)来连接。

上面只是Mixins在三个CSS预处器的简单区别,详细的还可以进入他们的官网了解,或者对比一下上面的三段代码。

继承(Inheritance)

在多个元素应用相同的样式时,我们在CSS通常都是这样写:

p,
ul,
ol {
  /* 样式写在这 */
}
Copy after login

这样做非常的好,但往往我们需要给单独元素添加另外的样式,这个时候我们就需要把其中选择器单独出来写样式,这样一回来我们维护样式就相当的麻烦。为了应对这个问题,CSS预处理器可以从一个选择继承另一个选择器下的所有样式。

sass和stylus

.block {
  margin: 10px 5px;
  padding: 2px;
}
p {
  @extend .block; /* 继承.block所有样式 */
  border: 1px solid #EEE;
}
ul, ol {
  @extend .block; /* 继承.block所有样式 */
  color: #333;
  text-transform: uppercase;
}
Copy after login

转译出来的CSS

.block, p, ul, ol {
  margin: 10px 5px;
  padding: 2px;
}
p {
  border: 1px solid #EEE;
}
ul, ol {
  color: #333;
  text-transform: uppercase;
}
Copy after login

less

less支持的继承和sass与stylus不一样,他不是在选择器上继承,而是将Mixins中的样式嵌套到每个选择器里面。这种方法的缺点就是在每个选择器中会有重复的样式产生。

 .block {
  margin: 10px 5px;
  padding: 2px;
}
p {
  .block; /* 继承 '.block'中的样式 */
  border: 1px solid #EEE;
}
ul, ol {
  .block; /*继承'.block' 中的样式*/
  color: #333;
  text-transform: uppercase;
}
Copy after login

转译出来的CSS

.block {
  margin: 10px 5px;
  padding: 2px;
}
p {
  margin: 10px 5px;
  padding: 2px;
  /* 继承 '.block'中的样式 */
  border: 1px solid #EEE;
}
ul,
ol {
  margin: 10px 5px;
  padding: 2px;
  /*继承'.block' 中的样式*/
  color: #333;
  text-transform: uppercase;
}
Copy after login

正如所看到的,上面的代码.block的样式将会被插入到相应的你要继承的选择器中,但需要注意是的优先级的问题。

导入(import)

在CSS中,并不喜欢用@import来导入样式,因为这样的做法会增加HTTP的请求。但是在CSS预处理器中的导入@import规则和CSS的有所不同,它只是在语义上导入不同的文件,但最终结果是生成一个CSS文件。如果你是通过@import 'file.css'导入file.css样式文件,那效果跟普通CSS导入样式文件一样。注意:导入文件中定义了变量、混合等信息也将会被引入到主样式文件中,因此需要避免他们的相互冲突。

sass、less和stylus

/* file.{type} */
body {
  background: #EEE;
}
Copy after login
@import "reset.css";
@import "file.{type}";
p {
  background: #0982C1;
}
Copy after login

转译出来的CSS

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

颜色函数

颜色函数是CSS预处里器中内置的颜色函数功能,这些功能可以对颜色值进行处理,例如颜色的变亮、变暗、渐变颜色等处理十分的方便。

sass

lighten($color, 10%); /* 返回的颜色在$color基础上变亮10% */
darken($color, 10%);  /* 返回的颜色在$color基础上变暗10% */
saturate($color, 10%);   /* 返回的颜色在$color基础上饱和度增加10% */
desaturate($color, 10%); /* 返回的颜色在$color基础上饱和度减少10% */
grayscale($color);  /* 返回$color的灰度色*/
complement($color); /* returns complement color of $color */
invert($color);     /* 返回$color的反相色 */
mix($color1, $color2, 50%); /* mix $color1 with $color2 with a weight of 50% */
Copy after login

这只是sass中颜色函数的一个简单列表,更多详细的介绍你可以阅读 sass文档 。

颜色函数何以运用到任何一个元素上都是一个有颜色的CSS属性,下面是一个简单的例子:

$color: #0982C1;
h1 {
  background: $color;
  border: 3px solid darken($color, 50%);/*边框颜色在$color的基础上变暗50%*/
}
Copy after login

less

lighten(@color, 10%); /* 返回的颜色在@color基础上变亮10% */
darken(@color, 10%);  /* 返回的颜色在@color基础上变暗10%*/
saturate(@color, 10%);   /* 返回的颜色在@color基础上饱和度增加10% */
desaturate(@color, 10%); /* 返回的颜色在@color基础上饱和度降低10%*/
spin(@color, 10);  /* 返回的颜色在@color基础上色调增加10 */
spin(@color, -10); /* 返回的颜色在@color基础上色调减少10 */
mix(@color1, @color2); /* 返回的颜色是@color1和@color2两者的混合色 */
Copy after login

less的完整颜色函数功能,请阅读 less 文档。

下面是less中如何使用一个颜色函数的简单例子

@color: #0982C1;
h1 {
  background: @color;
  border: 3px solid darken(@color, 50%);
}
Copy after login

stylus

lighten(color, 10%); /* 返回的颜色在'color'基础上变亮10% */
darken(color, 10%);  /* 返回的颜色在'color'基础上变暗10% */
saturate(color, 10%);   /* 返回的颜色在'color'基础上饱和度增加10% */
desaturate(color, 10%); /* 返回的颜色在'color'基础上饱和度降低10% */
Copy after login

有关于stylus的颜色函数介绍,请阅读 stylus文档。

下面是stylus颜色函数的一个简单实例

color = #0982C1
h1
  background color
  border 3px solid darken(color, 50%)
Copy after login

运算符(Operations)

我们都向往在CSS做一些运算,但是无法实现。但是在CSS预处器中对样式做一些运算是一点问题都没有了,例如:

sass、less和stylus

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

实际运用

我们介绍了CSS预处理器各方面的特性,但我们还没有实战过。下面是CSS预处理器应用的一些例子。

属性前缀

这是宣传CSS预处理器的原因之一,并且是一个很好的理由,这样可以节省大量的时间和汗水。创建一个minxin来处理浏览器的前缀问题是一个很简单的,并且能节省大量的重复工作和痛苦的代码编辑,我们来看一个例子。

sass

@mixin border-radius($values) {
  -webkit-border-radius: $values;
     -moz-border-radius: $values;
          border-radius: $values;
}
p {
  @include border-radius(10px);
}
Copy after login

less

.border-radius(@values) {
  -webkit-border-radius: @values;
     -moz-border-radius: @values;
          border-radius: @values;
}
p {
  .border-radius(10px);
}
Copy after login

stylus

border-radius(values) {
  -webkit-border-radius: values;
     -moz-border-radius: values;
          border-radius: values;
}
p {
  border-radius(10px);
}
Copy after login

转译出来的CSS

p {
  -webkit-border-radius: 10px;
     -moz-border-radius: 10px;
          border-radius: 10px;
}
Copy after login

浏览器前缀的问题,主要是针对于CSS3属性的运用,众所周知,CSS3的属性有并不是所有浏览器都支持属性的标准语法,因此在实际运用中时,不得不加上各浏览器的前缀来识别,这对于我们前端人员来说是多么苦逼的一件事情。虽然有prefix这样的js脚本帮我们解决,但对于怎么说也需要额外添加一个脚本文件,这对于追求完美的同学来说可能完法接受。
现在多了一种解决方案,就是使用CSS预处理器,如上面圆角的实现方法,这样减轻了我们很多工作量。如果你对这一部分实现方法感兴趣,不仿花时间阅读一下以下源码:

  • cssmixins :由 Matthew Wagerfield 整理的CSS3属性在三种预处理器中的Mixins的定义:less 、 sass (还有 SCSS )和 stylus

  • less-Prefixer

  • Custom User @mixins

3D文本

使用text-shadow的多重属性制作3D文本效果是一个很好的方法。唯一麻烦的问题就是修改文本阴影的颜色。如果我们使用Mixin和颜色函数的话,实现3D文本效果就非常的轻松了,我们来尝试一下。

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);
}
Copy after login

less

.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);
}
Copy after login

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)
Copy after login

在stylus中text-shadow的样式写在一行,是因为stylus中省略了花括号{}和分号;
上面三种方法实现的效果都是一样的:

CSS预处理器的对比 — Sass、Less和Stylus

列(Columns)

我第一次接触CSS预处理器的时候,我就想着使用数字或者变量进行运算。使用数值和变量运算,可以很方便的实现适应屏幕大小的布局处理。只需要定义宽度的变量,我们就可以很方便的根据需求实现布局。下面的例子就是这么做的。

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;
}
Copy after login

less

@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;
}
Copy after login

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;
}
Copy after login

转译出来的CSS

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

错误报告

如果你经常使用CSS,你会发现很难找到CSS中出错的地方。也许你也会像我一样,花一下午的时间,发了疯的注解每行样式代码来寻找这个CSS错误。

CSS预处理器就轻松多了,他会给你报告错误。你可以阅读这篇文章,学习如何让CSS预处理器报告错误。

注释(Comments)

CSS预处理器支持/* */这样的多行注释方式(类似于CSS的注释方式),也支持//单行注释方式(类似于Javascript的注释方式)。

需要注意,如果你要压缩文件,你需要把所有注释都删除。

总结

三个预处理器我们都覆盖了(sass、less和stylus),都以他们独特的特性完成了相同的效果。这样让开发人员更好的选择适合自己的CSS预处理器,从而更好的维护自己的代码,提高开发效率。

虽然不是开发的要求,但CSS预处理器可以节省大量的时间,并且有一些非常有用的功能。

我鼓励大家尽可能的尝试使用CSS预处理器,这样就可以有效的让你选择一个你最喜欢的和知道他为什么是受人青睐的。如果你还没有尝试过使用CSS预处理器来写你的CSS样式,我强烈建议你尝试一下。
如果你有最喜欢的CSS预处理器的功能,我并没有提到的,希望在下面的评论中与我们分享。

译者手语: 整个翻译依照原文线路进行,并在翻译过程略加了个人对技术的理解。如果翻译有不对之处,还烦请同行朋友指点。谢谢!

更多CSS预处理器的对比 — Sass、Less和Stylus 相关文章请关注PHP中文网!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template