跳到內容
在此頁面

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