Home  >  Article  >  Web Front-end  >  How to write elegant and durable css code? Sharing css naming tips!

How to write elegant and durable css code? Sharing css naming tips!

青灯夜游
青灯夜游forward
2022-09-15 11:28:351664browse

If you've been working with CSS for a while, you know that inheritance, specificity, and naming are some of the hardest things to deal with. Since all CSS is global, as your codebase grows, it becomes increasingly difficult to prevent styles from conflicting with each other and causing unintended side effects! The following article will share with you some CSS naming tips to make our code elegant and attractive. I hope it will be helpful to everyone!

How to write elegant and durable css code? Sharing css naming tips!

CSS Naming——BEM

What is BEM

BEM is a CSS naming convention. [Recommended learning: css video tutorial]

BEM stands for "block, element, modifier".

In the selector, the following three symbols represent extended relationships:

-   中划线 :仅作为连字符使用,表示某个块或者某个子元素的多单词之间的连接记号。
__  双下划线:双下划线用来连接块和块的子元素
_   单下划线:单下划线用来描述一个块或者块的子元素的一种状态

Use BEM example comparison

Block

As in the example below, li.item is a sub-element of the list

/* 常规写法和BEM写法相同 */
.list

Element

/* 常规写法 */ 
  <ul class="list">
    <li class="item">Pricing</li>
    <li class="item">Contact</li>
  </ul>
  
/* BEM写法 */   
   <ul class="list">
    <li class="list__item">Pricing</li>
    <li class="list__item">Contact</li>
  </ul>
/* 常规写法 */ 
.list{} 
.list .item{} 

/* BEM写法 */ 
.list{} 
.list__item{}

Modifier

A "modifier" can be understood as a specific state of a block!

/* 常规写法 */ 
  <ul class="list">
    <li class="item">Pricing</li>
    <li class="item">Contact</li>
  </ul>
  
/* BEM写法 */   
  <ul class="list">
    <li class="list__item_active">
      Pricing
    </li>
    <li class="list__item">Contact</li>
  </ul>
rrree

Benefits of BEM

    ##Get rid of the trouble of specificity
  • Fix inheritance problems
  • stop worrying about naming

Several principles of css naming

##Short names are better than long names Because short naming shortens writing time, it also reduces the size of css files! For example, this example:

/* 常规写法 */ 
.list{} 
.list .item{} 
.list .item.active{} 

/* BEM写法 */ 
.list{} 
.list__item{}
.list__item_active{}

Combined naming is better than single naming

//  推荐
.some-intro{...}

// 不推荐
.some-introduction{...}

Attribute-oriented naming and Semantic namingAttribute-oriented naming means that the naming of the selector follows the specific CSS style and has nothing to do with the project, page, or module. For example, the more classic clear floating class name.clearfix:

// 不建议
.header{...}

//推荐
.cs-header{...}

The semantic-oriented naming is named according to the context in which the application element is located. For example:

.clearfix:after { content : ''; display: table; clear: both; }

The above two naming methods have their own advantages and disadvantages:

1. Attribute-oriented

    Advantages
  • : Reuse of CSS High efficiency, best performance, plug and play, convenient and fast, and development is also extremely fast because it saves a lot of time switching between HTML and CSS files.
  • Disadvantages
  • : Due to its single attribute, its applicable scenarios are limited. In addition, because it is easy to use, it is easily overused, resulting in higher maintenance costs.
  • 2. Semantics-oriented

    Advantages
  • It has a wide range of application scenarios, can achieve very exquisite layout effects, and is easy to expand;
  • Disadvantages
  • : The code is long and the development efficiency is average, because all HTML needs to be named, even if it is a 10-pixel spacing. This leads many developers to either choose to use the tag selector directly, or to choose a simple class name and then define the style through the parent-child relationship, which results in worse maintenance problems.
Name summary recommendation

Status

.header { background-color: #333; color: #fff; }
.logo {font-size: 0; color : transparent;}

Layout

前一个    prev
后一个    next
当前的    current

显示的    show
隐藏的    hide
打开的    open
关闭的    close

选中的    selected
有效的    active
默认的    default
反转的    toggle

禁用的    disabled
危险的    danger
主要的    primary
成功的    success
提醒的    info
警告的    warning
出错的    error

大型的    lg
小型的    sm
超小的    xs

Common Parts

文档    doc
头部    header(hd)
主体    body    
尾部    footer(ft)    
主栏    main
侧栏    side    
容器    box/container

Component

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

Semantic widget

按钮        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

Functional widget

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

Recommended naming websitecodelf: https://unbug.github.io/codelf

Reference document:

1. "CSS Selection World" by Zhang Xinxu

2. https://www.cnblogs.com/qianxiaox/p/13816077.html

(Learning video sharing:

webfrontend

)

The above is the detailed content of How to write elegant and durable css code? Sharing css naming tips!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete