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ネストの落とし穴

Last updated at Posted at 2023-03-24

はじめに

皆さん、CSSやSCSSなどでコーディングしているとき、「セレクタの詳細度」を意識していますでしょうか?
私は全く意識せずにSCSSのネスト機能を使いまくっていたら困ったことになり、原因はセレクタの詳細度をガン無視していたことでした。

セレクタの詳細度とは?

セレクタが競合した時にどのセレクタを適用するか、という「優先順位」を決めるための数値のこと。
「idはclassよりも優先順位が高い」「!importantを付ければ間違いない」とはよく聞きます。

そしてセレクタは一つ指定するよりも二つ指定する方が優先順位は高くなります。
例えば、

html
<div class="test1 test2"></div>
css
.test1.test2 {
  width: 300px;
  height: 300px;
  margin-top: 50px;
  background-color: red;
}
.test1 {
  width: 300px;
  height: 300px;
  margin-top: 50px;
  background-color: blue;
}

というファイルがあり、通常は後に書いたbackground-color: blue;が適用されるが、セレクタを二つ指定する方が優先順位が高いため、background-color: red;の方が適用される。
スクリーンショット 2023-03-24 22.28.34.png
参考:

本題

SCSSにはネスト機能がありますが、上記で説明した通り、ネストすればするほどセレクタの詳細度は上がっていきます。
それが時に意図しない挙動の原因になる可能性を生む可能性があります。

例えば以下ののコードがあります。

html
<body>
  <div class="wrapper">
    <div class="box"></div>
  </div>
</body>
scss
.wrapper {
  width: 100%;
  .box {
    width: 300px;
    height: 300px;
    background-color: blue;
    margin: 50px auto 0;
  }
}

@media (max-width: 600px) {
  .box {
    background-color: red;
  }
}

上記は、青い要素を、@media (max-width: 600px)で画面が600px以下になると赤色にしようとしています。
しかしこのままでは600px以下でも赤にはなりません。
Image from Gyazo

なぜなら、セレクターの詳細度が違うからです。
コンパイルすると以下のようになります。

css
.wrapper {
  width: 100%;
}
.wrapper .box {
  width: 300px;
  height: 300px;
  background-color: blue;
  margin: 50px auto 0;
}

@media (max-width: 600px) {
  .box {
    background-color: red;
  }

ここで、最初に説明しました「セレクターは一つより二つ指定する方が詳細度が上がり、優先順位が高くなる。」が出てきます。
.boxより.wrapper .boxの方が優先順位が高くなるため、色が変わらなかったのです。

ではどうすればいいのかと言えば簡単で、優先順位が同じになるようにします。

scss
.wrapper {
  width: 100%;
}
.box {
  width: 300px;
  height: 300px;
  background-color: blue;
  margin: 50px auto 0;
}

@media (max-width: 600px) {
  .box {
    background-color: red;
  }
}

もしくは

scss
.wrapper {
  width: 100%;
  .box {
    width: 300px;
    height: 300px;
    background-color: blue;
    margin: 50px auto 0;
  }
}

@media (max-width: 600px) {
  .wrapper {
    .box {
      background-color: red;
    }
  }
}

このようにすれば優先順位が同じになり、意図した通りの挙動になります。
Image from Gyazo

終わりに

ネストできるとこはとりあえずネスト!、と思いネストしまくったら痛い目に遭いました。
よく考えずに書いても動くことが多いため、気にしたことがなかったのですが、これからは頭の隅に置きながらやろうと思いました...。

参考サイト

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?