10
4

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 5 years have passed since last update.

Sassで&を使ってネストするなら@at-rootを書かなくてもいい

Posted at

@at-rootを使うとこのようにネストを解除できます。

input.scss
.foo {
  color: red;

  @at-root .bar {
    color: blue;
  }

}
output.css
.foo {
  color: red;
}

.bar {
  color: blue;
}

しかし、&で名前を持って来たりするときには@at-rootを書く必要はありません。

input.scss
.foo {
  color: red;

  &-bar {
    color: blue;

    &-baz {
      color: green;
    }

  }

}
output.css
.foo {
  color: red;
}

.foo-bar {
  color: blue;
}

.foo-bar-baz {
  color: green;
}

これはBEM記法などでとても役に立ちます(僕はつい先日までいちいち@at-rootを書いてしまっていました)。

ちなみに、インターポレーション(#{})を使うとネストされるようです。

input.scss
.foo {
  color: red;

  #{&}-bar {
    color: blue;
  }

}
output.css
.foo {
  color: red;
}

.foo .foo-bar {
  color: blue;
}
10
4
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
10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?