Table of Contents
CSS Naming——BEM
Block
Element
Modifier
Home Web Front-end CSS Tutorial How to write elegant and durable css code? Sharing css naming tips!

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

Sep 15, 2022 am 11:28 AM
css

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:

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

Use BEM example comparison

Block

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

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

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>
Copy after login
/* 常规写法 */ 
.list{} 
.list .item{} 

/* BEM写法 */ 
.list{} 
.list__item{}
Copy after login

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

Combined naming is better than single naming

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

// 不推荐
.some-introduction{...}
Copy after login

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

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

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

Layout

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

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

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

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

大型的    lg
小型的    sm
超小的    xs
Copy after login

Common Parts

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

Component

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

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

Functional widget

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 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.

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 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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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 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 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 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.

See all articles