BEM的命名规范

基本概念

CSS 的命名规范又叫作BEM规范,为的是结束混乱的命名方式,达到一个语义化的CSS命名方式。 BEM是三个单词的缩写:Block(块)表明更高级别的抽象或组件,Element(元素) Block的后代,以及Modifier(修饰) 不一样状态的修饰符。布局

命名方法:flex

.block__element--modifier {
    display: flex;
}
.block--modifier {
    display: flex;
}
.block__element {
    display: flex;
}

<p class="header">
    <p class="header__body">
        <button class="header__button--primary"></button>
        <button class="header__button--default"></button>
    </p>
</p>

经过BEM的命名规范咱们能够达到一个什么目的呢?就是有一个清晰的描述,从上面的代码中咱们能够看到一层一层的清晰明了,并且有一个清晰的结构。code

1 block

block 表明一个更高级别的抽象或者是一个组件,它仅仅做为一个边界。它主要的功能有下面三点:element

负责描述功能的,不该该包含状态。it

/* correct */
    .header {
    
    }
    
    /* wrong */
    .header--select {
    
    }

不影响自身布局,不包含具体的样式,也就是block里面不该该加样式class

/* correct */
    .header {
    
    }
    
    /* wrong */
    .header {
        margin-top: 50px;
    }

不能使用元素选择器和ID选择器select

/* correct */
    .header {
    
    }
    
    /* wrong */
    .header a {
        margin-top: 50px;
    }

2 Element

是用一个双下划线隔开方法

/* correct */
  .header__body {
      margin-top: 50px;
  }
  
  /* wrong */
  .header .body {
      margin-top: 50px;
  }

表示的是目的,而不是状态,以下例子:目的是在header下面定义三个区域 body、logo、title,可是并无指定任何状态。im

.header__body {
      margin-top: 50px;
  }
  
  .header__logo {
      margin-top: 50px;
  }
  
  .header__title {
      margin-top: 50px;
  }

不能脱离Block父级单独使用命名

/* correct */
  <p class="header">
      <p class="header__body">
          <button class="header__button--primary"></button>
          <button class="header__button--default"></button>
      </p>
  </p>
  
  /* wrong */
  <p>
      <p class="header__body">
          <button class="header__button--primary"></button>
          <button class="header__button--default"></button>
      </p>
  </p>

3 Modifier

表示的是一个状态,是用双横杠分开的。

.header__button--default {
      background: none;
  }
Boolean
  .header__button--select {
      background: none;
  }
枚举
  .header__button--primary {
      background: #329FD9;
  }

不能单独使用

/* correct */
  <p class="header">
      <p class="header__body">
          <button class="header__button--primary"></button>
          <button class="header__button--default"></button>
      </p>
  </p>
  
  /* wrong */
  <p>
      <p>
          <button class="header__button--primary"></button>
          <button class="header__button--default"></button>
      </p>
  </p>

BEM在预处理器语言中的使用

在Sass中的使用

.header {
        &__body {
            padding: 20px;
        }
    
        &__button {
            &--primary {
                background: #329FD9;
            }
            &--default {
                background: none;
            }
        }
    }

在Less中的使用

@classname: header;
    
    .@{classname} {
        .@{classname}__body {
            padding: 20px;
        }
    
        .@{classname}__button {
            .@{classname}__button--primary {
                background: #329FD9;
            }
    
            .@{classname}__button--default {
                background: none;
            }
        }
    }
相关文章
相关标签/搜索