Table of Contents
Previous words
Mainstream naming
Naming method
命名推荐
实践
Home Web Front-end HTML Tutorial CSS naming rules

CSS naming rules

Jul 18, 2017 pm 05:49 PM
css practice

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;}
Copy after login

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>
Copy after login

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>
Copy after login

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{}
    Copy after login

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

  • element, you need to set it as follows

    .list .item{}
    Copy after login

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

  • element, you need to set it as follows

    .list li{}
    Copy after login

    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{}
    }
    Copy after login

      但是,它有一个很严重的问题,就是如果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
    Copy after login

      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>
    Copy after login

      2、下划线

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

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

      3、首字母大写

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

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

    【组件】

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

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

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

     

    命名推荐

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

    【布局】

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

    【通用部件】

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

    【组件】

    按钮        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
    Copy after login

    【语义化小部件】

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

    【功能部件】

    左浮动    fl
    右浮动    fr
    清浮动    clear
    Copy after login

    【状态】

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

     

    实践

    <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>
    Copy after login

      关于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 of this Website
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Chat Commands and How to Use Them
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

    There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

    How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

    There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

    To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

    How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

    To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

    How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

    Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

    How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

    How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

    How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

    Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

    See all articles