BEM举例

带单个修饰符的组件

一个组件可能有不同状态。状态应该使用修饰符类来实现。
<!-- 这样写 -->
<button class="btn btn--secondary"></button>

<style lang="scss">
  .btn {
    display: inline-block;
    color: blue;
    &--secondary {
      color: green;
    }  
  }
</style>  
不要单独使用修饰符。修饰符的作用是增加而不是替换基类。
<!-- 别 -->
<button class="btn--secondary"></button>

<style lang="scss">
  .btn--secondary {
    display: inline-block;
    color: green;
  }  
</style>  

 

带子元素的组件

更复杂的组件含有子元素。
原则上不要使用标签选择器,你不知道<li>里面是否还会出现嵌套的<ul><li>
<!-- 这样写 -->
<figure class="photo">
  <img class="photo__img" src="me.jpg">
  <figcaption class="photo__caption">Look at me!</figcaption>
</figure>

<style lang="scss">
  .photo { /* css权重 of 10 */
    &__img { } /* css权重 of 10 */
    &__caption { } /* css权重 of 10 */
  }
</style>

<!-- 别 -->
<figure class="photo">
  <img src="me.jpg">
  <figcaption>Look at me!</figcaption>
</figure>

<style lang="scss">
  .photo { /* css权重 of 10 */
    img { } /* css权重 of 11 */
    figcaption { } /* css权重 of 11 */
  }
</style>
如果您的组件的子元素有几个层次,请不要尝试在类名称中表示每个层次。
BEM并非旨在传达结构深度。
表示组件中子元素的BEM类名称应仅包括基本/块名称和一个元素名称。
在下面的示例中,请注意photo__caption__quote是BEM的不正确用法,而photo__quote更合适。
<!-- 这样写 -->
<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>
 

带修饰符的组件

在某些情况下,您可能希望更改组件中的单个元素。在这些情况下,请向元素而不是组件添加修饰符。
我发现,修改元素比修改整个组件要少见得多,也没什么用。
<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层级,但使修改组件变得更加简单。
<!-- 这样写 -->
<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>

广州品牌设计公司https://www.houdianzi.com

多词名称

BEM名称有意使用双下划线和双连字符而不是单个来分隔块元素修饰符。原因是可以将单个连字符(-)用作单词分隔符。
class名称应易于阅读,慎用缩写
<!-- 这样写 -->
<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>
posted @ 2020-11-22 20:38  浅笑·  阅读(149)  评论(0编辑  收藏  举报