选择器

缩进

Stylus 语法是 “python 式”的 (即,基于缩进)。空格很重要,所以我们使用_缩进_和_突出_来代替 {},如下所示:

body
  color white

Which compiles to:

body {
  color: #fff;
}

如果喜欢,可以使用冒号来分隔属性和值:

body
  color: white

规则集

和 CSS 一样,Stylus 允许通过逗号分隔,一次为多个选择器定义属性。

textarea, input
  border 1px solid #eee

也可以通过换行书写来实现:

textarea
input
  border 1px solid #eee

最终都会编译为:

textarea,
input {
  border: 1px solid #eee;
}

The only exception to this rule are selectors that look like properties. For example, the following foo bar baz might be a property or a selector:

foo bar baz
> input
  border 1px solid

由于上述原因(或者只是喜欢如此),我们可在末尾添加一个逗号:

foo bar baz,
form input,
> a
  border 1px solid

父级引用

& 符号代表父级选择器。下面的例子中,(textareainput)两个选择器的伪类:hover都会改变 color 属性。

textarea
input
  color #A7A7A7
  &:hover
    color #000

编译为:

textarea,
input {
  color: #a7a7a7;
}
textarea:hover,
input:hover {
  color: #000;
}

下面的例子中,在混入(mixin)中使用父级引用为 IE 浏览器中的元素加了一个简单的 2px 的边框。

  box-shadow()
    -webkit-box-shadow arguments
    -moz-box-shadow arguments
    box-shadow arguments
    html.ie8 &,
    html.ie7 &,
    html.ie6 &
      border 2px solid arguments[length(arguments) - 1]

  body
    #login
      box-shadow 1px 1px 3px #eee

编译为:

  body #login {
    -webkit-box-shadow: 1px 1px 3px #eee;
    -moz-box-shadow: 1px 1px 3px #eee;
    box-shadow: 1px 1px 3px #eee;
  }
  html.ie8 body #login,
  html.ie7 body #login,
  html.ie6 body #login {
    border: 2px solid #eee;
  }

如果需要在选择器中单纯地使用&符,不使用其父级引用的功能,可以通过转义符\来转义:

.foo[title*='\&']
// => .foo[title*='&']

Partial Reference

^[N] anywhere in a selector, where N can be a number, represents a partial reference.

Partial reference works similar to the parent reference, but while parent reference contains the whole selector, partial selectors contain only the first merged N levels of the nested selectors, so you could access those nesting levels individually.

The ^[0] would give you the selector from the first level, the ^[1] would give you the rendered selector from the second level and so on:

.foo
  &__bar
    width: 10px

    ^[0]:hover &
      width: 20px

would be rendered as

.foo__bar {
  width: 10px;
}
.foo:hover .foo__bar {
  width: 20px;
}

Negative values are counting from the end, so ^[-1] would give you the last selector from the chain before &:

.foo
  &__bar
    &_baz
      width: 10px

      ^[-1]:hover &
        width: 20px

would be rendered as

.foo__bar_baz {
  width: 10px;
}
.foo__bar:hover .foo__bar_baz {
  width: 20px;
}

Negative values are especially helpful for usage inside mixins when you don’t know at what nesting level you’re calling it.


Note that partial reference contain the whole rendered chain of selectors until the given nesting level, not the “part” of the selector.

Ranges in partial references

^[N..M] anywhere in a selector, where both N and M can be numbers, represents a partial reference.

If you’d have a case when you’d need to get the raw part of the selector, or to get the range of parts programmatically, you could use ranges inside partial reference.

If the range would start from the positive value, the result won’t contain the selectors of the previous levels and you’d get the result as if the selectors of those levels were inserted at the root of the stylesheet with the combinators omitted:

.foo
  & .bar
    width: 10px

    ^[0]:hover ^[1..-1]
      width: 20px

would be rendered as

.foo .bar {
  width: 10px;
}
.foo:hover .bar {
  width: 20px;
}

One number in the range would be the start index, the second — the end index. Note that the order of those numbers won’t matter as the selectors would always render from the first levels to the last, so ^[1..-1] would be equal to the ^[-1..1].

When both numbers are equal, the result would be just one raw level of a selector, so you could replace ^[1..-1] in a previous example to ^[-1..-1], and it would be equal to the same last one raw selector, but would be more reliable if used inside mixins.

Initial Reference

The ~/ characters at the start of a selector can be used to point at the selector at the first nesting and could be considered as a shortcut to ^[0]. The only drawback is that you can use initial reference only at the start of a selector:

.block
  &__element
    ~/:hover &
      color: red

Would be rendered as

.block:hover .block__element {
  color: #f00;
}

Relative Reference

The ../ characters at the start of a selector mark a relative reference, which points to the previous to the & compiled selector. You can nest relative reference: ../../ to get deeper levels, but note that it can be used only at the start of the selector.

.foo
  .bar
    width: 10px

    &,
    ../ .baz
      height: 10px

would be rendered as

.foo .bar {
  width: 10px;
}
.foo .bar,
.foo .baz {
  height: 10px;
}

Relative references can be considered as shortcuts to the partial references with ranges like ^[0..-(N + 1)] where the N is the number of relative references used.

Root Reference

The / character at the start of a selector is a root reference. It references the root context and this means the selector won’t prepend the parent’s selector to it (unless you would use it with &). It is helpful when you need to write some styles both to some nested selector and to another one, not in the current scope.

textarea
input
  color #A7A7A7
  &:hover,
  /.is-hovered
    color #000

Compiles to:

textarea,
input {
  color: #a7a7a7;
}
textarea:hover,
input:hover,
.is-hovered {
  color: #000;
}

selector() bif

You can use the built-in selector() to get the current compiled selector. Could be used inside mixins for checks or other clever things.

.foo
  selector()
// => '.foo'

.foo
  &:hover
    selector()
// '.foo:hover'

This bif could also accept an optional string argument, in this case it would return the compiled selector. Note that it wouldn’t prepend the selector of the current scope in case it don’t have any & symbols.

.foo
  selector('.bar')
// => '.bar'

.foo
  selector('&:hover')
// '.foo:hover'

Multiple values for selector() bif

selector() bif can accept multiple values or a comma-separated list in order to create a nested selector structure easier.

{selector('.a', '.b', '.c, .d')}
  color: red

would be equal to the

.a
  .b
    .c,
    .d
      color: red

and would be rendered as

.a .b .c,
.a .b .d {
  color: #f00;
}

selectors() bif

This bif returns a comma-separated list of nested selectors for the current level:

.a
  .b
    &__c
      content: selectors()

would be rendered as

.a .b__c {
  content: '.a', '& .b', '&__c';
}

Disambiguation

Expressions such as margin - n could be interpreted both as a subtraction operation, as well as a property with an unary minus. To disambiguate, wrap the expression with parens:

pad(n)
  margin (- n)

body
  pad(5px)

Compiles to:

body {
  margin: -5px;
}

However, this is only true in functions (since functions act both as mixins, or calls with return values).

For example, the following is fine (and yields the same results as above):

body
  margin -5px

Have weird property values that Stylus can’t process? unquote() can help you out:

filter unquote('progid:DXImageTransform.Microsoft.BasicImage(rotation=1)')

Yields:

filter progid:DXImageTransform.Microsoft.BasicImage(rotation=1)
Stylus on GitHub