親セレクタ

親セレクタである & は、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 ルール と組み合わせて使用​​すると、非常に強力な方法でセレクタをネストできます。

たとえば、外部セレクタ*と*要素セレクタの両方に一致するセレクタを作成したいとします。 selector.unify() 関数 を使用して & をユーザーのセレクタと組み合わせる、このような Mixin を記述できます。

プレイグラウンド

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 に指示する必要があります。