0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[SCSS]親の親セレクタを &(アンパサンド) を使って引っ張りたい時の方法

Posted at

親の親セレクタを &(アンパサンド) を使って引っ張りたい時の方法

// 最終的な出力
.parent .parent-child {
  color: #fff;
}
.parent:hover .parent-child {
  color: #000;
}

これまで無理だと思ってたのでこうしてた

.parent {
  &-child {
    color: #fff;
  }
  &:hover {
    .parent-child {
      color: #000;
    }
  }
}

以下のように 変数で定義すると成功した

.parent {
  &-child {
    color: #fff;
  }

  //$key = .parent
  $selector: #{&};

  &:hover {
    // parent:hover .parent-child になる
    & #{$selector}-child {
      color: #000;
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?