Parent Selectors
使用“&”引用父选择器
“&”运算符表示嵌套规则的父选择器,通常用于将修改类或伪类应用于现有选择器:
a {
  color: blue;
  &:hover {
    color: green;
  }
}
结果为:
a {
  color: blue;
}
a:hover {
  color: green;
}
请注意,如果没有“&”,上面的示例将导致“a :hover”规则(匹配在<a>标签内悬停的元素的后代选择器),这不是我们通常希望使用嵌套的:hover。
“父选择器”运算符有各种用途。基本上,任何时候你需要将嵌套规则的选择器以其它方式组合而不是默认方式时都可以使用。例如,“&”的另一个典型用途是生成重复的类名:
.button {
  &-ok {
    background-image: url("ok.png");
  }
  &-cancel {
    background-image: url("cancel.png");
  }
  &-custom {
    background-image: url("custom.png");
  }
}
输出:
.button-ok {
  background-image: url("ok.png");
}
.button-cancel {
  background-image: url("cancel.png");
}
.button-custom {
  background-image: url("custom.png");
}
多个“&”
“&”可以在选择器中出现多次。这使得可以重复引用父选择器而不重复其名称。
.link {
  & + & {
    color: red;
  }
  & & {
    color: green;
  }
  && {
    color: blue;
  }
  &,
  &ish {
    color: cyan;
  }
}
将输出:
.link + .link {
  color: red;
}
.link .link {
  color: green;
}
.link.link {
  color: blue;
}
.link,
.linkish {
  color: cyan;
}
请注意,“&”表示所有父选择器(而不仅仅是最近的祖先),因此以下示例:
.grand {
  .parent {
    & > & {
      color: red;
    }
    & & {
      color: green;
    }
    && {
      color: blue;
    }
    &,
    &ish {
      color: cyan;
    }
  }
}
结果为:
.grand .parent > .grand .parent {
  color: red;
}
.grand .parent .grand .parent {
  color: green;
}
.grand .parent.grand .parent {
  color: blue;
}
.grand .parent,
.grand .parentish {
  color: cyan;
}
更改选择器顺序
在继承(父)选择器之前添加选择器可能很有用。这可以通过将“&”放在当前选择器之后来完成。 例如,当使用 Modernizr 时,你可能希望根据支持的功能指定不同的规则。
.header {
  .menu {
    border-radius: 5px;
    .no-borderradius & {
      background-image: url('images/button-background.png');
    }
  }
}
选择器.no-borderradius &将在输出时将.no-borderradius前置到其父级.header .menu上,形成.no-borderradius .header .menu:
.header .menu {
  border-radius: 5px;
}
.no-borderradius .header .menu {
  background-image: url("images/button-background.png");
}
Combinatorial Explosion
&还可以用于生成逗号分隔列表中选择器的所有可能排列:
p,
a,
ul,
li {
  border-top: 2px dotted #366;
  & + & {
    border-top: 0;
  }
}
这将扩展为指定元素的所有可能组合(16 个):
p,
a,
ul,
li {
  border-top: 2px dotted #366;
}
p + p,
p + a,
p + ul,
p + li,
a + p,
a + a,
a + ul,
a + li,
ul + p,
ul + a,
ul + ul,
ul + li,
li + p,
li + a,
li + ul,
li + li {
  border-top: 0;
}