跳至内容
在此页面上

CSS 样式语法

Stylus 透明地支持常规 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

由于大括号、冒号和分号是可选的,因此我们可以像使用普通 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 的语法,但它甚至可以理解此类代码

      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;
        }

由于我们可以混合匹配这两种变体,因此以下内容也同样有效

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 提供的所有其他功能仍然按预期工作

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 的其他强大功能。