父選擇器

父選擇器 & 是 Sass 發明的一種特殊選擇器,用於巢狀選擇器中,以引用外部選擇器。它可以更複雜的方式重複使用外部選擇器,例如新增偽類別或在父選擇器*之前*新增選擇器。

當在內部選擇器中使用父選擇器時,它會被對應的外部選擇器取代。這取代了一般的巢狀行為。

遊樂場

SCSS 語法

.alert {
  // The parent selector can be used to add pseudo-classes to the outer
  // selector.
  &:hover {
    font-weight: bold;
  }

  // It can also be used to style the outer selector in a certain context, such
  // as a body set to use a right-to-left language.
  [dir=rtl] & {
    margin-left: 0;
    margin-right: 10px;
  }

  // You can even use it as an argument to pseudo-class selectors.
  :not(&) {
    opacity: 0.8;
  }
}
遊樂場

Sass 語法

.alert
  // The parent selector can be used to add pseudo-classes to the outer
  // selector.
  &:hover
    font-weight: bold


  // It can also be used to style the outer selector in a certain context, such
  // as a body set to use a right-to-left language.
  [dir=rtl] &
    margin-left: 0
    margin-right: 10px


  // You can even use it as an argument to pseudo-class selectors.
  :not(&)
    opacity: 0.8


CSS 輸出

.alert:hover {
  font-weight: bold;
}
[dir=rtl] .alert {
  margin-left: 0;
  margin-right: 10px;
}
:not(.alert) {
  opacity: 0.8;
}









⚠️ 注意!

因為父選擇器可能會被類型選擇器(例如 h1)取代,所以它只允許在複合選擇器的開頭使用,類型選擇器也允許在開頭使用。例如,不允許使用 span&

不過,我們正在研究放寬這項限制。如果您想協助實現這一點,請查看這個 GitHub 問題

新增後綴新增後綴永久連結

您也可以使用父選擇器向外部選擇器添加額外的後綴。當使用像 BEM 這樣使用高度結構化類別名稱的方法時,這特別有用。只要外部選擇器以字母數字名稱結尾(例如類別、ID 和元素選擇器),您就可以使用父選擇器來附加額外的文字。

遊樂場

SCSS 語法

.accordion {
  max-width: 600px;
  margin: 4rem auto;
  width: 90%;
  font-family: "Raleway", sans-serif;
  background: #f4f4f4;

  &__copy {
    display: none;
    padding: 1rem 1.5rem 2rem 1.5rem;
    color: gray;
    line-height: 1.6;
    font-size: 14px;
    font-weight: 500;

    &--open {
      display: block;
    }
  }
}
遊樂場

Sass 語法

.accordion
  max-width: 600px
  margin: 4rem auto
  width: 90%
  font-family: "Raleway", sans-serif
  background: #f4f4f4

  &__copy
    display: none
    padding: 1rem 1.5rem 2rem 1.5rem
    color: gray
    line-height: 1.6
    font-size: 14px
    font-weight: 500

    &--open
      display: block



CSS 輸出

.accordion {
  max-width: 600px;
  margin: 4rem auto;
  width: 90%;
  font-family: "Raleway", sans-serif;
  background: #f4f4f4;
}
.accordion__copy {
  display: none;
  padding: 1rem 1.5rem 2rem 1.5rem;
  color: gray;
  line-height: 1.6;
  font-size: 14px;
  font-weight: 500;
}
.accordion__copy--open {
  display: block;
}


在 SassScript 中SassScript 永久連結

父選擇器也可以在 SassScript 中使用。它是一個特殊的表達式,會以 選擇器函式 使用的相同格式返回目前的父選擇器:一個以逗號分隔的列表(選擇器列表),其中包含以空格分隔的列表(複雜選擇器),而這些列表又包含未加引號的字串(複合選擇器)。

遊樂場

SCSS 語法

.main aside:hover,
.sidebar p {
  parent-selector: &;
  // => ((unquote(".main") unquote("aside:hover")),
  //     (unquote(".sidebar") unquote("p")))
}
遊樂場

Sass 語法

.main aside:hover,
.sidebar p
  parent-selector: &
  // => ((unquote(".main") unquote("aside:hover")),
  //     (unquote(".sidebar") unquote("p")))

CSS 輸出

.main aside:hover,
.sidebar p {
  parent-selector: .main aside:hover, .sidebar p;
}


如果 & 表達式在任何樣式規則之外使用,它會返回 null。由於 null假值,這表示您可以輕鬆地使用它來判斷 mixin 是否在樣式規則中被呼叫。

遊樂場

SCSS 語法

@mixin app-background($color) {
  #{if(&, '&.app-background', '.app-background')} {
    background-color: $color;
    color: rgba(#fff, 0.75);
  }
}

@include app-background(#036);

.sidebar {
  @include app-background(#c6538c);
}
遊樂場

Sass 語法

@mixin app-background($color)
  #{if(&, '&.app-background', '.app-background')}
    background-color: $color
    color: rgba(#fff, 0.75)



@include app-background(#036)

.sidebar
  @include app-background(#c6538c)

CSS 輸出

.app-background {
  background-color: #036;
  color: rgba(255, 255, 255, 0.75);
}

.sidebar.app-background {
  background-color: #c6538c;
  color: rgba(255, 255, 255, 0.75);
}



進階巢狀進階巢狀永久連結

您可以將 & 作為一般的 SassScript 表達式使用,這表示您可以將其傳遞給函式或將其包含在插值中,甚至在其他選擇器中!將它與 選擇器函式@at-root 規則 結合使用,可以讓您以非常強大的方式巢狀選擇器。

例如,假設您想要編寫一個匹配外部選擇器*和*元素選擇器的選擇器。您可以編寫一個像這樣的 mixin,它使用 selector.unify() 函式& 與使用者的選擇器結合。

遊樂場

SCSS 語法

@use "sass:selector";

@mixin unify-parent($child) {
  @at-root #{selector.unify(&, $child)} {
    @content;
  }
}

.wrapper .field {
  @include unify-parent("input") {
    /* ... */
  }
  @include unify-parent("select") {
    /* ... */
  }
}
遊樂場

Sass 語法

@use "sass:selector"

@mixin unify-parent($child)
  @at-root #{selector.unify(&, $child)}
    @content



.wrapper .field
  @include unify-parent("input")
    /* ... */

  @include unify-parent("select")
    /* ... */


CSS 輸出

.wrapper input.field {
  /* ... */
}

.wrapper select.field {
  /* ... */
}









⚠️ 注意!

當 Sass 巢狀選擇器時,它不知道使用了什麼插值來產生它們。這表示它會自動將外部選擇器添加到內部選擇器中,*即使* 您使用 & 作為 SassScript 表達式。這就是為什麼您需要明確地使用 @at-root 規則 來告訴 Sass 不要包含外部選擇器。