Home  >  Article  >  Web Front-end  >  CSS naming rules

CSS naming rules

巴扎黑
巴扎黑Original
2017-07-18 17:49:252766browse

Previous words

Whenever writing HTML structure involves CSS naming, you have to struggle. There are many CSS naming specifications on the market, such as OOCSS, SMACSS, BEM and MVCSS. The most popular among them is probably BEM. This article will introduce CSS naming in detail

Mainstream naming

[BEM]

Speaking of CSS naming, of course BEM must be mentioned. BEM means B module (block), E element (element), and M modifier (modifier). The module and the sub-element are separated by two underscores, and the sub-element and the modifier are separated by two dashes

. Regarding the sub-element E, there are two ways to write it. One is to write according to hierarchical nesting, such as block-ele1-son-inner, but writing this way will cause the name to be too long; the other is to flatten, all sub-elements under a module B, Regardless of the mutual level, they are directly connected to B, such as block-inner, but this cannot express the hierarchical relationship, and conflicts may also occur when naming.

   BEM's naming is very good, Otherwise, it would not become the most popular naming method. However, BEM’s naming of sub-elements, whether it is hierarchical long naming or flat short naming, has flaws

[NEC]

Compared with BEM, which uses module B as the top-level element, the sub-elements The element class name contains the naming of the inheritance relationship. NetEase's NEC specification uses the descendant selector method

NEC divides elements into 5 categories: layout (grid) (.g-); module (module) (.m -); unit (.u-); function (.f-); skin (.s-); status (.z-). The descendant selector does not need to fully represent the structure tree level, and should be as short as possible

.m-list{margin:0;padding:0;}.m-list .itm{margin:1px;padding:1px;}.m-list .cnt{margin-left:100px;}

Personally, I think NetEase’s approach to element classification is very good. Some globally reusable functional modules are distinguished to make the structure clearer. However, I personally don't agree with the way descendant selectors are used. When the nesting level is deep, naming conflicts are still a problem

[JD]

JD.com’s naming rules use long names that represent hierarchical nesting relationships. When the descendant module exceeds level 4 or above, consider an identifiable independent abbreviation in the ancestor module as the new descendant module

<div class="modulename"><div class="modulename_cover"></div><div class="modulename_info"><div class="modulename_info_user"><div class="modulename_info_user_img"><img src="" alt=""><!-- 这个时候 miui 为 modulename_info_user_img 首字母缩写--><div class="miui_tit"></div><div class="miui_txt"></div>...</div></div><div class="modulename_info_list"></div></div></div>

The name of the factor element like Jingdong is too long and The method of using acronyms is very good. So far, there is no other better solution to long naming on the market

Naming method

[Descendant selector or class name]

Regarding CSS naming, the biggest debate is whether to use descendant selectors or class names. As shown in the following example

<ul class="list"><li class="list-item"></li><li class="list-item"></li><li class="list-item"></li></ul><ul class="list"><li class="item"></li><li class="item"></li><li class="item"></li></ul><ul class="list"><li></li><li></li><li></li></ul>

If you use the first long class name method to set the style for the

  • element, you only need to set it as follows

    .list-item{}

    If you use the second short class name method, set the style for the

  • element, you need to set it as follows

    .list .item{}

    If you use the third descendant selection To set the style for the

  • element, you need to set it as follows

    .list li{}

    From a simple point of view, the third descendant selector method is the simplest and does not require any effort. Time to name the child elements, and it’s easy to write in sass

    .list{
        li{}
    }

      但是,它有一个很严重的问题,就是如果HTML结构层级较深,往往出现选择器层级过长,如.list li span a{}

      而且,因为后代选择器强烈地依赖HTML结构,为了避免因为少写一层结构,导致选择器特殊性降低,样式无法生效的情况,也不得不这样写

      一个不得不提的问题是,CSS选择器的解析顺序是从右到左。而使用后代选择器.list li{},浏览器需要遍历出所有的li,再找出.list下的li,效率是最低的

      因此,个人认为第三种后代选择器的方式并不是好选择

      下面介绍第二种短类名的方式

      1、选择器解析效率比第三种方式好,毕竟.item比li的范围小很多

      2、短类名.list .item同样存在依赖HTML结构的情况,很可能出现选择器层级过长

      3、使用较简易,在sass中书写容易,且起名也较简单

      4、由于给li增加了类名,于是增加了HTML文件大小

      最后介绍第三种长类名的方式

      这种方式的选择器效率最高,因为.list-item这个类型页面中只出现一次,可类比于id选择器的解析速度

      由于使用长类名的方式,可以完全不使用后代选择器,则无需考虑选择器特殊性较低,样式无法生效的情况,也不会出现选择器层级过长,因为它仅有一级

      但是,相应地,它最大的缺点是类名较长,大大地增加了HTML文件大小。于是,可借鉴京东,当子孙模块超过3级时,采用首字母缩写,并将缩写后首字母大写的做法,在如将.list-item-link-title缩写为.Lil-title

      最终,选择可缩写的长类名作为CSS命名的主要方式

    【分隔符】

      一般地,classname分隔符有3种,中划线-,下划线_,以及首字母大写,以分隔list和item为例

    //中划线
    list-item
    //下划线
    list_item
    //首字母大写
    listItem

      1、中划线

      中划线可以用来表示层级关系

    <div class="box"><ul class="box-list"><li class="box-list-item"></li><li class="box-list-item"></li><li class="box-list-item"></li></ul></div>

      2、下划线

      下划线可以用来表示不同的状态

    <div class="box"><button class="box-btn box-btn_default" type="button"></button><button class="box-btn" type="button"></button></div>

      3、首字母大写

      首字母大写可以用来表示因为样式的需要,而不得不增加的HTML结构。一般地,如果在外层增加结构,可以增加Wrap,在内层增加结构,可以增加Inner,且不影响原先的classname的命名

    <div class="boxWrap"><section class="box"><h2 class="box-title"></h2><p class="box-content"></p></section>    </div>

    【组件】

      通过上面的长命名方式和分隔符的使用,解决了基础结构的命名。但是,在页面中,很可能出现一些组件的应用,这些组件可以复用到页面的多个位置。这时,再使用上面的方式就不太合适

      于是,可以以m-为前缀,来表示这是一个组件

    <div class="box"><button class="m-btn m-btn_error" type="button"></button><button class="m-btn" type="button"></button></div>

     

    命名推荐

      有了合适的命名方式,还需要语义化命名,且有不影响语义的情况下,可以简写

    【布局】

    <span style="color: #000000;">文档    doc
    头部    header(hd)
    主体    body    
    尾部    footer(ft)    
    主栏    main
    侧栏    side    
    容器    box/container<br></span>

    【通用部件】

    列表    list
    列表项  item
    表格    table    
    表单    form
    链接    link
    标题    caption/heading/title
    菜单    menu
    集合    group
    条      bar
    内容    content    
    结果    result

    【组件】

    按钮        button(btn)
    字体        icon
    下拉菜单     dropdown
    工具栏       toolbar
    分页         page
    缩略图       thumbnail
    警告框       alert
    进度条       progress
    导航条       navbar
    导航         nav    
    子导航       subnav
    面包屑       breadcrumb(crumb)    
    标签        label
    徽章        badge
    巨幕        jumbotron
    面板        panel
    洼地        well
    标签页      tab
    提示框      tooltip
    弹出框      popover
    轮播图      carousel
    手风琴      collapse 
    定位浮标    affix

    【语义化小部件】

    品牌        brand
    标志        logo
    额外部件    addon
    版权        copyright
    注册        regist(reg)
    登录        login
    搜索        search    
    热点        hot
    帮助        help
    信息        info
    提示        tips
    开关        toggle
    新闻        news
    广告        advertise(ad)
    排行        top    
    下载        download

    【功能部件】

    左浮动    fl
    右浮动    fr
    清浮动    clear

    【状态】

    前一个    previous
    后一个    next
    当前的    current
    
    显示的    show
    隐藏的    hide
    打开的    open
    关闭的    close
    
    选中的    selected
    有效的    active
    默认的    default
    反转的    toggle
    
    禁用的    disabled
    危险的    danger
    主要的    primary
    成功的    success
    提醒的    info
    警告的    warning
    出错的    error
    
    大型的    lg
    小型的    sm
    超小的    xs

     

    实践

    <header class="hd"><nav class="hd-navbar m-navbar m-varbar_primary"><div class="hd-navbar-tel">联系方式:400-888-8888</div><ul class="hd-navbar-nav"><li class="Hnn-itm m-btn m-btn_info"><a href="#">登录</a></li><li class="Hnn-itm m-btn"><a href="#">快速注册</a></li><li class="Hnn-itm m-btn"><a href="#">关于</a></li><li class="Hnn-itm m-btn"><a href="#">帮助</a></li></ul></nav>...</header>

      关于CSS命名,并没有最佳实践之说,根据项目的复杂程序进行合适的命名才是可取的  

      欢迎交流  

    The above is the detailed content of CSS naming rules. For more information, please follow other related articles on the PHP Chinese website!

  • Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn