Skip to content
On this page

CSS 样式语法

¥CSS Style Syntax

Stylus 透明地支持常规 CSS 样式语法。这意味着你不需要替代解析器,或指定某个文件使用特定样式。

¥Stylus transparently supports a regular CSS-style syntax. This means you don't need an alternative parser, or specify that a certain file uses a specific style.

示例

¥Example

下面是使用缩进方法的小样式:

¥Below is a small style using the indented approach:

border-radius()
 -webkit-border-radius arguments
 -moz-border-radius arguments
 border-radius arguments

body a
 font 12px/1.4 "Lucida Grande", Arial, sans-serif
 background black
 color #ccc

form input
 padding 5px
 border 1px solid
 border-radius 5px
border-radius()
 -webkit-border-radius arguments
 -moz-border-radius arguments
 border-radius arguments

body a
 font 12px/1.4 "Lucida Grande", Arial, sans-serif
 background black
 color #ccc

form input
 padding 5px
 border 1px solid
 border-radius 5px

由于大括号、冒号和分号是可选的,因此我们可以像使用普通 CSS 一样编写此示例:

¥Since braces, colons, and semi-colons are optional, we could write this example just as we would with normal CSS:

border-radius() {
  -webkit-border-radius: arguments;
  -moz-border-radius: arguments;
  border-radius: arguments;
}

body a {
  font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
  background: black;
  color: #ccc;
}

form input {
  padding: 5px;
  border: 1px solid;
  border-radius: 5px;
}
border-radius() {
  -webkit-border-radius: arguments;
  -moz-border-radius: arguments;
  border-radius: arguments;
}

body a {
  font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
  background: black;
  color: #ccc;
}

form input {
  padding: 5px;
  border: 1px solid;
  border-radius: 5px;
}

虽然 Stylus 不支持所有可能的类似 CSS 的语法,但它甚至可以理解这样的代码:

¥While Stylus don't support every possible CSS-like syntax, it can understand even such code:

      border-radius() {
        -webkit-border-radius: arguments;
        -moz-border-radius: arguments;
        border-radius: arguments;
      }

  body a
  {
    font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
      background: black;
    color: #ccc;
  }

      form input {
        padding: 5px;
    border: 1px solid;
        border-radius: 5px;
        }
      border-radius() {
        -webkit-border-radius: arguments;
        -moz-border-radius: arguments;
        border-radius: arguments;
      }

  body a
  {
    font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
      background: black;
    color: #ccc;
  }

      form input {
        padding: 5px;
    border: 1px solid;
        border-radius: 5px;
        }

由于我们可以混合和匹配这两个变体,因此以下内容也是有效的:

¥Since we may mix and match the two variants, the following is valid as well:

border-radius()
  -webkit-border-radius: arguments;
  -moz-border-radius: arguments;
  border-radius: arguments;

body a {
  font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
  background: black;
  color: #ccc;
}

form input
  padding: 5px;
  border: 1px solid;
  border-radius: 5px;
border-radius()
  -webkit-border-radius: arguments;
  -moz-border-radius: arguments;
  border-radius: arguments;

body a {
  font: 12px/1.4 "Lucida Grande", Arial, sans-serif;
  background: black;
  color: #ccc;
}

form input
  padding: 5px;
  border: 1px solid;
  border-radius: 5px;

Stylus 提供的变量、函数、mixins 和所有其他功能仍然按预期工作:

¥Variables, functions, mixins, and all the other features provided by Stylus still work as expected:

main-color = white
main-hover-color = black

body a {
 color: main-color;
 &:hover { color: main-hover-color; }
}

body a { color: main-color; &:hover { color: main-hover-color; }}
main-color = white
main-hover-color = black

body a {
 color: main-color;
 &:hover { color: main-hover-color; }
}

body a { color: main-color; &:hover { color: main-hover-color; }}

此规则有一些注意事项:由于两种样式可能混合和匹配,因此一些缩进规则仍然适用。因此,虽然并非每个纯 CSS 样式表都可以进行零修改,但此功能允许那些喜欢 CSS 语法的人继续这样做,同时利用 Stylus 的其他强大功能。

¥There are a few caveats to this rule: since the two styles may be mixed and matched, some indentation rules still apply. So although not every plain-CSS stylesheet will work with zero modification, this feature allows those who prefer CSS syntax to continue doing so while leveraging Stylus' other powerful features.