3
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の変数を使って少し快適にBEMを使う

Posted at

cssのクラスをBEMを参考に命名しています。これにより名前の衝突の可能性を下げ安心してcssを書いていくことができます。反面、文字数が長くなるので打つのも読むの大変になります。

.my-awesome-block {
  padding: 20px;
}

.my-awesome-block__element-1 {
  > p {
    font-size: 16px;
  }
}

.my-wesome-block--modifier {
  .my-awesome-block__element-1 {
    > p {
      font-size: 24px;
    }
  }
}

Sassの機能、変数を使って読みやすくします。ファイル上部でどのようなブロック、モディファイア、エレメントが存在するかリストされる点も良いです。

$block: ".my-awesome-block";
$block-modified: "#{$block}__modifier":
$element1: "#{$block}__element-1";

#{$block} {
  padding: 20px;
}

#{$element1} {
  > p {
    font-size: 16px;
  }
}

#{$block-modified} {
  #{$element-1} {
    > p {
      font-size: 24px;
    }
  }
}
3
4
3

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
3
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?