Skip to content
On this page

使用 Stylus 进行样式设计

安装 Stylus 非常简单,只要你已经安装了 Node.js。 所以先获取适合你平台的二进制文件,并确保它们包含 npm(Node 的包管理器)。

现在,在你的终端中输入:

bash
# npm
$ npm install stylus -g

# pnpm
$ pnpm add -g stylus
# npm
$ npm install stylus -g

# pnpm
$ pnpm add -g stylus

如果你想要一个适用于 nodejs 的富有表现力的 CSS 语言,并且具有以下特性或下面列出的特性,请前往 GitHub 获取更多信息。

Stylus 命令行工具

Stylus 附带了 stylus 可执行文件,用于将 Stylus 转换为 CSS。

bash
用法: stylus [选项] [命令] [< 输入 [> 输出]]
              [文件|目录 ...]

命令:

  help [<类型>:]<属性> 在默认浏览器中打开 MDC 上 <属性> 的帮助信息。
                        可选择搜索其他资源类型:
                        safari opera w3c ms caniuse quirksmode

选项:

  -i, --interactive       启动交互式 REPL
  -u, --use <>        使用位于 <>  Stylus 插件
  -U, --inline            通过数据 URI 支持启用图像内联
  -w, --watch             监视文件变化并重新编译
  -o, --out <>        传递文件时输出到 <>
  -C, --css <> [目标]   将 CSS 输入转换为 Stylus
  -I, --include <>    添加 <> 到查找路径
  -c, --compress          压缩 CSS 输出
  -d, --compare           显示输入和输出
  -f, --firebug           在生成的 CSS 中发出调试信息,
                          可以被 FireStylus Firebug 插件使用
  -l, --line-numbers      在生成的 CSS 中发出注释,
                          指示对应的 Stylus 
  -m, --sourcemap          sourcemaps v3 格式生成源映射
  --sourcemap-inline       base64 格式内联包含完整源文本的源映射
  --sourcemap-root <url>  生成的源映射的 "sourceRoot" 属性
  --sourcemap-base <> 源映射和所有源的相对基础 <>
  -P, --prefix [前缀]     为所有 CSS 类添加前缀
  -p, --print             打印编译后的 CSS
  --import <>         导入 Stylus <>
  --include-css            @import 中包含常规 CSS
  -D, --deps              显示已编译文件的依赖项
  --disable-cache         禁用缓存
  --hoist-atrules          @import  @charset 移到顶部
  -r, --resolve-url       解析导入中的相对 URL
  --resolve-url-nocheck   类似 --resolve-url,但不检查文件是否存在
  -V, --version           显示 Stylus 版本
  -h, --help              显示帮助信息
用法: stylus [选项] [命令] [< 输入 [> 输出]]
              [文件|目录 ...]

命令:

  help [<类型>:]<属性> 在默认浏览器中打开 MDC 上 <属性> 的帮助信息。
                        可选择搜索其他资源类型:
                        safari opera w3c ms caniuse quirksmode

选项:

  -i, --interactive       启动交互式 REPL
  -u, --use <>        使用位于 <>  Stylus 插件
  -U, --inline            通过数据 URI 支持启用图像内联
  -w, --watch             监视文件变化并重新编译
  -o, --out <>        传递文件时输出到 <>
  -C, --css <> [目标]   将 CSS 输入转换为 Stylus
  -I, --include <>    添加 <> 到查找路径
  -c, --compress          压缩 CSS 输出
  -d, --compare           显示输入和输出
  -f, --firebug           在生成的 CSS 中发出调试信息,
                          可以被 FireStylus Firebug 插件使用
  -l, --line-numbers      在生成的 CSS 中发出注释,
                          指示对应的 Stylus 
  -m, --sourcemap          sourcemaps v3 格式生成源映射
  --sourcemap-inline       base64 格式内联包含完整源文本的源映射
  --sourcemap-root <url>  生成的源映射的 "sourceRoot" 属性
  --sourcemap-base <> 源映射和所有源的相对基础 <>
  -P, --prefix [前缀]     为所有 CSS 类添加前缀
  -p, --print             打印编译后的 CSS
  --import <>         导入 Stylus <>
  --include-css            @import 中包含常规 CSS
  -D, --deps              显示已编译文件的依赖项
  --disable-cache         禁用缓存
  --hoist-atrules          @import  @charset 移到顶部
  -r, --resolve-url       解析导入中的相对 URL
  --resolve-url-nocheck   类似 --resolve-url,但不检查文件是否存在
  -V, --version           显示 Stylus 版本
  -h, --help              显示帮助信息

STDIO 编译示例

stylusstdin 读取并输出到 stdout,例如:

bash
$ stylus --compress < some.styl > some.css
$ stylus --compress < some.styl > some.css

在终端中尝试 Stylus!输入以下内容,按 CTRL-D 表示 __EOF__

bash
$ stylus
body
  color red
  font 14px Arial, sans-serif
$ stylus
body
  color red
  font 14px Arial, sans-serif

编译文件示例

stylus 还接受文件和目录。例如,名为 css 的目录将编译并输出 .css 文件到同一目录。

bash
$ stylus css
$ stylus css

以下将输出到 ./public/stylesheets

bash
$ stylus css --out public/stylesheets
$ stylus css --out public/stylesheets

或者几个文件:

bash
$ stylus one.styl two.styl
$ stylus one.styl two.styl

出于开发目的,你可以使用 linenos 选项在生成的 CSS 中发出指示 Stylus 文件名和行号的注释:

bash
$ stylus --line-numbers <>
$ stylus --line-numbers <>

或者如果你想使用 FireStylus 扩展用于 Firebug,可以使用 firebug 选项:

bash
$ stylus --firebug <>
$ stylus --firebug <>

前缀类

stylus 可执行文件通过 --prefix 选项提供了一种使用给定 [前缀] 前缀所有生成样式的方法,

bash
$ stylus --prefix foo-
$ stylus --prefix foo-

与以下代码一起使用:

stylus
.bar
  width: 10px
.bar
  width: 10px

将生成:

css
.foo-bar {
  width: 10px;
}
.foo-bar {
  width: 10px;
}

所有的类都会被前缀化:插值、扩展等。

转换 CSS

如果你希望将 CSS 转换为简洁的 Stylus 语法,请使用 --css 标志。

通过 stdio:

bash
$ stylus --css < test.css > test.styl
$ stylus --css < test.css > test.styl

输出同名的 .styl 文件:

bash
$ stylus --css test.css
$ stylus --css test.css

输出到特定目标:

bash
$ stylus --css test.css /tmp/out.styl
$ stylus --css test.css /tmp/out.styl

CSS 属性帮助

在 OS X 上,stylus help <属性> 将打开默认浏览器并显示给定 <属性> 的帮助文档。

bash
$ stylus help box-shadow
$ stylus help box-shadow

交互式 Shell

Stylus REPL(读取-求值-打印-循环)或"交互式 shell"允许你直接从终端尝试 Stylus 表达式。

注意,这仅适用于表达式——不适用于选择器等。要使用,只需添加 -i--interactive 标志:

bash
$ stylus -i
> color = white
=> #fff
> color - rgb(200,50,0)
=> #37cdff
> color
=> #fff
> color -= rgb(200,50,0)
=> #37cdff
> color
=> #37cdff
> rgba(color, 0.5)
=> rgba(55,205,255,0.5)
$ stylus -i
> color = white
=> #fff
> color - rgb(200,50,0)
=> #37cdff
> color
=> #fff
> color -= rgb(200,50,0)
=> #37cdff
> color
=> #37cdff
> rgba(color, 0.5)
=> rgba(55,205,255,0.5)

解析导入文件中的相对 URL

默认情况下,Stylus 不会解析导入的 .styl 文件中的 URL,所以如果你有一个 foo.styl 文件,其中包含 @import "bar/bar.styl",而后者有 url("baz.png"),那么在结果 CSS 中它也会是 url("baz.png")

但你可以通过使用 --resolve-url(或简写为 -r)CLI 选项来改变这种行为,以便在结果 CSS 中得到 url("bar/baz.png")

列出依赖项

你可以使用 --deps(或简写为 -D)标志获取已编译文件的依赖项列表。

例如,假设我们有 test.styl

stylus
@import 'foo'
@import 'bar'
@import 'foo'
@import 'bar'

而在 foo.styl 中:

stylus
@import 'baz'
@import 'baz'

运行:

bash
$ stylus --deps test.styl
$ stylus --deps test.styl

将给出导入路径列表:

bash
foo.styl
baz.styl
bar.styl
foo.styl
baz.styl
bar.styl

注意,目前这不适用于动态生成的路径

使用插件

在这个例子中,我们将使用 nib Stylus 插件来说明其 CLI 用法。

假设我们有以下 Stylus,导入 nib 以使用其 linear-gradient() 函数。

stylus
@import 'nib'

body
  background: linear-gradient(20px top, white, black)
@import 'nib'

body
  background: linear-gradient(20px top, white, black)

我们第一次尝试通过 stdio 使用 stylus(1) 渲染可能如下所示:

bash
$ stylus < test.styl
$ stylus < test.styl

这将产生以下错误(因为 Stylus 不知道在哪里找到 nib)。

bash
Error: stdin:3
  1|
  2|
> 3| @import 'nib'
  4|
  5| body
  6|   background: linear-gradient(20px top, white, black)
Error: stdin:3
  1|
  2|
> 3| @import 'nib'
  4|
  5| body
  6|   background: linear-gradient(20px top, white, black)

对于仅提供 Stylus API 的插件,我们可以添加路径到 Stylus 查找路径。我们通过使用 --include-I 标志来实现:

bash
$ stylus < test.styl --include ../nib/lib
$ stylus < test.styl --include ../nib/lib

现在输出如下。(正如你可能注意到的,对 gradient-data-uri()create-gradient-image() 的调用输出为字面量。这是因为仅公开库路径不足以在插件提供 JavaScript API 时使用。但是,如果我们只想使用纯 Stylus nib 函数,我们就可以了。)

css
body {
  background: url(gradient-data-uri(create-gradient-image(20px, top)));
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}
body {
  background: url(gradient-data-uri(create-gradient-image(20px, top)));
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}

所以,我们需要做的是使用 --use-u 标志。它期望一个节点模块的路径(带或不带 .js 扩展名)。这将 require() 模块,期望导出一个函数作为 module.exports,然后调用 style.use(fn()) 来公开插件(定义其 js 函数等)。

bash
$ stylus < test.styl --use ../nib/lib/nib
$ stylus < test.styl --use ../nib/lib/nib

产生预期的结果:

css
body {
  background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAUCAYAAABMDlehAAAABmJLR0QA/wD/AP+gvaeTAAAAI0lEQVQImWP4+fPnf6bPnz8zMH358oUBwkIjKJBgYGNj+w8Aphk4blt0EcMAAAAASUVORK5CYII=");
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}
body {
  background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAUCAYAAABMDlehAAAABmJLR0QA/wD/AP+gvaeTAAAAI0lEQVQImWP4+fPnf6bPnz8zMH358oUBwkIjKJBgYGNj+w8Aphk4blt0EcMAAAAASUVORK5CYII=");
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
  background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
  background: -moz-linear-gradient(top, #fff 0%, #000 100%);
  background: linear-gradient(top, #fff 0%, #000 100%);
}

如果你需要向插件传递参数,请使用 --with 选项。--with 评估任何有效的 JavaScript 表达式并将其值传递给插件。例如:

stylus
$ stylus < test.styl --use ../node_modules/autoprefixer-stylus --with "{ browsers: ['ie 7', 'ie 8'] }"
$ stylus < test.styl --use ../node_modules/autoprefixer-stylus --with "{ browsers: ['ie 7', 'ie 8'] }"

Released under the MIT License.