via BEM by Example 侵删
带单个修饰符的组件
一个组件可能有不同状态。状态应该使用修饰符类来实现。
1 2 3 4 5 6 7 8 9 10 11 12
| <button class="btn btn--secondary"></button>
<style lang="scss"> .btn { display: inline-block; color: blue; &--secondary { color: green; } } </style>
|
不要单独使用修饰符。修饰符的作用是增加而不是替换基类。
1 2 3 4 5 6 7 8 9
| <button class="btn--secondary"></button>
<style lang="scss"> .btn--secondary { display: inline-block; color: green; } </style>
|
带子元素的组件
更复杂的组件含有子元素。
原则上不要使用标签选择器,你不知道<li>
里面是否还会出现嵌套的<ul><li>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <figure class="photo"> <img class="photo__img" src="me.jpg"> <figcaption class="photo__caption">Look at me!</figcaption> </figure>
<style lang="scss"> .photo { &__img { } &__caption { } } </style>
<figure class="photo"> <img src="me.jpg"> <figcaption>Look at me!</figcaption> </figure>
<style lang="scss"> .photo { img { } figcaption { } } </style>
|
如果您的组件的子元素有几个层次,请不要尝试在类名称中表示每个层次。
BEM并非旨在传达结构深度。
表示组件中子元素的BEM类名称应仅包括基本/块名称和一个元素名称。
在下面的示例中,请注意photo__caption__quote
是BEM的不正确用法,而photo__quote
更合适。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <figure class="photo"> <img class="photo__img" src="me.jpg"> <figcaption class="photo__caption"> <blockquote class="photo__quote"> Look at me! </blockquote> </figcaption> </figure>
<style lang="scss"> .photo { &__img { } &__caption { } &__quote { } } </style>
<figure class="photo"> <img class="photo__img" src="me.jpg"> <figcaption class="photo__caption"> <blockquote class="photo__caption__quote"> Look at me! </blockquote> </figcaption> </figure>
<style lang="scss"> .photo { &__img { } &__caption { &__quote { } // 在类名中永远不要包含多个子元素 } } </style>
|
带修饰符的组件
在某些情况下,您可能希望更改组件中的单个元素。在这些情况下,请向元素而不是组件添加修饰符。
我发现,修改元素比修改整个组件要少见得多,也没什么用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <figure class="photo"> <img class="photo__img photo__img--framed" src="me.jpg"> <figcaption class="photo__caption photo__caption--large">Look at me!</figcaption> </figure>
<style lang="scss"> .photo{ &__img--framed { } &__caption--large { } } </style>
|
基于组件修改器的样式元素
如果你发现正在以相同的方式修改同一组件的元素,则可以考虑将修改器添加到组件本身。
并基于该修改器为每个子元素调整样式。这将增加css层级,但使修改组件变得更加简单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <figure class="photo photo--highlighted"> <img class="photo__img" src="me.jpg"> <figcaption class="photo__caption">Look at me!</figcaption> </figure>
<style lang="scss"> .photo{ &--highlighted { .photo__img { } .photo__caption { } } } </style>
<figure class="photo"> <img class="photo__img photo__img--highlighted" src="me.jpg"> <figcaption class="photo__caption photo__caption--highlighted">Look at me!</figcaption> </figure>
<style lang="scss"> .photo__img--highlighted { } .photo__caption--highlighted { } </style>
|
多词名称
BEM名称有意使用双下划线和双连字符而不是单个来分隔块元素修饰符。原因是可以将单个连字符(-
)用作单词分隔符。
class名称应易于阅读,慎用缩写。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <div class="some-thesis some-thesis--fast-read"> <div class="some-thesis__some-element"></div> </div>
<style lang="scss"> .some-thesis { &--fast-read { } &__some-element { } } </style>
// These class names are harder to read <div class="somethesis somethesis--fastread"> <div class="somethesis__someelement"></div> </div>
<style lang="scss"> .somethesis { &--fastread { } &__someelement { } } </style>
|