资讯专栏INFORMATION COLUMN

BEM——前端命名规范介绍

Jason / 2535人阅读

摘要:什么是是由团队提出的一种前端命名规范。用法嵌套可以相互嵌套,并且嵌套数量任意。决定了命名空间,确保不被其他影响。何时用何时用使用创建定义和的外观状态或者行为。特征表示其表现其状态和其行为命名方法以单下划线与或者隔开。

什么是BEM?

BEM(Block, Element, Modifier)是由Yandex团队提出的一种前端命名规范。其核心思想是将页面拆分成一个个独立的富有语义的块(blocks),从而使得团队在开发复杂的项目变得高效,并且十分有利于代码复用,即便团队引入新成员,也容易维护。在某种程度上,BEM和OOP是相似的。

Block

Block是逻辑和功能独立的单元,类似于组件。每个block包含自身的行为(js)、结构(HTML模板)、表现(css)。block的独立性有利于代码的复用,有利于项目管理。

Block特点

1.block名描述block功能 ("What is it?" — menu or button), 不包含其状态 ("What does it look like?" — red or big)。block可以嵌套、复用。


block可以嵌套,并且可以嵌套任意多个block


2.block不影响自身布局,也就是说不能设置margin和position属性。

3.不能在BEM中使用元素选择器和ID选择器。

Element

Element 是block的组成部分,并且不能脱离block使用。

Element特点

1.element表示其目的( item, text, etc.), 而不是其状态( red, big, etc.).

2.Element的命名方式:block-name__element-name. element名字和block名字以双下划线分开。


Element用法——嵌套

Elements 可以相互嵌套,并且嵌套数量任意。element只能是block的一部分,也就是说element的命名层级不能是block__elem1__elem2。


block决定了命名空间,确保elements不被其他block影响。

block中的element在css中不需要跟block一起使用,而是独立定义规则。这样,当修改bolck的结构时不需要修改css。

.block {}
.block__elem1 {}
.block__elem2 {}
.block__elem3 {}

The block"s structure changes, but the rules for the elements and their names remain the same.

Element与block的关系

elementy只能作为block的一部分使用,不能独立使用。



block不一定含有element。


何时用Element何时用block

使用block: If a section of code might be reused and it doesn"t depend on other page components being implemented.
创建element:If a section of code can"t be used separately without the parent entity (the block).

Modifier

Modifier定义block和element的外观,状态,或者行为。

Modifier 特征

Modifier表示其表现("What size?" or "Which theme?" and so on — size_s or theme_islands), 其状态 ("How is it different from the others?" — disabled, focused, etc.) 和其行为 ("How does it behave?" or "How does it respond to the user?" — such as directions_left-top).

modifier命名方法:以单下划线与block 或者 element 隔开。

modifier类型:Boolean

modifier类型:Key-value

modifier不能多带带使用


文件组织结构

BEM理论也可以应用到工程目录的组织架构中。blocks, elements, 和 modifiers将分开为独立的文件。

Features:

1.A single block corresponds to a single directory.

2.The block and the directory have the same name. For example, the header block is in the header/ directory, and the menu block is in the menu/ directory.

3.A block"s implementation is divided into separate technology files. For example, header.css and header.js.

4.The block directory is the root directory for the subdirectories of its elements and modifiers.

5.Names of element directories begin with a double underscore (__). For example, header/__logo/ and menu/__item/.

6.Names of modifier directories begin with a single underscore (_). For example, header/_fixed/ and menu/_theme_islands/.

7.Implementations of elements and modifiers are divided into separate technology files. For example, header__input.js and header_theme_islands.css.

search-form/                           # Directory of the search-form

    __input/                           # Subdirectory of the search-form__input
        search-form__input.css         # CSS implementation of the
                                       # search-form__input element
        search-form__input.js          # JavaScript implementation of the
                                       # search-form__input element

    __button/                          # Subdirectory of the search-form__button
                                       # element
        search-form__button.css
        search-form__button.js

    _theme/                            # Subdirectory of the search-form_theme
                                       # modifier
        search-form_theme_islands.css  # CSS implementation of the search-form block
                                       # that has the theme modifier with the value
                                       # islands
        search-form_theme_lite.css     # CSS implementation of the search-form block
                                       # that has the theme modifier with the value
                                       # lite

    search-form.css                    # CSS implementation of the search-form block
    search-form.js                     # JavaScript implementation of the
                                       # search-form block

参考更多

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/112969.html

相关文章

  • 前端领域的BEM到底是什么

    摘要:在这篇译什么是我们为什么需要他们的结尾处,明确指出不需要,那么到底是什么呢下面我将把官网的教程翻译出来,带领大家搞清楚前端领域的到底是什么。命名在计算机科学领域,只有个非常难解决的问题一个是缓存失效,而另一个则是命名。 在这篇【译】什么是CSS Modules ?我们为什么需要他们?的结尾处,明确指出CSS Modules不需要BEM,那么BEM到底是什么呢? 下面我将把BEM官网的教...

    GitChat 评论0 收藏0
  • CSS命名规范

    摘要:本篇介绍几种命名规范。使用的网站四其他命名规范等减少对结构的依赖增加重复性的使用几种命名规范比较与在命名上相反的点可以放心使用,以为都是在模块内但不推荐当前我们的网站略有思想更概括,中的,相当于的,相当于的,相当于的中文 本篇介绍几种CSS命名规范。 (规范详细请参考底部References) 一、NEC (nice easy css) 网易前端CSS开源项目 1.1 样式分类 重...

    includecmath 评论0 收藏0
  • 前端编码规范之:样式(scss)编码规范

    摘要:前端编码规范之使用规范前端编码规范之样式编码规范前端编码规范之结构规范前端编码规范之最佳实践前端编码规范之编码规范命名的原则是通俗易懂,尽量保持不重复冲突,尽量不要用。我觉得应该避免出现出现这种方式用预处理器拼接出来的名称,会生成。 前端编码规范之:Git使用规范 前端编码规范之:样式(scss)编码规范 前端编码规范之:HTML结构规范 前端编码规范之:Vue最佳实践 前端编码规范...

    reclay 评论0 收藏0
  • CSS方法论(一)

    摘要:由于年提出,这基于她在雅虎的工作。但是这很难做到解决的问题样式全局性造成的样式冲突问题多人协作的命名问题解决层叠问题,使的优先级保持相对扁平的模块化,使更具有复用的能力于年由提出,当时他在雅虎工作。 编写CSS会遇到什么问题? 其实CSS很好写,只要知道css语法,你就可以写出来,通过各种学习,你也可以做出一个很美丽的页面。对能熟练编写网页的人来说,可以很简单的将设计图变成网页。但是在...

    haoguo 评论0 收藏0
  • CSS规范--BEM入门

    摘要:一开始,公司推出的,包括了规范以及其配套构建工具。代表的不同状态或不同版本。再来看一个之前用常规方式命名的的例子这些类名真是太不精确了,并不能告诉我们足够的信息。 这段时间在整理前端部分的代码规范,前面提到的CSS规范里面会涉及到选择器的命名,就参考BEM的命名规范,内容整理如下,供大家参考,请斧正!如大家有兴趣,可移步至CSS编码规范 BEM是由Yandex公司推出的一套CSS命名...

    li21 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<