Google 已经基于 Apache License 2.0 把 Closure Stylesheets 开源,这种工具属于 Closure Tools 包之内,在处理 CSS 的时候很有用。Closure Stylesheets 是一个 Java 程序,它向 CSS 中添加了变量、函数、条件语句以及混合类型,使得我们更易于处理大型的 CSS 文件。开发者可以使用 Google stylesheets (GSS)这种工具来生成 web 应用程序或者网站所使用的真正的 CSS 文件。
变量
变量是使用“@def”来定义的。下面的代码示例展示了如何使用变量:
01 | @def BG_COLOR rgb (235, 239, 249); |
03 | @def DIALOG_BG_COLOR BG_COLOR;body { |
05 | background-color: BG_COLOR; |
11 | background-color: DIALOG_BG_COLOR; |
19 | background-color: #ebeff9; |
25 | background-color: #ebeff9; |
函数
Closure Stylesheets 引入了大量数学函数,使用它们你可以对数字型的值——比方说像素——进行以下操作: add ()、 sub ()、mult ()、 div ()、 min ()以及max ()。使用这些函数的示例如下:
01 | @def LEFT_WIDTH 100px; |
03 | @def LEFT_PADDING 5px; |
05 | @def RIGHT_PADDING 5px;.content { |
09 | margin-left: add (LEFT_PADDING, |
得到的 CSS 如下所示:
条件语句
Closure Stylesheets 让我们可以使用@if、@elseif 和@else,从而基于某些变量的值来创建条件语句的分支。
混合类型
混合类型是为了重用带有参数的对结构体的声明,如下示例所示:
01 | @defmixin size (WIDTH, HEIGHT) { |
11 | @mixin size (200px, 300px); |
当解决跨浏览器的问题时,混合类型会更有用:
01 | @defmixin gradient (POS, HSL1, HSL2, HSL3, COLOR, FALLBACK_COLOR) { |
03 | background-color: FALLBACK_COLOR; |
05 | background-image: -webkit-linear-gradient (POS, hsl (HSL1, HSL2, HSL3), COLOR); |
07 | background-image: -moz-linear-gradient (POS, hsl (HSL1, HSL2, HSL3), COLOR); |
09 | background-image: -ms-linear-gradient (POS, hsl (HSL1, HSL2, HSL3), COLOR); |
11 | background-image: -o-linear-gradient (POS, hsl (HSL1, HSL2, HSL3), COLOR); |
17 | @mixin gradient (top, 0%, 50%, 70%, #cc0000, #f07575); |
结果如下:
03 | background-color: #f07575; |
05 | background-image: -webkit-linear-gradient (top,hsl (0%,50%,70%) ,#cc0000); |
07 | background-image: -moz-linear-gradient (top,hsl (0%,50%,70%) ,#cc0000); |
09 | background-image: -ms-linear-gradient (top,hsl (0%,50%,70%) ,#cc0000); |
11 | background-image: -o-linear-gradient (top,hsl (0%,50%,70%) ,#cc0000); |
我们还可以使用 Closure Stylesheets 把多个 CSS 文件合并成一个,以减少代码的规模,它会针对语法执行静态检查,并且知道如何交换左右两边的值(RTL flipping),以及如何对类进行重命名。
Closure Tools 是一组工具,其中包括编译器、程序库和模板,它原本是 Google 为 GMail、GDocs 和 Maps 内部使用,后来在2009年开源。我们可以使用它来处理大型 JavaScript 应用程序。
查看英文原文:Google Closure Stylesheets Makes It Easier to Work with CSS