CSS中的伪元素之前的"&"是什么意思?

neo*_*ium 66 css twitter-bootstrap

在以下从Twitter Bootstrap中获取的CSS中&符号(&)是什么意思?

.clearfix {
  *zoom: 1;
  &:before,
  &:after {
    display: table;
    content: "";
  }
  &:after {
    clear: both;
  }
}
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 79

这是LESS,而不是CSS.

此语法允许嵌套嵌套选择器修饰符.

.clearfix { 
  &:before {
    content: '';
  }
}
Run Code Online (Sandbox Code Playgroud)

将编译为:

.clearfix:before {
  content: '';
}
Run Code Online (Sandbox Code Playgroud)

使用&,嵌套选择器编译为.clearfix:before.
没有它,他们编译到.clearfix :before.

  • @KatieK:Bootstrap是用LESS写的,而不是SASS (10认同)

ant*_*ore 14

嵌套&选择SASS和LESS中的父元素.它不仅适用于伪元素,它可以与任何类型的选择器一起使用.

例如

h1 {
    &.class {

    }
}
Run Code Online (Sandbox Code Playgroud)

相当于:

h1.class {

}
Run Code Online (Sandbox Code Playgroud)


小智 9

这是一个 SCSS/LESS 示例:

a {
   text-decoration: underline; 
   @include padding(15px);
   display: inline-block;

     & img  {
                padding-left: 7px;
               margin-top: -4px;
             }
 }
Run Code Online (Sandbox Code Playgroud)

和它在 CSS 中的等价物:

a {
  text-decoration: underline; 
  @include padding(15px);
  display: inline-block;
}

a img  {
     padding-left: 7px;
     margin-top: -4px;
   }
Run Code Online (Sandbox Code Playgroud)