Appearance
使用 Stylus 编写样式
¥Get styling with Stylus
拥有 Node.js 后,安装 Stylus 就非常容易了。因此,获取适合你平台的二进制文件,并确保它们还包含 npm(Node 的包管理器)。
¥Installing Stylus is very easy once you have Node.js. So get the binaries for your platform and make sure that they also include npm, Node's package manager.
现在,在你的终端中输入:
¥Now, type in your terminal:
# npm
$ npm install stylus -g
# pnpm
$ pnpm add -g stylus
# npm
$ npm install stylus -g
# pnpm
$ pnpm add -g stylus
如果你想要一种具有这些功能或下面列出的功能的 Nodejs 富有表现力的 css 语言,请前往 GitHub 了解更多信息。
¥If you want an expressive css language for nodejs with these features or the features listed below, head over to GitHub for more information.
Stylus 命令行接口
¥Stylus cli
Stylus 附带 stylus 可执行文件,用于将 Stylus 转换为 CSS。
¥Stylus ships with the stylus executable for converting Stylus to CSS.
Usage: stylus [options] [command] [< in [> out]]
[file|dir ...]
Commands:
help [<type>:]<prop> Opens help info at MDC for <prop> in
your default browser. Optionally
searches other resources of <type>:
safari opera w3c ms caniuse quirksmode
Options:
-i, --interactive Start interactive REPL
-u, --use <path> Utilize the Stylus plugin at <path>
-U, --inline Utilize image inlining via data URI support
-w, --watch Watch file(s) for changes and re-compile
-o, --out <dir> Output to <dir> when passing files
-C, --css <src> [dest] Convert CSS input to Stylus
-I, --include <path> Add <path> to lookup paths
-c, --compress Compress CSS output
-d, --compare Display input along with output
-f, --firebug Emits debug infos in the generated CSS that
can be used by the FireStylus Firebug plugin
-l, --line-numbers Emits comments in the generated CSS
indicating the corresponding Stylus line
-m, --sourcemap Generates a sourcemap in sourcemaps v3 format
--sourcemap-inline Inlines sourcemap with full source text in base64 format
--sourcemap-root <url> "sourceRoot" property of the generated sourcemap
--sourcemap-base <path> Base <path> from which sourcemap and all sources are relative
-P, --prefix [prefix] Prefix all css classes
-p, --print Print out the compiled CSS
--import <file> Import stylus <file>
--include-css Include regular CSS on @import
-D, --deps Display dependencies of the compiled file
--disable-cache Disable caching
--hoist-atrules Move @import and @charset to the top
-r, --resolve-url Resolve relative urls inside imports
--resolve-url-nocheck Like --resolve-url but without file existence check
-V, --version Display the version of Stylus
-h, --help Display help information
Usage: stylus [options] [command] [< in [> out]]
[file|dir ...]
Commands:
help [<type>:]<prop> Opens help info at MDC for <prop> in
your default browser. Optionally
searches other resources of <type>:
safari opera w3c ms caniuse quirksmode
Options:
-i, --interactive Start interactive REPL
-u, --use <path> Utilize the Stylus plugin at <path>
-U, --inline Utilize image inlining via data URI support
-w, --watch Watch file(s) for changes and re-compile
-o, --out <dir> Output to <dir> when passing files
-C, --css <src> [dest] Convert CSS input to Stylus
-I, --include <path> Add <path> to lookup paths
-c, --compress Compress CSS output
-d, --compare Display input along with output
-f, --firebug Emits debug infos in the generated CSS that
can be used by the FireStylus Firebug plugin
-l, --line-numbers Emits comments in the generated CSS
indicating the corresponding Stylus line
-m, --sourcemap Generates a sourcemap in sourcemaps v3 format
--sourcemap-inline Inlines sourcemap with full source text in base64 format
--sourcemap-root <url> "sourceRoot" property of the generated sourcemap
--sourcemap-base <path> Base <path> from which sourcemap and all sources are relative
-P, --prefix [prefix] Prefix all css classes
-p, --print Print out the compiled CSS
--import <file> Import stylus <file>
--include-css Include regular CSS on @import
-D, --deps Display dependencies of the compiled file
--disable-cache Disable caching
--hoist-atrules Move @import and @charset to the top
-r, --resolve-url Resolve relative urls inside imports
--resolve-url-nocheck Like --resolve-url but without file existence check
-V, --version Display the version of Stylus
-h, --help Display help information
STDIO 编译示例
¥STDIO Compilation Example
stylus 从 stdin 读取并输出到 stdout,例如:
¥stylus reads from stdin and outputs to stdout, so for example:
$ stylus --compress < some.styl > some.css
$ stylus --compress < some.styl > some.css
在终端中尝试使用 Stylus!输入以下内容并按 CTRL-D 获取 __EOF__:
¥Try Stylus some in the terminal! Type below and press CTRL-D for __EOF__:
$ stylus
body
color red
font 14px Arial, sans-serif
$ stylus
body
color red
font 14px Arial, sans-serif
编译文件示例
¥Compiling Files Example
stylus 还接受文件和目录。例如,名为 css 的目录将编译并输出同一目录中的 .css 个文件。
¥stylus also accepts files and directories. For example, a directory named css will compile and output .css files in the same directory.
$ stylus css
$ stylus css
以下将输出到 ./public/stylesheets:
¥The following will output to ./public/stylesheets:
$ stylus css --out public/stylesheets
$ stylus css --out public/stylesheets
或者一些文件:
¥Or a few files:
$ stylus one.styl two.styl
$ stylus one.styl two.styl
出于开发目的,你可以使用 linenos 选项触发注释,指示生成的 CSS 中的 Stylus 文件名和行号:
¥For development purposes, you can use the linenos option to emit comments indicating the Stylus filename and line number in the generated CSS:
$ stylus --line-numbers <path>
$ stylus --line-numbers <path>
或者如果你想使用 Firebug 的 FireStylus 扩展,请选择 firebug 选项:
¥Or the firebug option if you want to use the FireStylus extension for Firebug:
$ stylus --firebug <path>
$ stylus --firebug <path>
给类添加前缀
¥Prefixing classes
stylus 可执行文件为你提供了一种使用 --prefix 选项和给定 [prefix] 为所有生成的样式添加前缀的方法,
¥stylus executable provides you a way to prefix all the generated styles using --prefix option with given [prefix],
$ stylus --prefix foo-
$ stylus --prefix foo-
与此代码一起使用:
¥used with this code:
.bar
width: 10px
.bar
width: 10px
会产生
¥would yield
.foo-bar {
width: 10px;
}
.foo-bar {
width: 10px;
}
所有类都将带有前缀:插值、扩展等
¥All the classes would be prefixed: interpolated, extended etc.
转换 CSS
¥Converting CSS
如果你希望将 CSS 转换为简洁的 Stylus 语法,请使用 --css 标志。
¥If you wish to convert CSS to the terse Stylus syntax, use the --css flag.
通过工作室:
¥Via stdio:
$ stylus --css < test.css > test.styl
$ stylus --css < test.css > test.styl
输出具有相同基名的 .styl 文件:
¥Output a .styl file of the same basename:
$ stylus --css test.css
$ stylus --css test.css
输出到特定目的地:
¥Output to a specific destination:
$ stylus --css test.css /tmp/out.styl
$ stylus --css test.css /tmp/out.styl
CSS 属性帮助
¥CSS Property Help
在 OS X 上,stylus help <prop> 将打开你的默认浏览器并显示给定 <prop> 的帮助文档。
¥On OS X, stylus help <prop> will open your default browser and display help documentation for the given <prop>.
$ stylus help box-shadow
$ stylus help box-shadow
交互式 Shell
¥Interactive Shell
Stylus REPL(读取-评估-打印-循环)或 "交互式外壳" 允许你直接从终端使用 Stylus 表达式。
¥The Stylus REPL (Read-Eval-Print-Loop) or "interactive shell" allows you to play around with Stylus expressions directly from your terminal.
请注意,这仅适用于表达式,不适用于选择器等。要使用简单,请添加 -i 或 --interactive 标志:
¥Note that this works only for expressions—not selectors, etc. To use simple add the -i, or --interactive flag:
$ 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
¥Resolving relative urls inside imports
默认情况下,Stylus 不会解析导入的 .styl 文件中的 url,因此,如果你碰巧有一个 foo.styl 和 @import "bar/bar.styl",其中包含 url("baz.png"),那么在生成的 CSS 中它也将是 url("baz.png")。
¥By default Stylus don't resolve the urls in imported .styl files, so if you'd happen to have a foo.styl with @import "bar/bar.styl" which would have url("baz.png"), it would be url("baz.png") too in a resulting CSS.
但是你可以通过使用 --resolve-url(或仅 -r)选项来更改此行为,以在生成的 CSS 中获取 url("bar/baz.png")。
¥But you can alter this behavior by using --resolve-url (or just -r) option to get url("bar/baz.png") in your resulting CSS.
列出依赖
¥List dependencies
你可以使用 --deps(或仅 -D)标志来获取已编译文件的依赖列表。
¥You can use --deps (or just -D) flag to get a list of dependencies of the compiled file.
例如,假设我们有 test.styl:
¥For example, suppose we have test.styl:
@import 'foo'
@import 'bar'
@import 'foo'
@import 'bar'
在 foo.styl 内部:
¥And inside foo.styl:
@import 'baz'
@import 'baz'
运行:
¥Running:
$ stylus --deps test.styl
$ stylus --deps test.styl
将为我们提供导入路径列表:
¥Will give us list of the imports paths:
foo.styl
baz.styl
bar.styl
foo.styl
baz.styl
bar.styl
请注意,目前这不适用于动态生成的路径。
¥Note that currently this does not works for dynamically generated paths.
使用插件
¥Utilizing Plugins
在本示例中,我们将使用 nib Stylus 插件来说明其 CLI 用法。
¥For this example we'll use the nib Stylus plugin to illustrate its CLI usage.
假设我们有下面的 Stylus,它导入 nib 来使用它的 linear-gradient() 功能。
¥Suppose we have the following Stylus, which imports nib to use its linear-gradient() function.
@import 'nib'
body
background: linear-gradient(20px top, white, black)
@import 'nib'
body
background: linear-gradient(20px top, white, black)
我们第一次尝试通过 stdio 使用 stylus(1) 进行渲染可能如下所示:
¥Our first attempt to render using stylus(1) via stdio might look like this:
$ stylus < test.styl
$ stylus < test.styl
这会产生以下错误(因为 Stylus 不知道在哪里找到 Nib)。
¥Which would yield the following error (because Stylus doesn't know where to find nib).
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 标志来做到这一点:
¥For plugins that simply supply Stylus APIs, we could add the path to the Stylus lookup paths. We do so by using the --include or -I flag:
$ stylus < test.styl --include ../nib/lib
$ stylus < test.styl --include ../nib/lib
现在产生以下输出。(你可能会注意到,对 gradient-data-uri() 和 create-gradient-image() 的调用以字面量形式输出。这是因为当插件提供 JavaScript API 时,公开库路径是不够的。但是,如果我们只想使用纯 Stylus nib 函数,我们' 没问题。)
¥Now yielding the output below. (As you might notice, calls to gradient-data-uri() and create-gradient-image() output as literals. This is because exposing the library path isn't enough when a plugin provides a JavaScript API. However, if we only wanted to use pure-Stylus nib functions, we'd be fine.)
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 函数等)。
¥So, what we need to do is use the --use, or -u flag. It expects a path to a node module (with or without the .js extension). This require()s the module, expecting a function to be exported as module.exports, which then calls style.use(fn()) to expose the plugin (defining its js functions, etc.).
$ stylus < test.styl --use ../nib/lib/nib
$ stylus < test.styl --use ../nib/lib/nib
产生预期结果:
¥Yielding the expected result:
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 表达式并将其值传递给插件。例如:
¥If you need to pass arguments to the plugin, use the --with option. --with evaluates any valid javascript expression and passes its value to the plugin. For example:
$ 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'] }"