什么'&'选择器选择?

时间:2012-07-04 20:26:32

标签: sass

this codepen中有一个css选择器&:hover该选择器匹配什么?

3 个答案:

答案 0 :(得分:35)

我认为&符是Sass功能。来自文档:

Referencing Parent Selectors: &

  

有时在其他情况下使用嵌套规则的父选择器很有用   比默认方式。例如,您可能想要特别的   当选择器悬停在身体​​上或身体上时的样式   元素有一定的类。在这些情况下,您可以明确地   使用&指定父选择器的插入位置   字符。

答案 1 :(得分:34)

完全。在萨斯你可以有这样的东西......

 div {
    background: green;

    p {
        background: red;

        &:hover {
            background: blue;
        }

        &:active {
           background: blue; 
        }
    }   
}

...当转换为CSS时会变成这样:

div {
    background: green;
}

div p {
    background: red;
}

div p:hover {
    background: blue;
}

div p:active {
    blue;
}

编辑:从& hover:到&:hover

答案 2 :(得分:4)

一种不太广为人知的用法是您可以将“&”添加到样式的末尾,以便父选择器成为子项。

例如:

 h3
   font-size: 20px
   margin-bottom: 10px

 .some-parent-selector &
   font-size: 24px
   margin-bottom: 20px

变,

  h3 {
    font-size: 20px;
    margin-bottom: 10px;
  }

  .some-parent-selector h3 {
    font-size: 24px;
    margin-bottom: 20px;
  }

你可以阅读更多关于&在这里使用

http://thesassway.com/intermediate/referencing-parent-selectors-using-ampersand

相关问题